In the following sections, let's look at a more advanced example for managing multiple instances of forms. In the example given in Chapter 2, you were introduced to this technique by opening the same form for employees and supervisors.
Now you can see how to open multiple copies of the same form, using the same record source, but with different records chosen. This is useful when you have a browse window open for viewing records, and then want users to be able to open up multiple records at the same time with the same form. Some of the features also included are as follows:
The code for performing these actions is examined later in the section “Examining the Code Used for Managing Multiple Copies of the Same Form.” In the meantime, let's look at some of the other objects used in this example. You will be using the same table used in the last example, tblEmployees, shown again in Figure 16.7.
Things are easier to understand for this example because you have to deal with the code behind only two forms, frmCollectionFormExample and frmEmpForCollectionFormExample. Both forms again can be found on the CD-ROM in ExamplesChap16Chap16.mdb.
To get started, look at the simple browse window created, frmCollectionFormExample (see Figure 16.8).
To see how the form works, click a few names in the list. You will then see the frmEmpForCollectionFormExample form open for each employee record chosen (see Figure 16.9).
To start, look at the code used with the lboEmployees list box. This code, plus the Declarations section of the frmCollectionFormExample form, is shown in Listing 16.7.
Before taking the parts of the lboEmployees_AfterUpdate event procedure one segment at a time, the line of code to look at is the declaration of the colEmpForms collection variable, which will contain the copies of the form. This is done in the Declarations section of the form:
Option Compare Database '-- This collection is used to store the multiple instances ' of the frmEmpForCollectionFormExample. Public colEmpForms As New Collection
Next, walking through the code of the After Update event, here are the steps taken:
1. |
If an employee is selected, open the form. This comparison is performed by looking at the Selected property of the multiselect list box, lboEmployees: If lboEmployees.Selected(Me!lboEmployees.ListIndex) Then |
2. |
Create a new instance of the form: Dim frmCurrEmp As Form_frmEmpForCollectionFormExample Set frmCurrEmp = New Form_frmEmpForCollectionFormExample |
3. | |
4. |
Make the form visible: frmCurrEmp.Visible = True |
5. |
Store the current EmployeeID into the Tag property on the form: frmCurrEmp.Tag = Me!lboEmployees.ListIndex |
Tip
The Tag property is a great way to store extra information about objects that you can examine in your code at a later time in execution. Forms, reports, and controls all have Tag properties for your use. The value stored in the Tag property must be of the data type String. To see another example of using the Tag property with a form, see Chapter 9, “Creating Powerful Forms.”
6. |
The rest of the code in this event procedure occurs if the employee is deselected in the list. Then the following piece of code executes:
colEmpForms.Remove Me!lboEmployees.ItemData(Me!lboEmployees.ListIndex)
That's it for this event. The next (and last) routine to look at is the event when you close a copy of the frmEmpForCollectionFormExample form directly. The event that occurs is the Close event of the form itself (see Listing 16.8).
The last action that can occur, where all the forms are closed when the browse window is closed, is a freebie. This is because the variable colEmpForms is declared in the frmCollectionFormExample form. Even though it was as a public variable, because it was declared in the code behind form, it's released when the form is closed.
Remember, my goal is to get you started in using techniques. Work with these examples and then try to build on them. An example of this would be to take the Bookmark Tracker and to make it so that it will take an arbitrary number of description elements.
3.145.172.146