Article ID: 142227
Article Last Modified on 1/19/2007
Dim strval As String
Dim sourcename As String
Function CountOccurrenceRecordset(strval As String, sourcename As _
String)
Dim db As DATABASE, rs As Recordset, Strval_Count As Integer
Dim I As Integer
Set db = CurrentDb
Set rs = db.OpenRecordset(sourcename, DB_OPEN_DYNASET)
rs.MoveFirst
Strval_Count = 0
Do Until rs.EOF
For I = 0 To rs.Fields.Count - 2
If TypeName(rs.Fields(I).Value) <> "Byte()" Then
' In version 7.0, the OLE data type returns a data type error 13
' on a comparison. This is to exclude that field from the search.
If rs.Fields(I).Value = strval Then
Strval_Count = Strval_Count + 1
End If
End If
Next I
rs.MoveNext
Loop
MsgBox "Count of " & strval & " found = " & Strval_Count
CountOccurrenceRecordset = Strval_Count
End Function
Microsoft Access Version 2.0:
Function CountOccurrenceRecordset (strval As String, sourcename As _
String)
Dim db As Database, rs As Recordset, Strval_Count As Integer
Set db = dbengine(0)(0)
Set rs = db.OpenRecordset(sourcename, DB_OPEN_DYNASET)
rs.MoveFirst 'Move to first record in recordset.
Strval_Count = 0 'Set strval_Count variable to zero.
Do Until rs.EOF 'Loop until end of recordset(EOF).
For i = 0 To rs.fields.Count - 1 'Loop through each field.
If rs.fields(i) = strval Then 'Do next line if the field
'equals string variable.
Strval_Count = Strval_Count + 1 'Add one to string
'value.
End If
Next i
rs.MoveNext 'Move to next record.
Loop
MsgBox "Count of " & strval & " found = " & Strval_Count
'Display count of sting in message box.
CountOccurrenceRecordset = Strval_Count
End Function
? CountOccurrenceRecordset("Robert","Employees")
Function CountOccurrenceRecord(strval As String, sourcename As _
String)
Dim db As DATABASE, rs As Recordset, Strval_Count As Integer
Dim I As Integer
Set db = CurrentDb
Set rs = db.OpenRecordset(sourcename, DB_OPEN_DYNASET)
rs.MoveFirst
Strval_Count = 0
For I = 0 To rs.Fields.Count - 2
If TypeName(rs.Fields(I).Value) <> "Byte()" Then
If rs.Fields(I).Value = strval Then
Strval_Count = Strval_Count + 1
End If
End If
Next I
MsgBox "Count of " & strval & " found = " & Strval_Count
CountOccurrenceRecord = Strval_Count
End Function
Microsoft Access Version 2.0:
Function CountOccurrenceRecord (strval As String, sourcename As _
String)
Dim db As Database, rs As Recordset, Strval_Count As Integer
Set db = dbengine(0)(0)
Set rs = db.OpenRecordset(sourcename, DB_OPEN_DYNASET)
rs.MoveFirst
Strval_Count = 0
For i = 0 To rs.fields.Count - 1
If rs.fields(i) = strval Then
Strval_Count = Strval_Count + 1
End If
Next i
MsgBox "Count of " & strval & " found = " & Strval_Count
CountOccurrenceRecord = Strval_Count
End Function
Additional query words: how to
Keywords: kbhowto kbprogramming KB142227