MANAGING MULTIPLE INSTANCES OF THE SAME FORM

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.

Looking at the Feature Set

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:

  • By selecting and deselecting from a list, the forms are created and closed automatically.

  • If you click a copy of the opened form, the selection in the browse window is deselected.

  • When you close the browse window, all copies of the edit form are closed.

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.

Figure 16.7. Again, the tblEmployees table is used for one of the forms used in this example.


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.

Looking at the Forms Used in Opening Copies of the Same Form Example

To get started, look at the simple browse window created, frmCollectionFormExample (see Figure 16.8).

Figure 16.8. You can see the frmCollectionFormExample without any employee records displayed.


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).

Figure 16.9. After opening three records, I tiled them by using the Window menu's Tile Vertically command.


Examining the Code Used for Managing Multiple Copies of the Same Form

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.

Listing 16.7. Chap16.mdb: Opening and Closing Multiple Instances of the frmEmpForCollectionFormExample Form
Option Compare Database

'-- This collection is used to store the multiple instances
'   of the frmEmpForCollectionFormExample.
Public colEmpForms As New Collection

Private Sub lboEmployees_AfterUpdate()

    '-- If an employee is selected open the form.
    If lboEmployees.Selected(Me!lboEmployees.ListIndex) Then

        '-- Create a new instance of the form.
        Dim frmCurrEmp As Form_frmEmpForCollectionFormExample

        Set frmCurrEmp = New Form_frmEmpForCollectionFormExample

        '-- Set the Filter property to the current record selected in
        '   the list. Then set the FilterOn property to enact the filter.
        frmCurrEmp.Filter = "EmployeeID = " & _
           Me!lboEmployees.ItemData(Me!lboEmployees.ListIndex)
        frmCurrEmp.FilterOn = True

        '-- Make the form visible
        frmCurrEmp.Visible = True

      '-- Store the current EmployeeID into the Tag property on the form.
        frmCurrEmp.Tag = Me!lboEmployees.ListIndex

        '-- Add the form to the colEmpForms collection,
        '   with the Employee ID as the key.
        colEmpForms.Add Item:=frmCurrEmp, _
           Key:=Me!lboEmployees.ItemData(Me!lboEmployees.ListIndex)

    Else  '-- If unselected, close the form.

        On Error Resume Next

        '-- Remove the form from the collection.
        colEmpForms.Remove _
           Me!lboEmployees.ItemData(Me!lboEmployees.ListIndex)

    End If

End Sub

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.
Set the Filter property to the current record selected in the list. Then set the FilterOn property to enact the filter:

frmCurrEmp.Filter = "EmployeeID = " & _
   Me!lboEmployees.ItemData(Me!lboEmployees.ListIndex)
frmCurrEmp.FilterOn = True

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.
Add the form to the colEmpForms collection, with the Employee ID as the key:

colEmpForms.Add Item:=frmCurrEmp, _
   Key:=Me!lboEmployees.ItemData(Me!lboEmployees.ListIndex)

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).

Listing 16.8. Chap16.mdb: If the Form Is Closed Directly, Deselect the Item in the List Box
Private Sub Form_Close()

    On Error Resume Next

    With Forms!frmCollectionFormExample

        '-- if the form is selected, unselect-it, then remove the
        '   form from the colEmpForms collection.

        If len(!lboEmployees.Selected(Me.Tag)) > 0 Then
            !lboEmployees.Selected(CInt(Me.Tag)) = False
            .colEmpForms.Remove _
                !lboEmployees.ItemData (CInt(Me.Tag))

        End If

    End With

End Sub

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.247