ACC2000: How to Enumerate Selected Form Records
ID: Q208502
|
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).
SUMMARY
You can select multiple records in a form using the record selector at the left side of the form. You can also use the SelTop and SelHeight form properties to specify or determine the number of selected records in a continuous or Datasheet view form.
This article shows you how to use the SelTop and SelHeight form properties to enumerate through the list of selected records in a form.
NOTE: SelLeft and SelWidth are form properties that you can use to determine the current columns selected in Datasheet view; however, this article does not discuss these form properties.
MORE INFORMATION
The SelTop and SelHeight properties enable you to determine which records are selected in a continuous or Datasheet view form. You can use the SelTop property to determine which row is the first in the selection. You can use the SelHeight property to determine the number of rows in the current selection.
The following information describes how to use these properties to
enumerate the list of selected records from one of the following:
- A macro run from a toolbar button or an AutoKeys macro.
-
Code run from a command button in the header or footer section of the form.
NOTE: The second method presents some challenges and is a bit more difficult to accomplish. Suppose that you want to place a command button in the header or footer section of a continuous form to enable your users to copy, move, or print a report against the selected set of records. When you press this button, the current selection of records disappears when the command button receives focus. Information later in this article shows you how to circumvent this behavior.
Using a Toolbar Button or an AutoKeys Macro to Enumerate Selected Records
The advantage to using a toolbar button or keyboard key to run your code
is that the current selection of records is now lost; this simplifies the
procedure:
CAUTION: Following the steps in this example will modify the sample
database Northwind.mdb. You may want to back up the Northwind.mdb file
and perform these steps on a copy of the database.
- Open the sample database Northwind.mdb and create a new module.
- Type or paste the following procedure in the new module:
Function DisplaySelectedCompanyNames()
Dim i As Long
Dim F As Form
Dim RS As Recordset
' Get the form and its recordset.
Set F = Forms![Customers1]
Set RS = F.RecordsetClone
' Move to the first record in the recordset.
RS.MoveFirst
' Move to the first selected record.
RS.Move F.SelTop - 1
' Enumerate the list of selected records presenting
' the CompanyName field in a message box.
For i = 1 To F.SelHeight
MsgBox RS![CompanyName]
RS.MoveNext
Next i
End Function
- Save the module with a unique name and close the module.
- Create a new macro named AutoKeys as follows:
Macro Name Action
----------------------
{F5} RunCode
AutoKeys Actions
------------------------------------------------
RunCode
Function Name: =DisplaySelectedCompanyNames()
- On the Insert menu, click Form to open the New Form dialog box.
- Click AutoForm: Tabular in the list of available wizards, select the Customers table in the Choose the table or query where the object's data comes from box, and then click OK.
- On the File menu, click Save and save the form with the default name, Customers1.
- Right-click the toolbar and click Customize.
- In the Customize dialog box, click the Commands tab and under Categories, click All Macros. Under Commands, drag "AutoKeys.{F5}" from the Commands list to an empty space on the toolbar, and then click Close.
- Select a record or a set of records on the form by using the record selector buttons on the left side of the continuous form. Press the F5 key or click the new toolbar button.
Note that a message box appears for each selected record displaying the contents of the selected record's Company Name field.
Using a Command Button to Enumerate Selected Records
- Follow steps 1 through 3 in the "Using a Toolbar Button or an AutoKeys Macro to Enumerate Selected Records" section to create the module with the DisplaySelectedCompanyNames() procedure.
- Open the module in Design view and add the following to the Declaration section:
Dim MySelTop As Long
Dim MySelHeight As Long
Dim MySelForm As Form
Dim fMouseDown As Integer
- Type or paste the following two procedures:
Function SelRecord(F As Form, MouseEvent As String)
Select Case MouseEvent
Case "Move"
' Store the form and the form's Sel property settings
' in the MySel variables ONLY if mouse down has not
' occurred.
If fMouseDown = True Then Exit Function
Set MySelForm = F
MySelTop = F.SelTop
MySelHeight = F.SelHeight
Case "Down"
' Set flag indicating the mouse button has been pushed.
fMouseDown = True
Case "Up"
' Reset the flag for the next time around.
fMouseDown = False
End Select
End Function
Public Sub SelRestore()
Debug.Print "got into Restore"
' Restore the form's Sel property settings with the values
' stored in the MySel variables.
MySelForm.SelTop = MySelTop
MySelForm.SelHeight = MySelHeight
End Sub
- Save and close the module.
- On the Insert menu, click Form to open the New Form dialog box.
- Click AutoForm: Tabular in the list of available wizards, select the Customers table in the Choose the table or query where the object's data comes from box, and then click OK.
- On the File menu, click Save and save the form with the default name, Customers1.
- Open the Customers1 form in Design view. Double-click the form footer section bar to open the properties sheet (if it isn't already open), and change the Height property to .5 inches.
- Add a command button to the form footer section and set the following properties as indicated:
Name: cmdSelectedCompanyNames
Caption: Display Selected Company Names
Width: 2"
OnClick: [Event Procedure]
OnMouseDown: =SelRecord([Form],"Down")
OnMouseMove: =SelRecord([Form],"Move")
OnMouseUp: =SelRecord([Form],"Up")
- Add the following code to the Click event of the new cmdSelectedCompanyNames command button:
Private Sub cmdSelectedCompanyNames_Click()
Dim X
' Restore the lost selection.
SelRestore
' Enumerate the list of selected company names.
X = DisplaySelectedCompanyNames()
End Sub
- View the form in Form view and select a record or a set of records by using the record selector buttons on the left side of the continuous form. Click the new "Display Selected Company Names" command button.
Note that a message box appears for each selected record displaying the
contents of the selected record's Company Name field.
NOTE: If you want to use the command button to enumerate the list of
selected records in a subform, pass the subform as the argument to the
SelRecord() function from the command button's Mouse events. For example,
suppose your subform is called Orders Subform; the command button mouse-move properties would be set to the following:
OnMouseDown: =SelRecord([Orders Subform].[Form],"Down")
OnMouseMove: =SelRecord([Orders Subform].[Form],"Move")
OnMouseUp: =SelRecord([Orders Subform].[Form],"Up")
REFERENCES
For more information about selected records in forms, click Microsoft Access Help on the
Help menu, type selecting fields and records in the Office Assistant or
the Answer Wizard, and then click Search to view the topic.
Additional query words:
multi-selection multiple
Keywords : kbdta FmsHowto
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto