I'm using the following function to count the number of selected records in a datasheet or continuous form view.
It works as expected.
Now I'm trying to make it more versatile by passing a form name to the function instead of hard-coding the form in the function.
I tried using a string ("Forms!" & FormName) instead of MyForm in this line
But it doesn't work.
All help will be appreciated!
Code:
Function CountSelectedRecords()
Dim i As Integer
Dim F As Form
Dim RS As Recordset
Dim intSelRec As Integer
On Error GoTo Err_Catch
' Get the form and its recordset.
Set F = Forms!MyForm
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
For i = 1 To F.SelHeight
intSelRec = intSelRec + 1
RS.MoveNext
Next i
CountSelectedRecords = intSelRec
Exit Function
Err_Catch:
CountSelectedRecords = Null
End Function
Now I'm trying to make it more versatile by passing a form name to the function instead of hard-coding the form in the function.
I tried using a string ("Forms!" & FormName) instead of MyForm in this line
Code:
Set F = Forms!MyForm
All help will be appreciated!
Last edited: