« Back from the dead | Main | Microsoft Project 2007 Released »

Using a ComboBox in a Microsoft Project Userform

I got a question from someone who wanted to create a userform which would set some values in project. It was to be populated by some preset values. So here are the basics:

The first thing to do is to create a form. Going to the Visual Basic editor, you would choose "Insert / UserForm". This creates a blank userform and should display the toolbox.

MS_Project_Forms_Toolbox.jpg

Drag a combo box from the toolbox onto the form. It will be called "ComboBox1" if it is the first one. You can rename it using properties (and you should) but for this example we are not.

Now to write some code for the form. Hit F7 or go to the view menu and select "Code". This should bring up a window which you can type code in. The first thing is some code which will initialize the values for the combo box. I've called this simply "InitializeME" but name it what you like. The code in this sub has one line for each value you want to add. Here the values are hardcoded, but you could substitute it with code which reads values from a file stored somewhere or an array of values you have gathered from the project file itself. You are limited only by your imagination. Here is the code:

Sub InitializeME()
'Create your list
ComboBox1.AddItem "Foo"
ComboBox1.AddItem "Faa"
ComboBox1.AddItem "Fay"
ComboBox1.AddItem "Fat"
ComboBox1.AddItem "Fun"
'Set the initial value. Without this it will be blank
ComboBox1.Value = "Select a Value"
End Sub

Next you need some code to start the form. To keep this example pathetically simple we will only do two things here, initialize the combo box values and then show the form. You could do it the other way around, but better to have the form ready to go when the user first sees the form.

To do this insert a module (again from the insert menu). Then call the code to initialize the combo box, then show the form. The code is very simple:

sub ShowTheUserFormAlreadyPlease()
UserForm1.InitializeME
UserForm1.Show
end sub

You can run the code now. Go to tools macros and select the ShowTheUserFormAlreadyPlease macro. The form should display and have a working combo box. Of course it does NOTHING right now. So the next thing is to look at the methods of the combo box and see what it can do. If you look at the top of the code window you see on the left a box which says "ComboBox1". This selects an object to work on. On the right is a box which says "Change". This is a list of procedures associated with that object.

MS_Project_Form_Code.jpg

With the combo box selected on the left and the "change" procedure selected on the right the shell of a procedure will open and we will type code into it. This code sets the text1 field of selected tasks to the value that is selected in the combo box. When you change the combo box value it will run. Selecting a value in the combo box counts as a change. Here is the code:

Private Sub ComboBox1_Change()
If Not ComboBox1.Value = "Select a Value" Then
ActiveSelection.Tasks.Text1 = ComboBox1.Value
End If
End Sub

The only trick in there is the if then statement so that the text1 value does not change when you are setting the value of the combo box when it first displays. The code that you can put in the procedure again is only limited by what you can imagine. It could be extensive and create and initialize a new project with default values etc. As much as you are capable of.

Warning: I have not shown any error handling here. If you had no tasks selected you would have gotten an error or if there was no project open or ... so be careful and write procedures to handle that. And always test against whatever scenarios you can imagine.

RELATED POSTS
  • Iterating through Microsoft Project Subprojects
  • Microsoft Project Undo Levels and Macros
  • VBA Writing to a text file (MS Project, Excel)
  • MS Project VBA - Trim Function
  • Setting Microsoft Project Level Custom fields using VBA
  • VBA and Visual Basic For ... to ... statements
  • VBA to VSTO Tutorial Part Two - Adding a Command Bar and Buttons
  • Office VBA for Mac After 2008
  • Analyze Microsoft Project Resource Usage Data In Excel
  • Telling Time - ProjDateDiff, VB DateDiff and Application.DateDifference

  • Comments (1)

    Prathima:

    I don't know whether I can post this doubt here or not.
    It would be great if you can provide some infor regarding this.

    I am developing an application for a hosiptal which is an Outpatient Billing information. A small part of it to implement following

    I have to handle a combo box in a new form which should implement the following:

    when you start typing the name, the drop down menu would appear offering options including

    : selecting this option would take you to the new patient registration form

    a list of names in which the information you typed is contained in the last name of all patients in the database

    a list of names in which the information you typed is contained in the first names of all patients

    a list of names in which the Soundex of the letters you typed matches in the Soundex of either the first or last names

    For example, you type in "Smith” and the drop menu looks something like:

    Smith, John

    Blackwell, Smithson

    Smythe, Robert

    Selecting on the drop down menu should return the auto number index of that patient in the "patients" table.

    How to handle this?

    Prathima, Sorry, I'm not an expert on this, but I'd think that you would be best off passing what the user has typed to a database query. The dataset that is returned would then populate the combobox. There are a number of other details that you would need to address on top of this and I'm sorry but I don't have time to cover them all. -Jack

    Post a comment

    (Comments are moderated to fight SPAM and will be published after I have a chance to approve them. Thanks for waiting.)

    About

    The previous article is Back from the dead.

    The next article is Microsoft Project 2007 Released.

    Current articles are in the main index page and you can find a complete list of articles in the archives.

    Creative Commons License
    This weblog is licensed under a Creative Commons License.
    Powered by
    Movable Type 3.34