Placing Controls on a UserForm Project Management Help

We can begin work on the User Form by,Placing two command buttons: one that saves the data, and one that cancels the action by closing the User Form. To place a command button, select the button in the toolbox, and then click in the User Form to place a button of the default size. Drag instead of clicking to create a larger or smaller button (or simply click to place the default button and then resize it).To change the appearance of the button, you change the button’s properties. Select the first button and look at the Properties window, shown here:

Placing Controls on a UserForm

Placing Controls on a UserForm

For more information about a property, click its name in the Properties. window and press Fl. To change a property, click its name in the and then type a value or choose a value from a drop-down list. For example, you would change the button’s name by clicking on the Name property and typing text. The properties you’ll

Placing Controls on a UserForm

Placing Controls on a UserForm

Each form can have only one button where the Default property is True and one . button with a Cancel property that’s True, but they can be the same button. You set the Cancel button’s Default property to True when confirming an action that’s often selected by mistake, and when not canceling results in an Irreversible action such as deleting a file.

Placing Controls on a UserForm

Placing Controls on a UserForm

Adding Code to the Form

The procedures you wrote earlier by using the VB Editor or the Macro Recorder have been general procedures that can be run from a toolbar, menu bar, shortcut key, Macros calog box, or the VB IDE. El’ellt procedures are blocks of code attached to an object’s events that run in response to an event: a mouse click, a key press on the keyboard, or a emevent.

In this form, there are three events to care about. You need events to handle a user codding either of the two command buttons. And you will need a procedure attached to one of the UserForm’ s events to do a little setup when the form loads. You may be asking:
“But what about my text boxes?” You really don’t care when a user types an entry in the text boxes; you just care what the text is when they click the cmd Save Settings button. to apttoned Ok).If the user clicks the cmdCancel button (captioned Cancel), the contents of the text boxes won’t matter. But as soon as either command button is clicked, it’s time for action, so we’ll create code attached to events on these two command buttons.

Coding the Cancel Button

The Cancel button doesn’t need to do too much, so let’s create that event code first. Select the Cancel button, right-click, and choose View Code or double-click the command button to open the module attached to the User Form. You won’t find this module in the Project Explorer; it’s only accessible when you’re editing the User Form. You’ll notice that the snippets for the command buttons have already been created. There’s an underscore before Click in the subroutine names because the code is assigned to a button’s Click event

After you create an object, it’s added to the VBA Project’s Object list. You’ll find your form  Trave Cost Settings and all of its controls on the Objects list. To add the object name, just type enough unique letters of the name (such as ), hold Ctrl, and press the space bar to fill in the remainder. Type the period after the object name, and you’ll see the VB Editor’S Auto List Members feature, which opens a list of events and objects to choose from:

You can test the procedure by clicking the Run Macro button. The form will appear clicking the Cancel button should close the form and return you to the VBIDE

Coding the OK Button

When a user clicks the cmd Save Settings button, you want to store the location of the Excel worksheet and the rate that the user entered in the Text Box controls. You need to-store this information in a way that we can recall it when the user reopens the project file. A simple variable w’ill not work because, regardless of scope, variables are removed from memory when you unload the application they’re created in. To keep a value from one Project 2000 session to another, you need to use a document property.

You use the custom documents to store information that applies to the project as a whole. Here are the properties in the Project Object Browser:

option browser

option browser

time tun error

time tun error

After the appropriate information has been stored, we’ll use the same code as the Cancel button to hide and unload the User Form. Here’s the complete procedure for the cmd Save Settings button:

Private Sub cmdSaveSettings_Click()
DimobjDocProps As Documentj’roper-ties
DimobjPropLocation As DocumentProperty
DimobjPropRate As DocumentProperty
On Error ResumeNext
If (Len(txtXlsLocation.Text) – 0) Or _
(Len(txtMileageRate.Text) – 0) Then
MsgBox’You need to enter both a location
‘And a rate to proceed.’
Else
Set objDocProps – _
ActiveProject.CustomDocumentProperties
.~
If objPropRate Is Nothin~ Then
objDocProps.Add ‘Tra~elCostRatePerMile’, _
False, _
msoPropertyTypeString, _
txtMileageRate.Text
Set objPropLoctaion – obJDocProps(‘TravelCostWorksheet’)
If objPropLocation I~ Nothing Then
objDocProps.Add ‘TravelCostWorksheet’, False, _
msoPropertyTypeString, _
. txtXl sLocati on. Text
Else
ObjPrOpLocation – txtXlsLocation.Text
End If
Set objPropRate – objDocProps(‘TravelCostRatePe,·Mile’)
Else
ObjPropRate – txtMileageRate.Text
End If
dlgTravelCostSettings.Hide
Unload dlgTravelCostSettings
End If
Set objDocProps – Nothlng
Set objPropLocation a Nothing
Set objPropRat~ – Nothing
End Sub

You also want the form to see whether settings have already been saved for these properties. If the settings have been saved, then they should be displayed when the form opens. The event in this case would be the startup or Initialize event of the UserForm itself. For this procedure, you will need to use the drop-down lists at the top of the Code window. Select UserForm for the Object (left) drop-down list. Then select Initialize from the Event (right) drop-down list (see Figure 25.4). Here’s the procedure:

Selecting the Initializing

Selecting the Initializing

Test the procedure to ensure that it works. Open the Properties of the Project document from the File menu of Project 2000 (File > Properties) to make sure that the document properties were saved

option

option

Some basic error-handling has been added in these procedures, but”it is far from thorough. See the VBA Help file for more information on error-handling.

Opening the UserForm

We still need one more procedure-a global procedure to open the UserForm. This procedure will allow you to assign the’ form to a command bar button. In the Project Explorer, activate Module! and create the following code to display the UserForm:

Posted on November 26, 2015 in Advance Programming In Project 2000

Share the Story

Back to Top