counting string occurences (1 Viewer)

W

witchbix

Guest
I'm a beginner on this. I know there are Dcount and Dsum, but I don't know where I enter the functions in the query. I would like to be able to count the string occurences from a particular column of a record. The string is provided by the user, with the range of date selected in the form. This function is to find out the frequency of certain string in a field in a certain period of time.

Thanks for your time!!

Eliza
 

raskew

AWF VIP
Local time
Yesterday, 19:58
Joined
Jun 2, 2001
Messages
2,734
This might help. It'll require modification to incorporate your date criteria.

Function StrCount(TheStr As String, TheItem As Variant) As Integer
'------------------------------------------------------------------
' PURPOSE: Counts the numbers of times an item occurs
' in a string.
' ARGUMENTS: TheStr: The string to be searched.
' TheItem: The item to search for.
' RETURNS: The number of occurences as an integer.
'
' NOTES: To test: Type '? StrCount("The quick brown fox jumped over
' the lazy dog", "the") in the debug window.
' The function will return 2.
'------------------------------------------------------------------
Dim strHold As String, itemhold As Variant
Dim placehold As Integer
Dim i As Integer, j As Integer

strHold = TheStr
itemhold = TheItem
j = 0


If InStr(1, strHold, itemhold) > 0 Then
While InStr(1, strHold, itemhold) > 0
placehold = InStr(1, strHold, itemhold)
j = j + 1
strHold = Mid(strHold, placehold + Len(itemhold))
Wend
'Debug.Print "StrCount= " & j
End If
StrCount = j
End Function
 

Users who are viewing this thread

Top Bottom