DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 9.8 Create an Expanding Dialog

9.8.1 Problem

You have a dialog with a lot of options, most of which are needed only in specific situations. You'd like to create this form as an expanding dialog, similar to forms that have an Advanced button revealing more options. How can you do this with your own form?

9.8.2 Technique

You can make a hidden section of the form become visible at runtime, and use the Window | Size to Fit Form command to force the form to expand to fit its new dimensions. This solution shows you how to create this type of form using an expanding form footer. You'll also learn how to minimize screen flashing while resizing the form by manipulating the form's Painting property.

Follow these steps to create your own expanding dialog form:

  1. Create a new form. To make the form look like a dialog, set the properties of the form as shown in Table 9-6. Some of these property settings are optional, since the expanding technique will work with non-dialog forms too. The settings for the DefaultView and AutoResize properties are required.

Table 9-6. Property settings for a dialog form

Property

Value

DefaultView

Single Form

ScrollBars

Neither

RecordSelectors

No

NavigationButtons

No

AutoResize

Yes

AutoCenter

Yes

PopUp

Yes

Modal

Yes

BorderStyle

Dialog

MinMaxButtons

None

  1. Select View Form Header/Footer to add a footer section to the form. Set the Visible property of the footer section to No. Because you're interested in only the footer section, you may wish to grab the bar separating the detail and header sections and drag it up so the header section has a height of zero.

  2. Partition the controls on your form into two groups: those you wish to display at all times, and those you wish to display only when the form is in the advanced (expanded) state. Place the first set of controls in the form's detail section; place the second set of controls in the footer section.

  3. Add a button named cmdExpand with the caption "Advanced >>" to the detail section of the form. Create an event procedure attached to the Click event of the button. (If you're unsure of how to do this, see How Do I Create an Event Procedure? in the the preface of this book.) Add the following code to the event procedure (or copy the code from the frmExpandingDialog form's module in 09-08.MDB):

    Private Sub cmdExpand_Click( )
    
        Dim sct As Section
        Dim blnExpanded As Boolean
        
        Const acbFirstBasicCtl = "txtFirstName"
        Const acbFirstAdvancedCtl = "txtOldPW"
        
        Set sct = Me.Section(acFooter)
    
        ' Keep track of the state of the form when first called.
        blnExpanded = sct.Visible
        
        ' 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
    
        ' 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.

  4. 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
figs/acb2_0924.gif

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
figs/acb2_0925.gif
Figure 9-26. The frmExpandingDialog form in its expanded state
figs/acb2_0926.gif

9.8.3 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:

' Size to fit the form to expand or contract the form's
' borders to match the visibility of the section.
    DoCmd.RunCommand acCmdSizeToFitForm

The function then changes the caption of the button, turns painting back on if it was turned off, and finally moves the focus to the first control of the appropriate section. This last step is not absolutely necessary, but it's a nice touch because the normal tab sequence will not jump across sections. The relevant code is:

' Change the button caption and repaint if necessary.
If Not blnExpanded Then
    Me.cmdExpand.Caption = "Basic <<"
    Me.Painting = True
    Me(acbcFirstAdvancedCtl).SetFocus
Else
    Me.cmdExpand.Caption = "Advanced >>"
    Me(acbcFirstBasicCtl).SetFocus
End If

You can also apply this technique to non-dialog and bound forms. Although it's not commonly done, there's nothing to stop you from placing bound controls in the footer section of a form. On the other hand, it may be more appropriate to use a tabbed form for bound forms. See the Solution in Recipe 2.5 for more details.

    [ Team LiB ] Previous Section Next Section