Access Cookbook - Ken Getz [195]
' form painting while expanding the form. This
' prevents the form from flashing.
' If the form is in expanded state, however, Access
' won't hide the expanded portion unless form
' painting is left on.
If Not blnExpanded Then Me.Painting = False
' Expand the form if currently unexpanded, and vice versa.
sct.Visible = Not blnExpanded
' Size to fit the form to expand or contract the form's
' borders to match the visibility of the section.
DoCmd.RunCommand acCmdSizeToFitForm
' Change the button caption and repaint if necessary.
If Not blnExpanded Then
Me.cmdExpand.Caption = "Basic <<"
Me.Painting = True
Me(acbFirstAdvancedCtl).SetFocus
Else
Me.cmdExpand.Caption = "Advanced >>"
Me(acbFirstBasicCtl).SetFocus
End If
End Sub
Change the constant declarations so that acbcFirstBasicCtl is the name of the first control in the detail section of the form and acbcFirstAdvancedCtl is the name of the first control in the footer section of the form.
Save and close the form. The final form should look like the one shown in design view in Figure 9-24.
Figure 9-24. The frmExpandingDialog form in design view
To demonstrate this new functionality, load the sample database 09-08.MDB and open frmExpandingDialog in form view. The dialog form will display in its initial, contracted state (see Figure 9-25). Click on the Advanced button and the form will expand to reveal additional text boxes (see Figure 9-26). Click again on the button (now labeled Basic) to return to the contracted state. (This sample form is for demonstration purposes only; it doesn't do anything with the data you enter into it.)
Figure 9-25. The frmExpandingDialog form in its contracted state
Figure 9-26. The frmExpandingDialog form in its expanded state
Discussion
Because you set the Visible property of the form footer section to No, the footer does not appear when the form is first opened. In addition, because you set the AutoResize property to Yes, Access resizes the form to show only the visible areas of the form.
Expansion of the form is handled by the code attached to the cmdExpand button's Click event. This event procedure begins by defining a few constants and variables. The two constants will be used later in the function to shift the focus to the first control of each section:
Dim sct As Section
Dim blnExpanded As Boolean
Const acbFirstBasicCtl = "txtFirstName"
Const acbFirstAdvancedCtl = "txtOldPW"
Next, the procedure sets the section variable to point to the form's footer section, using the built-in acFooter constant. In addition, it stores the current state of the Visible property of the section in the Boolean variable blnExpanded:
Set sct = Me.Section(acFooter)
' Keep track of the state of the form when first called.
blnExpanded = sct.Visible
If the form is currently contracted, it needs to be expanded, and vice versa. But before this is done, the code sets the form's Painting property to False if (and only if) the form is being expanded. The technique will work without performing this step, but the form will flash as it expands. On the other hand, if the form is being contracted, you shouldn't turn off Painting; if you do, the form will not properly repaint itself and the nonfunctional advanced section will remain painted on the screen. This step is accomplished with a single line of code and six lines of comments:
' If the form is in nonexpanded state, turn off
' form painting while expanding the form. This
' prevents the form from flashing.
' If the form is in expanded state, however, Access
' won't hide the expanded portion unless form
' painting is left on.
If Not blnExpanded Then Me.Painting = False
The form is then expanded or contracted by using Not to toggle the footer section's Visible property to the opposite of its current state:
' Expand form if currently unexpanded and vice versa.
sct.Visible = Not blnExpanded
The code then resizes the form using the RunCommand method of the DoCmd object to carry out the Window → Size to Fit Form menu command: