Working with the Tag Property

 < Day Day Up > 

Working with the Tag Property

Beginning VBA developers are sometimes confused by the Tag property, wondering what good it is to have a property that Access never uses. The answer is that it's very useful indeed to have a property that you can use yourself for whatever you want. Every control on an Access form has a Tag property, and it's up to you to decide what to put there and how to use it.

For an example of how to use the Tag property, let's modify the HighlightControl procedure that you saw earlier in the chapter. As it stands now, this procedure sets controls to a yellow background when you enter them. But what if you don't always want to use that color?

There's no built-in property for Access controls that says "use this color to highlight the control." Fortunately, there is the Tag property. Here's how to make the changes:

  1. Open the Timeslips form in Design mode. Set the Tag property of the EmployeeID and TaskID controls to 65535. Set the Tag property of the DateWorked and Hours controls to 16777088.

  2. Open the Chapter13 code module and modify the HighlightControl procedure as follows:

     

     Sub HighlightControl(ctl As Control)   ' Set the background color of the   ' specified control to yellow, if possible   On Error Resume Next   If Not IsNull(ctl.Tag) Then     ctl.BackColor = ctl.Tag   Else     ctl.BackColor = 65535   End If End Sub 

Save the form. Switch the form back to Form view and tab through the controls. You'll see that the first two controls turn yellow when they have the focus, which corresponds to a BackColor value of 65535. The second two controls turn a light blue, color 16777088.

CAUTION

Don't confuse the Tag property with the SmartTags property.


Note that the procedure has been written to do something useful (We hope!) whether or not there's a value in the Tag property. If the tag is filled in, the procedure uses that number for the highlighted background color. If there's no tag value, it falls back to the original yellow that was coded in the first version of HighlightControl.

CASE STUDY: Creating a Master Viewing Form

This chapter's case study explores an alternative user interface for displaying client, employee, and project information. The idea is to give users a single central form that they can toggle between these three types of information. You also add control highlighting to aid in data entry.

To begin with, you need to build three new forms to act as subforms on the main form. These forms, ClientSub, EmployeeSub, and ProjectSub, are each two inches high and three inches wide. The Tag property of each text box on these forms is set to 65535, and the Tag property of the combo box on the ProjectSub form is set to 12615935 to remind users that it draws its data from another table. Figure 13.4 shows these three forms open in Design mode.

Figure 13.4. Subforms for the alternative user interface.

graphics/13fig04.jpg


Of course, each of the subforms has the code necessary to handle control highlighting. Here's the code from the ClientSub form:

 

 Option Compare Database Option Explicit Private Sub Address_GotFocus()   HighlightControl Address End Sub Private Sub Address_LostFocus()   UnhighlightControl Address End Sub Private Sub City_GotFocus()   HighlightControl City End Sub Private Sub City_LostFocus()   UnhighlightControl City End Sub Private Sub Client_GotFocus()   HighlightControl Client End Sub Private Sub Client_LostFocus()   UnhighlightControl Client End Sub Private Sub Contact_GotFocus()   HighlightControl Contact End Sub Private Sub Contact_LostFocus()   UnhighlightControl Contact End Sub Private Sub Phone_GotFocus()   HighlightControl Phone End Sub Private Sub Phone_LostFocus()   UnhighlightControl Phone End Sub Private Sub State_GotFocus()   HighlightControl State End Sub Private Sub State_LostFocus()   UnhighlightControl State End Sub Private Sub Zip_GotFocus()   HighlightControl Zip End Sub Private Sub Zip_LostFocus()   UnhighlightControl Zip End Sub 

Here's the corresponding code for the EmployeeSub form:

 

 Option Compare Database Option Explicit Private Sub EmployeeID_GotFocus()   HighlightControl EmployeeID End Sub Private Sub EmployeeID_LostFocus()   UnhighlightControl EmployeeID End Sub Private Sub FirstName_GotFocus()   HighlightControl FirstName End Sub Private Sub FirstName_LostFocus()   UnhighlightControl FirstName End Sub Private Sub LastName_GotFocus()   HighlightControl LastName End Sub Private Sub LastName_LostFocus()   UnhighlightControl LastName End Sub 

And finally, the code for the ProjectSub form:

 

 Option Compare Database Option Explicit Private Sub ClientID_GotFocus()   HighlightControl ClientID End Sub Private Sub ClientID_LostFocus()   UnhighlightControl ClientID End Sub Private Sub EstimatedEndDate_GotFocus()   HighlightControl EstimatedEndDate End Sub Private Sub EstimatedEndDate_LostFocus()   UnhighlightControl EstimatedEndDate End Sub Private Sub ProjectName_GotFocus()   HighlightControl ProjectName End Sub Private Sub ProjectName_LostFocus()   UnhighlightControl ProjectName End Sub Private Sub StartDate_GotFocus()   HighlightControl StartDate End Sub Private Sub StartDate_LostFocus()   UnhighlightControl StartDate End Sub 

The next step is to build the master form. This is an unbound form that hosts the three subforms. Follow these steps to create this form:

  1. Create a new form in Design mode.

  2. Add an option group to the form and name it grpSub. Set the default value of grpSub to 1.

  3. Add three option buttons to the option group. Set their captions to Clients, Employees, and Projects, and set their Option Value properties to 1, 2, and 3.

  4. Drag the ClientSub form from the Database window and drop it on the master form. This is the easiest way to get a subform control sized properly to hold the form.

  5. Delete the label that Access creates for the subform.

  6. Name the subform control SwitchForm. Figure 13.5 shows the master form in Design view.

    Figure 13.5. Designing the master form.

    graphics/13fig05.jpg


  7. Add code behind the form to handle switching the contents of the subform:

     

     Private Sub grpSub_AfterUpdate()   ' Switch subforms as buttons as clicked   Select Case grpSub.Value     Case 1  ' Clients       Me!SwitchForm.SourceObject = "ClientSub"     Case 2  ' Employees       Me!SwitchForm.SourceObject = "EmployeeSub"     Case 3  ' Projects       Me!SwitchForm.SourceObject = "ProjectSub"   End Select End Sub 

Save the form and open it in regular Form view. You'll find that you can use the toggle buttons to switch the embedded subform, and that highlighting follows the cursor through the data-entry fields. Figure 13.6 shows the final form.

Figure 13.6. The master form in action.

graphics/13fig06.jpg



     < Day Day Up > 


    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186

    Similar book on Amazon

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