Count for multiple occurrence of an expression in a string

C

chinky

I want to count number of '.' (dots) in an expression e,g. asd.rty.de.com
(answer = 3 dots at positions 4,8,11 respectively) . Instr functions gives
only the first occurrence.
Any help is appreciated
Thanks
Anupam
 
L

Larry Daugherty

What comes redily to mind is a function that accepts two arguments:
the string to analyze and the comparison character. Look in Help for
Mid. The function would return an number of times the comparison
character was found in the string as an integer.

HTH
 
C

chinky

Thanks for the response.
I am using Windows 2003, but these functions are not available. Do I need to
install something ?
 
C

chinky

Thanks for your response. I will look into it.

Larry Daugherty said:
What comes redily to mind is a function that accepts two arguments:
the string to analyze and the comparison character. Look in Help for
Mid. The function would return an number of times the comparison
character was found in the string as an integer.

HTH
 
D

Douglas J. Steele

Do you mean Access 2003 (not Windows 2003)? The functions are definitely
there.

How are you trying to use the functions? (i.e.: where have you put Allen's
code?) What happens when you use them? Do you get an error message? If so,
what's the error?
 
A

Allen Browne

The version of Windows does not matter.

UBound() has been in Access for ever.
Split() has been part of Access since 2000.
 
K

Ken Snell \(MVP\)

Here's a function that does this type of counting:

' ***************************************
' ** Function CountCharacterOccurences **
' ***************************************

Public Function CountCharacterOccurences(ByVal strStringToSearch As Variant,
_
ByVal strCharacterToFind As String) As Long
' *** THIS FUNCTION COUNTS THE NUMBER OF TIMES A SINGLE CHARACTER IS FOUND
' *** IN A TEXT STRING. THE SEARCH IS CASE-INSENSITIVE.
' *** THE FUNCTION'S VALUE IS THE NUMBER OF TIMES THE SINGLE CHARACTER IS
FOUND.

Dim lngCountChars As Long, lngPosition As Long

On Error Resume Next

lngCountChars = 0
'truncate sChar in case it is longer than one character
strCharacterToFind = Left(strCharacterToFind, 1)

For lngPosition = 1 To Len(strStringToSearch)
'if character is found, increment the counter
If Mid(strStringToSearch, lngPosition, 1) = strCharacterToFind Then _
lngCountChars = lngCountChars + 1
Next lngPosition

CountCharacterOccurences = lngCountChars
Exit Function
End Function
 
D

Duane Hookom

I think we need another possible solution.
=Len([YourExpression]) - Len(Replace([YourExpression],".",""))

Len("asd.rty.de.com") - Len(Replace("asd.rty.de.com",".","")) = 3
 
C

chinky

My apologies, I meant to say that I am using Access 2003
I am using it in SQL query, probably that is again my mistake.
Need guidance on how to use it.
Thanks
 

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