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:
You can also select a form by pressing the up or the down arrow key continuously until the desired form is selected.
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.
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:
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:
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.
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 or . 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.
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:
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-2015, FunctionX||Next|