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. 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: Create a new form in Design mode.
Add an option group to the form and name it grpSub. Set the default value of grpSub to 1.
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.
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.
Delete the label that Access creates for the subform.
Name the subform control SwitchForm. Figure 13.5 shows the master form in Design view.
Figure 13.5. Designing the master form.
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.
|