The Forms of a Database Application
Introduction to Form Creation
A visual application such as a computer database usually starts with a rectangular object that hosts other controls. Such an object is a form. In Microsoft Access, you can create a form that stands on its own like the forms of other programming environments, or you can create a form that is tied to a list. This means that the process of creating a form may depend on how you proceed.
To manually create a new form, on the main menu of Microsoft Access, you can click Insert -> Form. This would display the New Form dialog box where you can click Design View and click Ok. If you are already in the Forms section of the Database window, you can double-click the Create Form In Design View link or button. This would directly start the new form in Design View.
To programmatically create a form, call the CreateForm() method of the Application object. The syntax of this method is:
The first argument to this method is the name of the database that will receive the form. If the form will be added to the current database, you can omit this argument. The second argument is the name of an existing form that you want to use as template. If you specify this argument, you must make sure that you provide the name of a form. Otherwise, you can omit it.
Here is an example:
Private Sub cmdCreateForm_Click() Dim frmEmployees As Form Set frmEmployees = CreateForm End Sub
When you have just created a form, it displays dully on the screen. Before designing, while designing, or after designing the form, it is a good idea to save it. To do this, you can click the Save button on the Standard toolbar and provide its name. The name of a form follows the rules of objects in Microsoft Access. This means that it can be as bizarre as possible. In our lessons, here are the rules we will use to name our forms:
While the table is probably the most appropriate object for you, it may not be convenient to most users. An alternative is to create a form and make it user friendly. Fortunately, Microsoft Access provides fast means of creating a form.
The AutoForm is a feature that allows you to easily generate a form with little to no effort. To use it:
After the AutoForm has generated a new form, you are able to enter new information into its Windows controls. You should also save the form if you want to preserve it.
A Data Access Page is another convenient object that can assist the users of your database. It is web-based object but appears like a form. As done for a form, you can easily create a normal DAP by using a the AutoPage. To do this:
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 to highlight 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 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.
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:
When calling the Close() method to close a form, if the form is not opened or if the specified form doesn't 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.
A form appears as a rectangular object that occupies the necessary portion of the screen and is used to represent its application. A form by itself accomplishes little to no purpose. Its main role is revealed in its ability to be a container. That is, a form is mainly used to hold or host other controls. As a regular window, a form is equipped with a system icon, a title bar, one or more system buttons, borders, corners, and a body. Depending on the role of your form, you may want to design forms that present differing characteristics, even if these forms belong to the same database. For example, while one form presents a normal title bar, you may want another form not to present a title bar at all. While you may allow the user to be able to minimize or maximize a form, you may want to present one or both of these actions for the user. As an application design environment, Microsoft Access provides most of the features you will need for a regular and even advanced database product.
To refer to a form of a Microsoft Visual Basic application, you can use the Me keyword.
After creating a database, or while working on one, all of the forms that belong to it are stored in a collection called AllForms. As seen for the AllTables collection, AllForms is equipped with the Count property that holds the number of the forms that belong to the current database. Each form can be located by its name or its index, using the Item() property.
In Microsoft Access, a form is an object of type Form. A form that is opened in a database is a member of the Forms collection. To refer to an open form in your code, you can enter the Forms collection, followed by an exclamation point !, followed by the name of the form. Here is an example that refers to a form named Students:
You can also include the name of the form between an opening square bracket and a closing square bracket but, if the name is in one word, then the square brackets are optional:
If the name is made of more than one word, then square brackets become required. Here is an example that refers to a form named Potential Applicants:
If you omit the square brackets in this case, the expression or the code may not work.
A form can also be referred to by an index in the Forms collection. The first form of the collection has an index of 0 and can be accessed with Forms(0). The second form in the collection has an index of 1 and can be referred to by Forms(1), and so on. The name of the form can also be used as index. For example, a form named Students can be referred to as Forms("Students").
We mentioned that a form is mostly used as a container because it hosts some of the controls of its application. The controls that a form hosts are members of the Controls collection.
If a form has been designed or exists already but you want its Windows controls to display the values from a table, you can change its Record Source property to that of the table. After doing this, all columns from the table become available to controls on the form.
To programmatically specify the table that holds the data that would be made available to the controls on a form or a report, assign the name of the table to its RecordSource property. Here is an example:
Private Sub cmdRecordSource_Click() Me.RecordSource = "CleaningOrders" End Sub
Like a regular window container, the form displays a title bar in its top section. On the left side of the title bar, it displays an icon. The system icon is fixed and you should not spend time trying to change it. The system icon holds a menu that allows the user to perform the regular operations of a Windows container, including minimizing, maximizing, restoring or closing the window. To display this menu, the user can click the formís system icon:
The presence of the formís system icon is partly controlled by the Control Box property of a form. In most cases, you should make this system icon and its menu available to the user. If for some reason you don't want to provide this functionality, set the Control Box property to No. The form would appear as follows:
If you decide to do this, make sure you provide the user with the ability to close the form and this type of title bar makes it impossible. Of course, a user may know that the form can be closed by clicking the Close menu item under the File group of the main menu or by pressing Ctrl + F4.
The middle section of the title bar is actually referred to as the title bar. It can be used to change the view of the form after right-clicking it. The title bar uses a color set in the Advanced Appearance of Control Panel as Active Title Bar:
Since you cannot control and cannot predict how your users will modify their system colors (because most users are free to set their system colors as they wish, even when they work corporate), you should refrain from changing this color when designing your forms, especially if you plan to distribute your database. Otherwise, the result you see on your form may be different from your users computers.
The right side of the formís title bar displays
three system buttons
group of these buttons is called the Control Box. If you do not
want these system buttons at all, access the Properties window of the form
and set the Control Box Boolean property to No from its Yes
default value. In this case, the form would appear with neither the system
icon nor the system buttons as seen above. Once again, if you do this,
make sure the user has a way to close the form.
Depending on the role and probably the number of Windows controls on a form, you will decide what button to allow or not.
Private Sub cmdManipulate_Click() DoCmd.Minimize End Sub
To maximize a form, the user can click its Maximize button. To programmatically maximize a form, you can call the Maximize() method of the DoCmd object. If a form is maximized, to restore it, the user can click the Restore button. To programmatically restore a form, call the Restore() button of the DoCmd object.
To close a form, the user can click its system Close button. As seen earlier, to close a form, you can call the DoCmd.Close method.
Whether you allow the system icon and system buttons or not, the user needs to be able to know where a form starts and where it ends. This is seen by the borders of the form. In most cases, you will not be concerned with this aspect. Otherwise, you can control the borders of a form.
The borders of a form are controlled using the Border Style property. If you set it to None, the form would appear without borders:
A form displays on the screen using its dimensions. These are the width and height. The form itself controls only the width. The height is controlled by its sections. To change the width of a form, in the Properties window, type the desired decimal value in the Width field of the Format property page. You can also change the form's width by dragging the right border of one of its sections.
The most visible part of a form is an area called Detail. This section starts on a bar labeled Detail and ends at the bottom unless a new section starts. To programmatically access the Detail section of a form, if you working in an event of the form, you can use either Detail or Me.Detail
The Detail section serves as the main host of
other controls. It can also serve to display messages of various kinds.
The Detail section can be enhanced by manipulating its properties. These
properties are not necessarily related to the parent form but can be used
conjointly with it.
To programmatically change the height of the Detail section, assign a constant natural or decimal value to its Height property. Here is an example:
Private Sub cmdChangeHeight_Click() Me.Detail.Height = 3.18 End Sub
Besides the Detail section, a form can be enhanced with one or two more sections: Header and Footer.
To add the Header and Footer sections on a form that doesn't have them, you can use the menu bar where you would click View -> Form Header/Footer. Probably the fastest way to add these sections is by right-clicking anywhere on the form and clicking Form Header/Footer. If the form already has these sections but they don't contain anything, they would be removed. If the form already has these sections and they contain anything, you would receive a warning:
If you still want to delete the Header and Footer sections, you can click Yes; this would un-recoverably delete their content.Like the Detail section, the Header and Footer sections control their own height, which you can change the same way we described above for the Detail section.
To access the Header section in your code, use the form's FormHeader property. Here is an example:
Private Sub cmdManipulate_Click() Me.FormHeader.Height = 2.24 End Sub
To programmatically access the Footer section, use the form's FormFooter property.
This also applies for the Back Color property. The Special Effect property allows you to raise or sink a section.
As much as you can, you should design your
(non-Datasheet) form to display all of the fields of a record. Sometimes this will not be possible. If a form
possesses too many fields, Microsoft Access would equip
the form with one or two scroll bars. A scroll bar allows the user to
scroll from one side of the form to another. The vertical scroll bar is
used to scroll up and down while the horizontal scroll allows scrolling
left and right.
To programmatically control the presence or absence of scroll bars, access the ScrollBars property of the form and assign one of the following four values:
When we study records, we will see that a form (also a table, a query, or a report) is equipped
with some buttons in its lower section. These buttons allow the user to
navigate back and forth between records. These buttons are very useful
during data entry and data analysis. If you are creating a form that would
display the same content all the time, such as a form that does not
actually display records, you can hide the form navigation buttons.
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
A form is equipped with special horizontal lines used
to visually separate sections of a form. They do not perform any other
special function. They can be useful on a continuous form.
To control the presence or absence of the dividing lines of a form, access its DividingLines property and assign the desired Boolean value. Here is an example:
Private Sub cmdManipulate_Click() DividingLines = False End Sub
By default, when a previously created and saved form appears, Microsoft Access remembers the previous position the form had and restores it. If you can make sure that the form is always centered when it comes up. To make this possible, the form is equipped with the Auto Center Boolean property. When set to Yes, this property causes the form to be centered when it displays to the user.
A dialog box is a rectangular object that is used to host or carry other controls without itself being hosted by another object:
The title bar on top of a dialog box has a title and the system close button (only, mostly). Although this is the classic appearance of a dialog box, it is not strictly exclusive. Some dialog boxes display the system icon. On the right side of the title bar, a classic dialog box displays only the system Close button made of X. Again, this is not exclusive. It is not unusual for a dialog box to display the minimize and the maximize/restore buttons but, according to Microsoft standards, a dialog box should appear as the one above: no system icon, only the system Close button, and a type of border referred to as Dialog Frame.
To use a dialog box, the user must open it one way or another. Your job is to decide how and when the user will be able to open a dialog box.
To create a dialog box in Microsoft Access, you start from a form and display it in Design View. To convert an existing form into a dialog box, set its Border Style property to Dialog. This reduces the system buttons to the Close button only.
There are two types of dialog boxes: modal and modeless.
A dialog box is referred to as modal if the user must close it before continuing with another task on the same application.
A dialog box is referred to as modeless if the user doesn't have to close it in order to continue using the application that owns the dialog box:
Private Sub cmdDelete_Click() DoCmd.DeleteObject acForm End Sub
If you omit the second argument and if a form is selected in the Forms section of the Database window, the selected form would be deleted. If no form is selected in the Forms section of the Database window and if you omit the second argument, you would receive a 2493 error stating that the action requires a form name:
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.
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 manually rename a form, in the Forms section of the Database window, you can right-click it and click Rename. This would put the name in edit mode, allowing you to type the new name and press Enter.
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. The second argument must be specified as acForm. The third argument is the name of the existing form that you want to rename. The form must exist in the database. Here is an example that changes the name of a form from Form1 to Specials:
Private Sub cmdRename_Click() DoCmd.Rename "Specials", acForm, "Form1" End Sub
Instead of renaming a form, you can make a copy of it and keep the original. To copy an existing form using the Microsoft Windows Save As routine, in the Forms section of the Database window, you can right-click the form and click Save As... This would open the Save As dialog box that allows you to enter the desired name of the copied form. Alternatively, you can right-click the form, click Copy, then right-click an empty area of the Forms section of the Database window 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.
|Previous||Copyright © Yevol, 2007||Next|