Adding code to display resource names

Initially, you need to add code to the UserForm so that when the form loads, it adds the names of all the resources in your project as items in the Resource drop-down list. Follow these steps to add the appropriate code:

1. Click anywhere on the UserForm (other than directly on a control), and choose ViewOCode. The Code window for the UserForm appears, as shown in Figure 25-20.

2. From the Object drop-down box at the upper-left corner of the Code window, select UserForm (it will most likely already be selected).

3. From the Procedure drop-down box at the upper-right corner of the Code window, select Initialize.

4. In the Code window, type the following code between the lines Private Sub UserForm_Initialize and End Sub:

Private Sub UserForm_Initialize() Dim r As Resource

For Each r In ActiveProject.Resources cboResource.AddItem r.Name

Next r End Sub

Object drop-down box

Procedure drop-down box Code window

Object drop-down box

Figure 25-20: The Code window for the UserForm allows you to add resource names to the ComboBox when the UserForm loads.

I'll now review this code to give you a better understanding of how it works.

Because you want this code to run before the form displays, you place the code within the UserForm_Initialize procedure. The code is automatically run by VBA when the UserForm loads.

The line Dim r As Resource declares a variable named r, and you instructed VBA to create this variable as a resource object. This allows you to manipulate a resource's properties and methods programmatically. An object's properties are the attributes that are associated with a type of object. A resource has many properties, including Name, Cost, and EMailAddress.

Note These properties are also fields that are associated with a resource in Project. In *""""' fact, all resource fields are properties of a resource object in VBA.

A method is a procedure or action that can be performed on an object to achieve a particular result. For example, the resource object has the Add method, which allows you to add resources to a project, and the AppendNotes method, which appends notes to the resource's notes field.

The line For Each r In ActiveProject.Resources begins a section of code known as a For. . . Next loop. The line Next r closes the loop. The lines of code that are contained within this loop run once for every resource in the project. The line cboResource.AddItem r.Name adds the Name property of the resource r as an item to the drop-down list. Each time that the code runs through the loop, the code keeps adding the next resource's name to the list until it reaches the last resource in the project.

You can now run the form and test some of its functionality. Choose RunORun Sub/UserForm (or press F5). The UserForm appears on the screen, as shown in Figure 25-21.

Figure 25-21: The completed Adjust Work dialog box, based on the UserForm.

On the UserForm, click the drop-down list, and you see a list of all the resources that are in your project. But when you try clicking the OK button or Cancel button, at this point nothing happens. Later in this chapter, you will add code to the form that will be associated with clicking the OK button and Cancel button. For now, close the dialog box.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment