Count of Occurances of a Character in a String - BCP

B

BCP

I need to find the number of carriage returns in a memo field. Is there a
canned function to do that?
 
D

Douglas J. Steele

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top