Creating UserForms


Writing macros and other code to accomplish things you need to do is a major part of using VBA, and UserForms are another. UserForms, which are the visual counterpart to Visual Basic procedures, provide you with the ability to interact with your users as if your creation was part of Microsoft Project itself.

Note  

All the normal rules and principles for working with Visual Basic code, including events and debugging, also apply to working with UserForms.

Creating a Simple Form

To begin designing a UserForm, click Insert, UserForm. A blank UserForm appears (see Figure 31-20) in place of the Code window (if showing) with the Toolbox window floating somewhere near it (see Figure 31-21). At the same time, the information in the Properties window changes to show properties for the new UserForm.

click to expand
Figure 31-20: A new UserForm window is the visual counterpart to a blank Code window.
Note  

For the remainder of this section, UserForms are simply referred to as forms.


Figure 31-21: Use the Toolbox to place visual components on your UserForm. You can use Tools, Additional Controls to place more "tools" in the Toolbox.

Follow these steps to change some of the default properties of the form:

  1. In the Properties window, change the value for the Name field from UserForm1 to frmWelcome .

    This changes the name of the form in the Project Explorer and is how you refer to the form, when necessary. Also, if you export it, frmWelcome.frm will be the filename of the form.

  2. Change the value for the Caption field from UserForm1 to Welcome! .

    This is what appears in the title bar of the form when it is running.

  3. Click the Font field and then use the Browse button to change the base font for the form from 8 point Tahoma to 10 point Arial (see Figure 31-22).


    Figure 31-22: Start designing your UserForm by changing certain properties.

    Although changing the font in the form doesn't have any visible effect, any controls you add to the form will use the font you specified as their default font. Changing the font now is quicker and easier than changing it for each control individually later.

Now that you've created a form and modified some of its properties, the next task is to add controls. Follow these steps to add two buttons , a label, and a text box:

  1. In the Toolbox window, click the TextBox button and then draw a rectangle on the form.

  2. In the Properties window, change the value for the Name field from TextBox1 to txtName .

  3. In the Toolbox window, click the Label button and then draw a rectangle just above the text box.

  4. In the Properties window, change the value for the Name field from Label1 to lblName .

  5. Change the value for the Caption field from Label1 to Enter your name :.

  6. In the Toolbox window, click the CommandButton button, draw a rectangle to the right of the label and the text box and then click the CommandButton button again to draw another button just below the first.

  7. Click the first button, change the value for Name from CommandButton1 to cmdOK , and then change the value for Caption from CommandButton1 to OK .

  8. Click the second button, and give it the name cmdCancel and the caption Cancel .

  9. Adjust the position of the controls and the size of the form (see Figure 31-23).


    Figure 31-23: This is what the completed frmWelcome should look like after the controls have been positioned and the form sized appropriately.

Adding Code to Your Form

Before your new form can be used to do anything, you must write event procedures for the controls on it. For the form created in the previous section, you need to write code only for the two buttons.

The button cmdCancel needs code to close the form's window, in case the user doesn't want to use the form. The quickest way to open the Code window for a form or control is to double-click the item while in design mode. If you double-click the Cancel button on the form, the Code window opens to the cmdCancel_Click() event procedure. Type the following code into the Cancel button's vClick event:

 frmWelcome.Hide 
Note  

The Me keyword       The Me keyword always refers to the class (a form is a kind of class) in which it is used. Instead of frmWelcome.Hide , for example, you could simply use Me.Hide .

The cmdOK button is where all the action is for this form. The OK button takes the name the user entered in the txtName box and adds it to the status bar at the bottom of the Microsoft Project window.

With the Code window open, click cmdOK in the Objects box to gain access to the cmdOK_Click() event. Type the following code into the OK button's Click event:

 Application.StatusBar = "What would you like to do now, " & _   txtName.Text & "?" 

This code sets the Application object's StatusBar property to be the text in quotes, plus the value of the txtName box's Text property.

Because you want the form to close after the user clicks the OK button, add the same code you added to the Cancel button's Click event:

 Me.Hide 

Integrating Your Form into Microsoft Project

