< Day Day Up > |
Finally, there are some things that you can do in VBA code that you simply cannot do from the Access user interface. One of these is to create more than one copy of the same form. As you learned in Chapter 8, a form object is just an instance of the corresponding form class. Like any other class, this class can be instantiated more than once. But the Access designers didn't choose to make this capability available to end users.
To review the code for creating a single instance of a form, see "Creating Your Own Objects," p. 124. Creating multiple form instances in code is just an extension of the technique for creating a single form instance in code. For example, this code creates two instances of the Projects form: Sub CreateTwoForms() ' Create and display two copies ' of the Projects form Dim frm1 As New Form_Projects frm1.Visible = True frm1.Move 0, 0 Dim frm2 As New Form_Projects frm2.Visible = True MsgBox "Click OK to continue" End Sub As you can see, the code uses two different form variables to represent the two forms. It also moves the first form to the upper-left corner of the work area (otherwise, the second form would exactly cover the first). Figure 10.7 shows the result of running this code. Figure 10.7. Two instances of the same form open in Access.Multiple form instances behave much like any other form: you can set their properties, navigate their data, and so on. Each instance is a separate member of the Forms collection. However, because you can only create multiple instances in VBA, you need to maintain a form variable for each instance to keep it visible onscreen.
For information on maintaining a variable beyond a single procedure, see "Measuring the Lifetime of a Variable or Constant," p. 136.
|
< Day Day Up > |