"Joe Blow" <(E-Mail Removed)> wrote in message
news:45749a39$0$20605$(E-Mail Removed)
> Hello All:
>
> First off, thank you so much for your help. It is truly appreciated.
>
> I am trying to write an expression in a query that counts how many
> times a particular string of data repeats in a text field.
>
> ie: THE BUS IS A VERY BUSY BUSINESS.
>
> I ask, how many times does the exact string "BUS" show up, and the
> result should be 3. One for BUS, another for BUSY and another for
> BUSINESS. Can't use InStr because that only returns the first
> position...
>
> Any ideas how to write this column in an Access 2003 query?
I think you'll have to call a user-defined function; like this one,
perhaps:
'----- start of code -----
Function fncCountString(LookIn As Variant, LookFor As Variant) As Long
' Returns the number of times the second (string) argument
' is found in the first (string) argument.
'
' Written by: Dirk Goldgar, DataGnostics
' Published for public use -- please retain attribution.
Dim lCount As Long
Dim sLookIn As String
Dim sLookFor As String
If IsNull(LookIn) Or IsNull(LookFor) Then
fncCountString = 0
Else
lCount = UBound(Split(LookIn, LookFor))
If lCount < 0 Then
fncCountString = 0
Else
fncCountString = lCount
End If
End If
End Function
'----- end of code -----
With the above function stored in a standard module, you can call it
from a query if you like.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)