You now have a form with controls and event procedures that perform actions when the controls are used, but no one will ever see the form until you determine how the form is activated. With many types of forms, it's enough that the user runs a macro, which then displays one or more forms as it runs.

For this form, though, you want to automatically display the form every time a particular plan is opened. Follow these steps to create an event procedure for the Project object that will display your form every time the plan is opened:

  1. In the Project Explorer, double-click ThisProject.

    The Code window opens.

  2. In the Objects box, click Project, and then type the following code into the Project object's Open event:

     frmWelcome.Show 
  3. In the Procedure/Events box, click BeforeClose and then type the following code into the Project object's BeforeClose event:

     Application.StatusBar = False 

    Because code tied to the Project object is changing an application-level property, you need this additional code to "clean up" the effects of using the form by resetting the StatusBar property to its default value (see Figure 31-24).


    Figure 31-24: The Microsoft Project status bar after someone has used the form.

    Note  

    The first form your users see when opening the plan is actually the macro virus security dialog box. If a user selects the Disable Macros button, none of the event procedures run and your form doesn't display.

    Cross-References  

    For more information about Project events, see "Creating Project Event Procedures" earlier in this chapter.

start sidebar
Three Birds, One Stone

If you like the idea behind the example of displaying a form every time a specific plan was opened, but want something a little more robust, you can avoid three of its potential detractions by working with the Global file (the ProjectGlobal project) instead of a particular plan (the VBAProject project):

  • To make the code frmWelcome.Show run every time Microsoft Project starts, expand the ProjectGlobal project and double-click the ThisProject object instead of the ThisProject object under the VBAProject project.

  • Because the StatusBar property is reset to its default when Microsoft Project closes , you don't need the BeforeClose event code.

  • Because of the special nature of the Global file, users don't see the macro virus security dialog box. This means there is no chance that the user will choose to disable the code that displays your form.

end sidebar
 

A more sophisticated version of this example is one that adds a new menu item or toolbar that can be used to access the forms you have created. This method integrates your forms with the Microsoft Project interface even more closely, although you probably wouldn't bother doing so for a simple, limited-use form such as the one you just created.

Follow these steps to create two Project event procedures that add a new item to the View menu (see Figure 31-25) that, when clicked, displays the frmWelcome form:


Figure 31-25: The new Show My UserForm command appears at the bottom of the View menu.
  1. Double-click ThisProject in the Project Explorer to open the Code window.

  2. In the Objects box, click Project, and then type the following code into the Project object's Open event:

     Dim objViewBar As CommandBar Dim objNewItem As CommandBarButton Dim intTotalItems As Integer     Set objViewBar = Application.CommandBars("View") intTotalItems = objViewBar.Controls.Count     If objViewBar.Controls(intTotalItems).Caption <> "Show my UserForm" Then     Set objNewItem = objViewBar.Controls.Add(Type:=msoControlButton)     objNewItem.Caption = "Show my UserForm"     objNewItem.BeginGroup = True     objNewItem.OnAction = "frmWelcome.Show" End If     Set objNewItem = Nothing Set objViewBar = Nothing 
  3. In the Procedures/Events box, click BeforeClose, and then type the following code into the Project object's BeforeClose event:

     Dim objViewBar As CommandBar Dim intTotalItems As Integer     Application.StatusBar = False     Set objViewBar = Application.CommandBars("View") intTotalItems = objViewBar.Controls.Count     If objViewBar.Controls(intTotalItems).Caption = "Show my UserForm" Then     objViewBar.Controls(intTotalItems).Delete End If     Set objViewBar = Nothing 

    Because code tied to the Project object is changing two application-level items, you need this additional code to "clean up" when the plan is closed by resetting the StatusBar property to its default value and removing the Show My UserForm command from the View menu. This procedure ensures that Microsoft Project is in its default state when other plans are opened.

    Note  

    Until the plan is closed, the new View menu item is visible from every other open plan. Clicking it from one of those plans, however, results in an error.




Microsoft Office Project 2003 Inside Out
Microsoft Office Project 2003 Inside Out
ISBN: 0735619581
EAN: 2147483647
Year: 2003
Pages: 268

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net