Article ID: 120913
Article Last Modified on 1/19/2007
145777 Microsoft Access Sample Reports Available in Download Center
175072 Microsoft Access 97 Sample Reports Available in Download Center
Option Explicit
Function GetLineNumber (F As Form, KeyName As String, KeyValue)
Dim RS As Recordset
Dim CountLines
On Error GoTo Err_GetLineNumber
Set RS = F.RecordsetClone
' Find the current record.
Select Case RS.Fields(KeyName).Type
' Find using numeric data type key value.
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & KeyName & "] = " & KeyValue
' Find using date data type key value.
Case DB_DATE
RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
' Find using text data type key value.
Case DB_TEXT
RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
Case Else
MsgBox "ERROR: Invalid key field data type!"
Exit Function
End Select
' Loop backward, counting the lines.
Do Until RS.BOF
CountLines = CountLines + 1
RS.MovePrevious
Loop
Bye_GetLineNumber:
' Return the result.
GetLineNumber = CountLines
Exit Function
Err_GetLineNumber:
CountLines = 0
Resume Bye_GetLineNumber
End Function
=GetLineNumber(Form,"ID",[ID])
Field Name: ID
Data Type: AutoNumber
Name: LineNum
ControlSource: =GetLineNumber([Form], "ID", [ID])
Keywords: kbhowto kbprogramming kbusage KB120913