Line Count

  • Thread starter Thread starter Tom
  • Start date Start date
Assuming you're using Access 2000 or newer, a generic approach to finding
out how times a particular character or string appears in another string is
a function like:

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 routine 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 decreases, you can
determine how many times the character occurred in the string.

Now, if you've got multiple lines of text showing up in a text box, each
line must be separated by a Line Feed (Chr(13)) and a Carriage Return
(Chr(10)) in that order. Therefore, you could use:

lngNumberOfLines = CountInstances(Me.MyTextBox, vbCrLf)

Another approach you could take would be to use the Split function to break
your text into an array of individual lines. You'd do this as:

Dim lngNumberOfLines As Long
Dim varText As Variant

varText = Split(Me.MyTextBox, vbCrLf)
lngNumberOfLines = UBound(varText)
 
Thanks Doug both otions work a treat

Tom
Douglas J Steele said:
Assuming you're using Access 2000 or newer, a generic approach to finding
out how times a particular character or string appears in another string
is
a function like:

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 routine 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 decreases, you
can
determine how many times the character occurred in the string.

Now, if you've got multiple lines of text showing up in a text box, each
line must be separated by a Line Feed (Chr(13)) and a Carriage Return
(Chr(10)) in that order. Therefore, you could use:

lngNumberOfLines = CountInstances(Me.MyTextBox, vbCrLf)

Another approach you could take would be to use the Split function to
break
your text into an array of individual lines. You'd do this as:

Dim lngNumberOfLines As Long
Dim varText As Variant

varText = Split(Me.MyTextBox, vbCrLf)
lngNumberOfLines = UBound(varText)
 
Back
Top