ACC: How To Enumerate Selected Form RecordsID: Q148393
|
Moderate: Requires basic macro, coding, and interoperability skills.
In Microsoft Access, you can select multiple records in a form using the
record selector at the left side of the form. In Microsoft Access 7.0 and
97, you can use the new SelTop and SelHeight form properties to specify or
determine the number of selected records in a continuous or Datasheet view
form.
The following information describes how you can use these new properties
to enumerate through the list of selected records in a form.
NOTE: SelLeft and SelWidth are new form properties that you can use to
determine the current columns selected in Datasheet view; however, this
article does not discuss these new form properties.
Microsoft Access 7.0 and 97 provide the SelTop and SelHeight properties to
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
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
Macro Name Action
----------------------
{F5} RunCode
AutoKeys Actions
------------------------------------------------
RunCode
Function Name: =DisplaySelectedCompanyNames()
In the Customize dialog box, click the Commands tab and select
"All Macros." Under Commands, drag "AutoKeys.{F5}" from the Commands
list to an empty space on the toolbar, and then click Close.
In the Customize Toolbars dialog box, under Categories, select "All
Macros." Under Objects, drag "AutoKeys.{F5}" from the Objects list to
an empty space on the toolbar, and then click Close.
Dim MySelTop As Long
Dim MySelHeight As Long
Dim MySelForm As Form
Dim fMouseDown As Integer
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
Name: cmdSelectedCompanyNames
Caption: Display Selected Company Names
Width: 2"
OnClick: [Event Procedure]
OnMouseDown: =SelRecord([Form],"Down")
OnMouseMove: =SelRecord([Form],"Move")
OnMouseUp: =SelRecord([Form],"Up")
Private Sub cmdSelectedCompanyNames_Click()
Dim X
' Restore the lost selection.
SelRestore
' Enumerate the list of selected company names.
X = DisplaySelectedCompanyNames()
End Sub
OnMouseDown: =SelRecord([Orders Subform].[Form],"Down")
OnMouseMove: =SelRecord([Orders Subform].[Form],"Move")
OnMouseUp: =SelRecord([Orders Subform].[Form],"Up")
For more information about selected records in forms, search the Help Index for the term "Selecting," and then view "Selecting fields and records."
Additional query words: multi-selection multiple
Keywords : FmsHowto
Version : 7.0 97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 19, 1999