Form Navigation and Flow


You can control the order and manner in which forms can be opened within your Access applications in various ways. For example, you can design switchboard forms that allow the user to select which form to open. Another option is to open a main form and allow a user to open separate forms from the main form. The style of navigation you choose depends on the type of system and the manner in which users will interact with the various forms.

During the design phase, you should consider the flow of forms and how users will navigate from one form to the next. It is very simple to control how forms are displayed. The following is an example that illustrates how to open, hide, minimize, and close forms.

  'open a form DoCmd.OpenForm "FormName" 'close a form DoCmd.Close acForm, "FormName" 'minimize the current form DoCmd.Minimize 'hide a form FormName.Visible = False 'make the form visible again FormName.Visible = True 

A well-designed application allows a user to interact with the application without having to use Access to open the form manually from the database window. An easy way to have a form open automatically is to use the AutoExec macro to specify which form should open automatically when the application opens.

Try It Out-Building a Switchboard Form

image from book

In this example, you will build a simple switchboard form that opens automatically when the application opens. The switchboard form enables the user to open multiple forms from a menu of choices.

  1. Create a new database by selecting the Office Button image from book New image from book Blank Database. Specify Ch6CodeExamples for the file name and click the Create button.

  2. Add four new forms to the application. Name the forms frmMain, frmAccounts, frmPayments, and frmRegister, respectively.

  3. Open frmMain in design view and add three command buttons. Name the command buttons cmdAccounts, cmdPayments, and cmdRegister. Set the caption of the command buttons to View/Manage Accounts, Make Payments, and View/Manage Register, respectively. Also, set the Record Selectors, Navigation Buttons, and Dividing Lines properties of the form to No. An example of the form at this point is shown in Figure 6-1.

    image from book
    Figure 6-1

  4. Select the cmdAccounts command button and add the following code to the Click event.

      Private Sub cmdAccounts_Click() 'open the Accounts form DoCmd.OpenForm "frmAccounts" 'hide the main switchboard form Forms.Item("frmMain").Visible = False End Sub 

  1. Select the cmdPayments command button and add the following code to the Click event:

      Private Sub cmdPayments_Click() 'open the Accounts form DoCmd.OpenForm "frmPayments" 'hide the main switchboard form Forms.Item("frmMain").Visible = False End Sub 

  2. Select the cmdRegister command button and add the following code to the Click event:

      Private Sub cmdRegister_Click() 'open the Accounts form DoCmd.OpenForm "frmRegister" 'hide the main switchboard form Forms.Item("frmMain").Visible = False End Sub 

  1. Open the frmAccounts form. Add a command button to the form. Change the Name property of the command button to cmdMenu and change the Caption property to Menu. Add a label to the form and type View/Manage Accounts in the label. The form should be similar to that shown in Figure 6-2.

    image from book
    Figure 6-2

  2. Select the cmdMenu command button and add the following code to the Click event:

      Private Sub cmdMenu_Click() 'display the switchboard main form Forms.Item("frmMain").Visible = True 'close the current form DoCmd.Close acForm, "frmAccounts" End Sub 

  3. Open the frmPayments form. Add a command button to the form. Change the Name property of the command button to cmdMenu and change the Caption property to Menu. Add a label to the form and set the caption to Make Payment.

  4. Select the cmdMenu command button and add the following code to the Click event:

      Private Sub cmdMenu_Click() 'display the switchboard main form Forms.Item("frmMain").Visible = True 'close the current form DoCmd.Close acForm, "frmPayments" End Sub 

  1. Open the frmRegister form. Add a command button to the form. Change the Name property of the command button to cmdMenu and change the Caption property to Menu. Add a label to the form and type Check Register in the label.

  2. Select the cmdMenu command button and add the following code to the Click event:

      Private Sub cmdMenu_Click() 'display the switchboard main form Forms.Item("frmMain").Visible = True 'close the current form DoCmd.Close acForm, "frmRegister" End Sub 

  3. From the database window, create a new macro named AutoExec that contains the OpenForm command for frmMain, as shown in Figure 6-3.

    image from book
    Figure 6-3

  4. Save all changes to the forms and VBA code.

  5. Close the entire database by selecting Office Button image from book Close Database, and then reopen the same database. A screen like the one shown in Figure 6-4 should load automatically.

    image from book
    Figure 6-4

  6. Select the View/Manage Accounts button, and a screen like the one shown in Figure 6-5 should open.

    image from book
    Figure 6-5

How It Works

In this example, you created a switchboard form with three command buttons. Under each command, you added code to open the respective form, using the OpenForm method, and then you hid the switchboard form by setting its Visible property to False.

 Private Sub cmdAccounts_Click() 'open the Accounts form DoCmd.OpenForm "frmAccounts" 'hide the main switchboard form Forms.Item("frmMain").Visible = False End Sub

You also created the three forms that can be opened from the switchboard. On each of those forms, a command button was added to allow the user to return to the switchboard menu. Code was then added to the Click event of each menu button to unhide the switchboard form by setting the Visible property to True, and to close the current form by using the Close method of the DoCmd object:

 Private Sub cmdMenu_Click() 'display the switchboard main form Forms.Item("frmMain").Visible = True 'close the current form DoCmd.Close acForm, "frmAccounts" End Sub

You then added an AutoExec macro to cause the switchboard form to open when the database opens. When the database opens, Access automatically runs any commands in the macro named AutoExec.

From the switchboard, any one of the three other forms can be opened. You can then return to the switchboard to select a different form by using the Menu button on the respective form. Although this is a simple example, you can apply the concepts to any application for navigating among forms.

image from book




Beginning Access 2007 VBA
Beginning Access 2007 VBA
ISBN: 0470046848
EAN: 2147483647
Year: 2004
Pages: 143

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