Count dots/periods in a text string

G

Guest

I need validate that there are no more than 3 dots anywhere in a text
string. If there are at least 3 then there needs to be an error message and
the information gets cleared from the field.

Cheers
 
G

Guest

Public Function correctPeriods(ByVal s As String) As Boolean
Dim finished As Boolean
finished = False
dim count as Integer
count = 0
Do While Not finished
If (InStr(s, ".") <> 0) Then
count = count + 1
s = Mid$(s, InStr(s, ".") + 1)
Else
finished = True
End If
Loop
correctPeriods = (count < 4)
End Function

Hope this helps.
 
G

Guest

How would I declare this in the field?

Stwange said:
Public Function correctPeriods(ByVal s As String) As Boolean
Dim finished As Boolean
finished = False
dim count as Integer
count = 0
Do While Not finished
If (InStr(s, ".") <> 0) Then
count = count + 1
s = Mid$(s, InStr(s, ".") + 1)
Else
finished = True
End If
Loop
correctPeriods = (count < 4)
End Function

Hope this helps.
 
D

Douglas J. Steele

In the text box's BeforeUpdate event, try:

Private Sub MyTextbox_BeforeUpdate(Cancel As Integer)

If IsNull(Me!MyTextbox) = False Then
If Len(Me!MyTextbox) - Len(Replace(Me!MyTextbox, ".", "")) > 3 Then
MsgBox "You have too many dots in the text."
Cancel = True
End If
Else
MsgBox "You must supply some text"
Cancel = True
End If

End Sub
 
G

Guest

Now that I have thought about it, what I thought I wanted isn't correct. The
text box is used for entering process document reference points and I want to
ensure that only one reference is being entered.

Maybe it would better to search for a space?
 
G

Guest

It should work. It is an elegant expression. To do it any other way would
require more code. Post your code as you have it.
 
D

Douglas J. Steele

What does "didn't do anything" mean?

I did notice a slight error: it should be >= 3, rather than simply >3, if
you're trying to catch strings with exactly three periods in them.

(Your original spec is ambiguous: in one place you say "validate that there
are no more than 3 dots", where in another place you say "if there are at
least 3 then there needs to be an error message")
 
G

Guest

Doug's is better, but just for grins, here is another way. The number of
periods in the string will be returned:

If Ubound(Split(strSomeString,".")) > 3 Then
"I'm seeing spots again"
End If
 
G

Guest

To test whether they are Polka Dots, use the IsAccordian() function.

(I'ts Friday, it's raining, I'm bored)
 
G

Guest

The text box is used for entering process documentation reference points and
I would like to ensure that only one reference is being entered but there is
one case where that will not be correct:

1.2 1.3 1.4

That is why I am now asking to request code to search for a space
 
M

missinglinq via AccessMonster.com

In ACC97 wasn't it called the function called IsSqueezeBox() ?
 
M

missinglinq via AccessMonster.com

Syntax is the same for spaces as for dots, Polka or otherwise!

Replace

If Len(Me!MyTextbox) - Len(Replace(Me!MyTextbox, ".", "")) > 3 Then

with

If Len(Me!MyTextbox) - Len(Replace(Me!MyTextbox, " ", "")) > 3 Then

and change the 3 to the number of spaces you're testing for.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 

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