Home

The Forms of a Database Application: Using a Form

 

Selecting a Form

Some operations to perform on a form require that you select it first. If a form is opened on the screen, it may not have focus or it may not be activated. To select a form, you can click its title bar or any section of its body. If a form is closed, to manually select it, in the Database window, click the Forms button to activate its section. Then, click the name of the form. The name of the form would become highlighted, indicating that it is selected. Here is an example where a form named CD@Home is selected:

Selecting a Table in the Database Window

You can also select a form by pressing the up or the down arrow key continuously until the desired form is selected.

To programmatically select a form, use the DoCmd object that is equipped with the SelectObject() method. The syntax to use would be:

DoCmd.SelectObject acForm, [objectname][, indatabasewindow]

The first argument must be acForm to indicate that you are select a form. The second argument is the name of the form to select. If you only want to highlight the form in the Database window, then pass the third argument as True.

Here is an example that selects a form named Teachers and highlights it in the Database window:

Private Sub cmdSelect_Click()
    DoCmd.SelectObject acForm, "Teachers", True
End Sub

If the form is already displaying, it may be in the background. If you omit the third argument or pass it as False, the form would be displayed in the foreground. If the form is not opened and you omit the third argument or pass it as False, you would receive a 2489 error:

You can use a conditional statement and error handling to make sure the user doesn't see this dialog box.

 

Opening a Form

Before using a form or performing an update in it, in most cases, you probably would need to open it first but this may depend on what you want to do at the time. This is because a form offers two main views. To simply and manually open a form, in the Forms section of the Database window, you can double-click. You can also right-click a form and click Open. This would open a form in Form View. Here is an example:

The second view of the form is used to design or modify the way a form looks. This is referred to as Design View. To open a form in Design View, in the Forms section of the Database window, you can right-click the desired form and and click Design View. If the form is already selected, on the toolbar of the Database window, you can click the Design button:

To programmatically open a form, you can use the DoCmd object that provides the OpenForm() method. Its syntax is:

DoCmd.OpenForm tablename[, view][, datamode]

The first argument of this method is the name of the table that you want to open.

The second argument is a constant value as follows:

View Name Result
acDesign The form will display in Design View
acFormDS The form will display like a table
acFormPivotChart The form will display as a pivot chart
acFormPivotTable The form will display as a pivot table
acNormal The form will display in Form View
acPreview The form will display in Print Preview

This second argument is optional. If you omit it, the acNormal option applies. Here is an example:

Private Sub cmdOpenForm_Click()
    DoCmd.OpenForm "Teachers", acNormal
End Sub

When this  code executes, a form named Teachers would be opened in Form View.

Instead of writing the code to open a form, you can use the Command Button Wizard that can do this for you. To do this, while the form is opened in Design View, make sure the Control Wizards button is down. Then, click the Command Button and click the form. The wizard would start and you can select the Open Form option after selecting Form Operations.

 

Practical Learning Practical Learning: Opening a Form

  1. To open the Central form, in the Database window, click Forms, then right-click Central and click Design View
  2. To automate the opening of a form, on the Toolbox, make sure the Control Wizards button is pushed , click Command Button and click the top-left section of the form
  3. In the Categories list of the first page of the Command Button Wizard, click Form Operations
  4. In the Actions list, click Open Form
     
  5. Click Next
  6. In the second page of the wizard, click CleaningOrders and click Next
  7. In the third page of the wizard, click the text box and change its string to Cleaning Orders
     
  8. Click Next
  9. Change the name of the button to cmdCleaningOrders and click Finish
  10. To view the code that was written, right-click the button and click Build Event...
     
    Private Sub cmdCleaningOrders_Click()
    On Error GoTo Err_cmdCleaningOrders_Click
    
        Dim stDocName As String
        Dim stLinkCriteria As String
    
        stDocName = "CleaningOrders"
        DoCmd.OpenForm stDocName, , , stLinkCriteria
    
    Exit_cmdCleaningOrders_Click:
        Exit Sub
    
    Err_cmdCleaningOrders_Click:
        MsgBox Err.Description
        Resume Exit_cmdCleaningOrders_Click
        
    End Sub
  11. To return to Microsoft Access, on the Standard toolbar, click View Microsoft Access

Closing a Form

After using a form, you can close it if is (still) opened. If there is a structural change that needs to be saved, Microsoft Access would prompt you.

To manually close a form, you can click its system Close button Close or Close (Windows XP). You can also click File -> Close on the main menu if the form is active. You can also double-click its System button on the left side of its title bar. You can also press Ctrl + F4.

To programmatically close a form, you can call the Close() method of the DoCmd object. Its syntax is:

DoCmd.Close acForm, [objectname], [save]

The first argument must be specified as acForm to indicate that you want to close a form. If you are closing the same form that is calling this method, this is the only argument you would need. Consider the following example:

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub

In this case, the form would be closed.

The second argument can be the name of the form you want to close. This argument is useful if you are trying to close a form other than the one that is making the call. Here is an example:

Private Sub cmdClose_Click()
    DoCmd.Close acForm, "Teachers"
End Sub

In this example, a form named is asked to be closed.

If you suspect that the form would need to be saved before formally being closed, you can pass the third argument with one of the following values:

View Name Result
acSaveNo The form doesn't need to be saved
acSavePrompt Prompt the user to save the changes. This is the default
acSaveYes Save the form without having to prompt the user

Instead of writing your own code, to let Microsoft Visual Basic write it for you, you can use the Command Button Wizard.

 

Previous Copyright © 2005-2016, FunctionX Next