Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if a string matches specific format 1

Status
Not open for further replies.

addy

Technical User
May 18, 2001
743
GB
I have a field on a form which displays the values from a text field from a table.

There are numerous formats (i.e. combinations of alpha and numeric characters) that the values can be in but I need to test if the value matches a specific format, e.g. nnn-nnnnn (where n is any numeric character from 0 to 9).

How can I do this? I thought it would be fairly straightforward but can't seem to figure out how best to do it?

Thanks.

 
I'd use the Like operator.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
So can I just do it as I would in a query as in Like "###-#####"
 
with vba

Code:
Public Function IsNumeric(TestString As String) As Boolean

    Dim strTemp As String
    Dim intLen As Integer
    Dim intCtr As Integer
    Dim strChar As String

    strTemp = TestString
    intLen = Len(strTemp)
    
    If intLen <> 9 Then
        Exit Function
    Else
        strTemp = Left(strTemp, 4)
        If Right(strTemp, 1) <> "-" Then
            Exit Function
        Else
            strTemp = Left(TestString, 3)
            intLen = 3

            For intCtr = 1 To iLen
                strChar = Mid(strTemp, intCtr, 1)
                If Not strChar Like "[0-9]" Then Exit Function
            Next

            strTemp = Right(TestString, 5)
            intLen = 5

            For intCtr = 1 To intLen
                strChar = Mid(strTemp, intCtr, 1)
                If Not strChar Like "[0-9]" Then Exit Function
            Next
            IsNumeric = True
        End If
    End If
End Function

This could be called from a form something like:

Code:
    If IsNumeric(Me.Text0) = False Then
        MsgBox "No Match"
    Else
        MsgBox "Match found"
    End If

This example looks for a string match to your format for a single string however could be modified to check all records in a table.

Thoughts? [ponder]

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
Cheers mate - that looks perfect.

I need to just check one at a time as on the Form, I have some functionality which needs to run or not, depending on the format of this particular string.

Cheers!!
 
Why not simply this ?
If strTest Like "###-#####" Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Also, be careful - IsNumeric is already a build-in function in VBA, so it would be a good idea to rename your function to something else...

Have fun.

---- Andy
 
How are ya addy . . .

Did you try the one liner by [blue]PHV![/blue]:
Code:
[blue]Public Function IsFormatted(usrDat As String) As Boolean
   If usrDat Like "###-#####" Then IsFormatted = True
End Function[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top