Chapter 6. Managing Data

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

     

  2. Select View figs/u2192.gif 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.

  3. 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.

  4. 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 Section P.5.5 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 fExpanded As Boolean          Const acbFirstBasicCtl = "txtFirstName"     Const acbFirstAdvancedCtl = "txtOldPW"          Set sct = Me.Section(acFooter)     ' Keep track of the state of the form when first called.     fExpanded = 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 fExpanded Then Me.Painting = False     ' Expand the form if currently unexpanded, and vice versa.     sct.Visible = Not fExpanded     ' Size to fit the form to expand or contract the form's     ' borders to match the visibility of the section.     DoCmd.DoMenuItem acFormBar, 7, 6, 0, acMenuVer70          ' Change the button caption and repaint if necessary.     If Not fExpanded 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.

  5. 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/acb_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/acb_0925.gif

Figure 9-26. The frmExpandingDialog form in its expanded state

figs/acb_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 fExpanded 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 fExpanded:

Set sct = Me.Section(acFooter) ' Keep track of the state of the form when first called. fExpanded = 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 fExpanded 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 fExpanded

The code then resizes the form using the DoMenuItem method of the DoCmd object to carry out the Window figs/u2192.gif Size to Fit Form menu command, as there's no equivalent Access action or method:

' Size to fit the form to expand or contract the form's ' borders to match the visiblilty of the section. DoCmd.DoMenuItem acFormBar, 7, 6, 0, acMenuVer70

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 fExpanded 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 Section 2.5.2 for more details.



Access Cookbook
Access Data Analysis Cookbook (Cookbooks)
ISBN: 0596101228
EAN: 2147483647
Year: 2005
Pages: 174

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