Count of Occurances of a Character in a String - BCP

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to find the number of carriage returns in a memo field. Is there a
canned function to do that?
 
Assuming you're using Access 2000 or newer, try the following:

Private Function CountInstances( _
ByVal ToSearch As String, _
ByVal ToFind As String) As Long

CountInstances = (Len(ToSearch) - _
Len(Replace$(ToSearch, ToFind, vbNullString))) _
\ Len(ToFind)

End Function

What this does is replace each occurrence of the character (or word or
expression) in the string with a null string (""). Assuming that the
character exists in the string, the length of the string will be reduced by
that operation. Knowing how much the length of the string decreased, we can
determine how many times the character occurred in the string.

A carriage return in Access should be both a Carriage Return (Chr$(10)) and
a Line Feed (Chr$(13)), in that order.
 
Back
Top