We saw that the Windows controls on a form could be used to display data from a table. This is done by specifying a list of values in the RecordSource property of the form. To get the set of records that the RecordSource property of a form holds, you can access its RecordsetClone property.
The Record Selector is a vertical bar on the left side of a form. The presence or absence of the record selector is controlled by the Boolean Record Selectors property field. To programmatically control the presence or absence of the record selector on a form, assign a value of True (to display it) or False (to hide it) to the RecordSelector property of the form. Here is an example: Private Sub cmdManipulate_Click() Me.RecordSelectors = False End Sub
By default, a form is equipped with buttons that allow the user to navigate back and forth between records. The presence or absence of navigation buttons is controlled by the Boolean Navigation Buttons property. To programmatically control the presence or absence of the navigation buttons, access the form's NavigationButtons property and assign the desired Boolean value. Here is an example: Private Sub cmdManipulate_Click() Me.NavigationButtons = False End Sub
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, if the database is configured to display overlapped windows, you can click its title bar or any section of its body. If a form is closed, to manually select it, in the Navigation Pane, click the form to select it. The name of the form would become highlighted, indicating that it is selected. Here is an example where a form named Exercise and that is selected in the Navigation Pane:
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 Navigation Pane, then pass the third argument as True. Here is an example that selects a form named Teachers to highlight it in the Navigation Pane: Private Sub cmdSelect_Click()
DoCmd.SelectObject acForm, "Teachers", True
End Sub
If the form is already displaying, it may be in the background. If there is no form by the name you specified in the second argument, you would receive a 2544 error:
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. If you create a new form, it automatically receives a temporary name but you must save it to have an actual name for the form. Once a form exists, if you want, you can change its name if you judge this necessary. In order to rename a form, it must be closed. To programmatically rename a form, you can call the Rename() method of the DoCmd object. The syntax to use is: DoCmd.Rename(NewName, ObjectType, OldName) The first argument is the new name that the form will have. Normally, only the first argument is required. If you call this method using only the first argument and if you call this argument from a form, the form you are using would be renamed. But, based on the rules of the Microsoft Windows operating system, you cannot rename a form that is currently opened. Consequently, if you call this method from a form and provide only the first argument, you would receive a 2009 error:
This means that, you should always pass the first argument. The second argument is a member of the AcObjectType enumeration. For a form, this member must be acForm. The third argument is the name of the existing form that you want to rename. Here is an example: Private Sub cmdRename_Click() DoCmd.Rename "Children", AcObjectType.acForm, "Pupils" End Sub Instead of renaming a form, you can make a copy of it and keep the original. To visually copy an existing form, in the Navigation Pane, right-click the form and click Copy. Then right-click an area of the Navigation Pane and click Paste. This would open the Paste Table As dialog box in which you can enter the new name of the copied object. To programmatically copy a form, you can call the CopyObject() method of the DoCmd object using the following syntax: DoCmd.CopyObject [destinationdatabase][, newname], ObjectType, sourceobjectname] The destinationdatabase argument is the name or path of the database where the copied form would be sent to. If you are copying the form in the same database, you can omit this argument. The newname argument is the name that you want the new form to hold. The third argument must be acForm. The last argument is the name of the existing 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 many types of views. One of them in the Form View. To programmatically open a form, you can use the DoCmd object that provides the OpenForm() method. Its syntax is: DoCmd.OpenForm FormName[, view][, datamode] The first argument of this method is the name of the form that you want to open. This second argument is optional. Here is an example: Private Sub cmdOpenForm_Click() DoCmd.OpenForm "Employees" End Sub If you want to use it, the second argument is a member of the acFormView enumeration. Its value can be one of the following:
If you omit the second argument, the acNormal option applies. Here is an example: Private Sub cmdOpenForm_Click() DoCmd.OpenForm "Employees", AcFormView.acNormal End Sub When this code executes, a form named Employees would be opened in the 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, in the Controls section of the Ribbon, click the More button and make sure the Use Control Wizards option is down . Then, click the Button and click the form. The wizard would start and you can select the Open Form option after selecting Form Operations. When you open a form, it fires the Open event. This event runs before the form actually displays. The structure of this event is: Private Sub Form_Open(Cancel As Integer) End Sub This event takes one argument. Because this event is fired as the form is about to be opened but before it actually does, the Cancel argument allows you at the last minute to proceed or to stop the process. If you set this argument to False, the form would not be opened.
When you open a form using the OpenForm method of the DoCmd argument, the form is equipped with an argument named OpenArgs, which is of type Variant. The OpenArgs argument is typically used to carry a string from the object or action that is opening the form, to the form that is being opened. Form maintenance includes deleting, copying, or renaming a form, etc. Most of these operations require that the form be closed. To manually delete a form, in the Navigation Pane, right-click it and click Delete. To programmatically delete a form, call the DeleteObject() method of the DoCmd object. Its syntax is: DoCmd.DeleteObject ObjectType, [objectname] The ObjectType argument is a member of the AcObjectType enumeration. For a form, it would be acForm. Since the second argument is optional, you can call this method as follows: Private Sub cmdDelete_Click() DoCmd.DeleteObject AcObjectType.acForm End Sub You can omit AcObjectType. When this method is called, the form that is selected in the Navigation Pane would be deleted. As mentioned already, the second argument is optional. If you want to use it, pass the name of the form to be deleted. Here is an example: Private Sub cmdDelete_Click() DoCmd.DeleteObject AcObjectType.acForm, "Customers" End Sub When this code executes, a form named Customers would be deleted. When you call this method and pass the second argument, if no form is selected in the Navigation Pane and if you omit the second argument, you would receive a 2493 error stating that the action requires a form name:
If you decide to pass the second argument but provide a name for a form that cannot be found in the Navigation Pane, you would receive a 7874 error:
This means that, either you select a form prior to calling this method, or you pass the name of the form to delete as the second argument.
To open a form and display it to the user, the computer must allocate memory for it. This is referred to as loading the form into memory. When this happens, the form fires the Load event. The structure of this event is: Private Sub Form_Load() End Sub This event simply allows you to know that the form as been presented to the user so you can take the appropriate action. After using a form, you can close it. As the form is being closed, it must empty the memory it was using. At this time, the form fires an Unload event. Its structure is: Private Sub Form_Unload(Cancel As Integer) End Sub This event takes one argument: Cancel. If you don't want the form to be closed, set the Cancel argument to False.
The Microsoft Windows operating system provides the ability to work on many objects and many windows at the same time. In Microsoft Access, you can display as many forms as you want (or as your computer memory can afford). When many forms display, only one can geometrically be in the front. To bring another form to the front, you can click it, usually its title bar. When a form is brought to the front, it is said to be activated. When this happens, the form fires the Activate event. The structure of this event is: Private Sub Form_Activate() End Sub As you can see, this event does not take any argument. It simply allows you to know that the form has been brought to the front and you can do what you judge necessary. As mentioned already, you can display different forms on the screen and switch to the one you need at one particular time. When you switch from one from to another, the form that goes in the background fires the Deactivate event. Its structure is: Private Sub Form_Deactivate() End Sub This event takes no argument.
If you create a form that is used to present records to a user, you may also provide the ability to navigate among records. When doing this, whenever the form displays a record, it fires the Current event. The structure of this event is: Private Sub Form_Current() End Sub This event only signals that you have just gotten to a record.
After using a form, you can close it if it 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 close button . 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 ObjectType, [objectname], [save] The first argument is a member of the AcObjectType enumeration. For a form, the member to use is called acForm: DoCmd.Close AcObjectType.acForm, [objectname], [save] Of course, you can omit AcObjectType and use the following syntax: 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, "Employees" End Sub In this example, a form named Employees is asked to be closed. If you suspect that the form would need to be saved before formally being closed, you can pass a third argument that is a member of the AcCloseSave enumeration. The available values are:
When calling the Close() method to close a form, if the form is not opened or if the specified form does not exist, nothing would happen (you would not receive an error). Instead of writing your own code, to let Microsoft Visual Basic write it for you, you can use the Command Button Wizard. As a form is being closed, it fires a Close event. The structure of this event is: Private Sub Form_Close() End Sub
Sometimes, you will open a form from another form. Some other times, you will use intermediary actions to create new values of a form. When the values in the controls of a form have been changed, the form does not automatically update the controls. To take care of this, you can refresh the form. To do this programmatically, you can call the Refresh() method of the Form class. This could be done as follows: Private Sub cmdRefresh_Click() Refresh End Sub
You may have specified the record source of a form. When the records in that source changes, the form does not automatically update itself. To take care of this, you can call the Requery() method of the Form class. This could be done as follows: Private Sub cmdQuery_Click() Requery End Sub |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|