>> Test for alpha character

J

Jonathan

Hi using Access 2003, is there a way to check whether a string contains alpha
characters?

Here is my code that checks string for at least 1 alpha character

For myLoop = 1 To Len(myString)
myChr = Mid(myString, myLoop, 1)
If myChr > Chr(64) And myChr < Chr(91) Or _
myChr > Chr(96) And myChr < Chr(123) Then
msg = vbNullString
Exit For
End If
Next myLoop

I guess that I'm looking for a function that returns the ascii number of
myChr.

Any ideas or suggestions appreciated :)

Many thanks,
Jonathan
 
R

Robert Morley

If you need the ASCII value for the code, then the way you're doing it is on
the right track (though I'll tweak it a bit for the ASCII value below). If
all you need to do is check if there IS an alpha character, then I'd suggest
you use:

IsAlpha = myString Like "*[a-z]*" 'if you know Option Text is in effect
or IsAlpha = myString Like "*[A-Za-z]*" 'if you don't.

But if you specifically need the ASCII value, here's the code you want:

For myLoop = 1 To Len(myString)
myChr = Asc(Mid(myString, myLoop, 1)) 'Dim myChr As Long instead
If myChr > 64 And myChr < 91 Or _
myChr > 96 And myChr < 123 Then
msg = vbNullString
Exit For
End If
Next myLoop

You may also get better performance out of a Select Case in this intance,
I'm not sure...give it a try if performance is importance:

For myLoop = 1 To Len(myString)
myChr = Asc(Mid(myString, myLoop, 1)) 'Dim myChr As Long instead
Select Case myChr
Case 64 To 91, 96 To 123
msg = vbNullString
Exit For
End Select
Next myLoop

I'm not sure if you can use an Exit For from within a Select Case statement,
though...I never use the things. :) If not, post back and I'll restructure
your loop for you to use Do Until or Do While.



Rob
 
J

Jonathan

Really great response to my question. Many thanks

Jonathan

Robert Morley said:
If you need the ASCII value for the code, then the way you're doing it is on
the right track (though I'll tweak it a bit for the ASCII value below). If
all you need to do is check if there IS an alpha character, then I'd suggest
you use:

IsAlpha = myString Like "*[a-z]*" 'if you know Option Text is in effect
or IsAlpha = myString Like "*[A-Za-z]*" 'if you don't.

But if you specifically need the ASCII value, here's the code you want:

For myLoop = 1 To Len(myString)
myChr = Asc(Mid(myString, myLoop, 1)) 'Dim myChr As Long instead
If myChr > 64 And myChr < 91 Or _
myChr > 96 And myChr < 123 Then
msg = vbNullString
Exit For
End If
Next myLoop

You may also get better performance out of a Select Case in this intance,
I'm not sure...give it a try if performance is importance:

For myLoop = 1 To Len(myString)
myChr = Asc(Mid(myString, myLoop, 1)) 'Dim myChr As Long instead
Select Case myChr
Case 64 To 91, 96 To 123
msg = vbNullString
Exit For
End Select
Next myLoop

I'm not sure if you can use an Exit For from within a Select Case statement,
though...I never use the things. :) If not, post back and I'll restructure
your loop for you to use Do Until or Do While.



Rob
Hi using Access 2003, is there a way to check whether a string contains alpha
characters?

Here is my code that checks string for at least 1 alpha character

For myLoop = 1 To Len(myString)
myChr = Mid(myString, myLoop, 1)
If myChr > Chr(64) And myChr < Chr(91) Or _
myChr > Chr(96) And myChr < Chr(123) Then
msg = vbNullString
Exit For
End If
Next myLoop

I guess that I'm looking for a function that returns the ascii number of
myChr.

Any ideas or suggestions appreciated :)

Many thanks,
Jonathan
 

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