Re: Code for counting commas in a text field?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: LeapYr60 (LeapYr60_at_discussions.microsoft.com)
Date: 06/16/04


Date: Wed, 16 Jun 2004 14:40:01 -0700

It's working now. I inserted the CountCharacters which is stored in Modules in the report's source query:
=========
Option Compare Database

Public Function CountCharacters(strInput As String, strCharactertoCount As String) As Double
On Error GoTo HandleErrors

Dim lngCount As Long
Dim lngCounter As Long
Dim lngCharacterCounter As Long
Dim lngCharacterstoCheck As Long
Dim strCheckCharacter As String

lngCharacterstoCheck = Len(strCharactertoCount)

lngCount = Len(strInput)

lngCounter = 1

lngCharacterCounter = 0

Do Until lngCounter > lngCount

strCheckCharacter = Mid(strInput, lngCounter, lngCharacterstoCheck)

If strCharactertoCount = strCheckCharacter Then
lngCharacterCount = lngCharacterCount + 1
End If

lngCounter = lngCounter + 1

Loop

CountCharacters = lngCharacterCount

Exit Function
HandleErrors:
CountCharacters = 0
Exit Function

End Function

"Katrina" wrote:

> What is the code you are using
>
>
> Kat
>
> "LeapYr60" <LeapYr60@discussions.microsoft.com> wrote in message
> news:12F27FF3-9868-4C0F-9906-BBAA9D8D3566@microsoft.com...
> > I receive an error msg that reads:
> > "The expression you entered contains invalid syntax, you may have entered
> an operand without a valid operator."
> >
> > ideas?
> >
> > "Katrina" wrote:
> >
> > > I would use the instr function in a loop until you find no more matches,
> > > then add 1 to the count
> > > dim myinstr as long, comcnt as long, mynotes as long
> > > mynotes = notes
> > > comcnt = 0
> > > myinstr = instr(mynotes, ",")
> > > do until myinstr = 0
> > > comcnt = comcnt + 1
> > > mynotes = right(mynotes, len(mynotes) - myinstr)
> > > myinstr = instr(mynotes, ",")
> > > loop
> > > 'add 1 to the number of commas found
> > > comcnt = comcnt + 1
> > >
> > > HTH
> > > Kat
> > >
> > >
> > >
> > >
> > > "LeapYr60" <LeapYr60@discussions.microsoft.com>
> > > wrote in message
> news:F19BCFE0-668D-44A5-81EF-7DB0FBF3EF0A@microsoft.com...
> > > > Can you help with the Access expression for counting the number of
> commas
> > > in a text field? I would like the expression to return a number plus
> one.
> > > >
> > > > The text field containing the data is named, "notes"
> > > >
> > > > So, if the text field contains:
> > > > C10, C20, C30
> > > >
> > > > The expression should return a number 3.
> > > >
> > > > Many thanks,
> > > > Matt
> > >
> > >
> > >
>
>
>



Relevant Pages