Any Access 2000 form with a module behind it is a form class; you can create new instances of the class with the New keyword just as you do with any generic Access class. One advantage of form classes is that they have all the standard form properties and methods as well as your custom additions.
You can view the properties and methods of form classes using the Object Browser. You select the Project's name in the Project/Library drop-down list box and select a form class name to see its properties and methods.
The Object Browser in Figure 5-15 displays a subset of the members in the Form_frmButtonSwitchboard class. Recall that this form navigates to any of three other forms via event procedures. The event procedures are class methods, such as cmdGoHere_click. The buttons, such as cmdGoHere, are class properties.
Figure 5-15. Methods and properties of a form class.
The following procedure references class modules in Access 2000. It has several code segments that manipulate the basic form class in progressively more sophisticated ways. Working with form classes and their instances resembles working with a cookie cutter and cookies. The cutter is the form class, and the cookies are instances of it. Changes to a cookie do not impact the cutter. On the other hand, changes to the cutter impact all cookies after the change.
Sub testformclass() Dim frm1 As Form 'First code segment 'Saves reference to instance of a form class in frm1. 'Can reference with either class or reference name. Set frm1 = Form_frmCustomers frm1.Caption = "foo" MsgBox Form_frmCustomers.Caption MsgBox frm1.Caption DoCmd.Close acForm, frm1.Name 'Second code segment 'Programmatically alters and opens default form instance. 'Does not set reference to instance. 'Clears instance by setting Visible to False. Form_frmCustomers.Caption = "foo" Form_frmCustomers.Visible = True MsgBox Form_frmCustomers.Caption If MsgBox("Do you want to close form instance?", vbYesNo, _ "Programming Microsoft Access 2000") = vbYes Then Form_frmCustomers.Visible = False End If 'Third code segment 'Open form in Design view to modify class properties. 'Open in Form view to see impact of Design view change. DoCmd.OpenForm "frmCustomers", acDesign Forms("frmCustomers").Caption = "foo" MsgBox Form_frmCustomers.Caption DoCmd.Close acForm, "frmCustomers", acSaveYes Form_frmCustomers.Visible = True MsgBox Form_frmCustomers.Caption 'Fourth code segment 'Restore class caption property. DoCmd.OpenForm "frmCustomers", acDesign Set frm1 = Form_frmCustomers frm1.Caption = "Customers" DoCmd.Close acForm, frm1.Name, acSaveYes Form_frmCustomers.Visible = True MsgBox Form_frmCustomers.Caption End Sub |
The first code segment assigns a form class to a reference. It creates an instance of the Form_frmCustomers class and assigns it to frm1. You can manipulate the instance using either the class or the reference name; the identical results from the two message boxes confirm this. Changing the instance of a form class does not alter the class itself.
You do not need references to designate or modify the properties of form class instances. The second segment accomplishes the same task as the initial one without creating a pointer to the form class. This segment also includes a prompt to ask the user whether he or she wants to close the form instance. (An instance survives for the life of the procedure that creates it unless your code terminates it sooner.)
The third code segment opens a form in Design view and manipulates its properties. Unlike when you programmatically modify the properties of a form in Form view, changes to properties in Design view are persistent after you save the form. The last two lines of the third segment open an instance of the form class in Form view. For the first time in the procedure, the form opens with the caption "foo".
The fourth code segment restores the form class's caption to its original value of "Customers".
The form class sample below drills down further into form classes. You can easily test the behavior of this procedure by using the frmFirstWithControls form, which includes a button that launches the testformclass2 procedure. testformclass2 processes multiple instances of a form class.
Sub testformclass2() On Error GoTo testclass2Trap Dim frm1 As Form Dim frm2 As New Form_frmCustomers Dim int1 As Integer 'Show caption of frm2 before editing the property. MsgBox "Frm2 default caption is " & frm2.Caption, _ vbInformation, "Programming Microsoft Access 2000" 'Set frm1 as a reference to frmCustomers class. Set frm1 = Form_frmCustomers frm1.Caption = "Caption from frm1 instance" 'Reset caption for frm2 instance. frm2.Caption = "Caption from frm2 instance" 'Show the captions of the class instances referenced by 'frm1 and frm2. frm1.SetFocus frm2.SetFocus MsgBox frm1.Caption, vbInformation, _ "Programming Microsoft Access 2000" MsgBox frm2.Caption, vbInformation, _ "Programming Microsoft Access 2000" 'Close form instances by their references. frm1.SetFocus DoCmd.Close frm2.SetFocus DoCmd.Close testclass2Exit: Exit Sub testclass2Trap: If Err.Number = 2467 Then 'Trap attempt to print caption for closed form. MsgBox "Cannot print caption of closed form", _ vbInformation, "Programming Microsoft Access 2000" Resume Next Else Debug.Print Err.Number, Err.Description Resume testclass2Exit End If End Sub |
It is convenient to use references to form class instances when you deal with more than one instance. The sample above uses the references frm1 and frm2. After declaring frm1 as a general form class, the code assigns a reference to an instance of the Form_frmCustomers class to it. The frm2 declaration statement points it at an instance of the same form class. Therefore, frm1 and frm2 are two separate instances of the identical form class.
The second Dim statement creates an instance based on a form class using the New keyword. (You must refer to a form class with the Form_ prefix when using the New keyword to create an instance.) After creating the references, you can invoke standard form properties and methods for the objects to which the variables refer. For example, the procedure resets the Caption property of frm2 and then assigns the focus first to frm1 and then to frm2. This opens both forms with one behind the other on the Access screen.
You can close a form instance by giving it the focus and then invoking the DoCmd object's Close method.