ACC: How to Use a Toggle Button to Hide and Display a Subform

Last reviewed: October 24, 1997
Article ID: Q89622
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

Novice: Requires knowledge of the user interface on single-user computers.

You can hide and show a subform by using a command button or toggle button. This article describes two methods to use a command button or toggle button to hide and show the Quarterly Orders subform on the Quarterly Orders form in the sample database Northwind.mdb (or Nwind.mdb in Microsoft Access versions 1.x and 2.0).

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications (used in Microsoft Access 97 and Microsoft Access for Windows 95 version 7.0) is called Access Basic in earlier versions. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in version 2.0.

NOTE: This article explains a technique demonstrated in the sample files, FrmSampl.exe (for Microsoft Access for Windows 95 version 7.0) and FrmSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q150895
   TITLE     : ACC95: Microsoft Access Sample Forms Available on MSL

   ARTICLE-ID: Q175066
   TITLE     : ACC97: Microsoft Access 97 Sample Forms Available on MSL

MORE INFORMATION

The following steps demonstrate how to use a command button or toggle button to hide and show a form.

Using a Macro

  1. Open the sample database Northwind.mdb (or Nwind.mdb).

  2. Create the following new macro, and then save it as Toggle Macro:

          Macro Name      Action
          ------------------------
          Toggle Macro    SetValue
    
          Toggle Macro Actions
          ------------------------------------------------------
          SetValue
             Item: [Quarterly Orders Subform].Visible
             Expression: Not [Quarterly Orders Subform].Visible
    
    

  3. Open the Quarterly Orders form in Design view.

  4. Set the form's AllowEdits property to Yes (or the form's DefaultEditing property to Allow Edits in versions 1.x and 2.0).

  5. Add a command button or toggle button to any open area on the form.

  6. Set the following properties for the command button:

          Caption: Toggle Subform
          OnClick (or AfterUpdate in version 1.x): Toggle Macro
    

  7. View the form in Form view. Note that when you click the toggle button, the Quarterly Orders subform is hidden or shown.

Using Code Behind Forms

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

  1. Open the sample database Northwind.mdb (or Nwind.mdb).

  2. Open the Quarterly Orders form in Design view.

  3. Set the form's AllowEdits property to Yes (or the form's DefaultEditing property to Allow Edits in versions 1.x and 2.0).

  4. Add a command button or toggle button to any open area on the form.

  5. Set the following properties for the command button:

          Caption: Toggle Subform
          OnClick (or AfterUpdate in version 1.x): [Event Procedure]
    

          Sub Button2_Click()
             Me![Quarterly Orders Subform].Visible = _
             Not Me![Quarterly Orders Subform].Visible
          End Sub
    
    

  6. View the form in Form view. Note that when you click the toggle button, the Quarterly Orders subform is hidden or shown.


Additional query words: togglebutton
Keywords : kbusage FmsHowTo FmsSubf
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.