VBA Validation Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wenhow52
    New Member
    • Mar 2010
    • 1

    VBA Validation Code

    Hi - I just need to know how to validate alphanumberic code in MS Access. I can validate numeric information (zip codes), but I can't seem to get the correct code to validate alphanumeric (Canada postal codes). Code in red is what does not work.

    'Verify Edmonton Postal Codes

    If Not IsNull(City) And Not IsNull(PostalCo de) Then
    PostalCodeFirst Two = Val(Left(Postal Code, 2))
    Select Case City
    Case "Edmonton"
    If Not (PostalCodeFirs tTwo Like "T5" Or PostalCodeFirst Two Like "T6") Then
    MsgBox "Edmonton Postal Codes should begin with T5 or T6"
    GoTo CommonProcessin g
    End If[/U][/U] End Select
    End If

    Exit Sub

    CommonProcessin g:
    DoCmd.CancelEve nt
    Me.Undo
    PostalCode.SetF ocus

    End Sub
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    I don't see the red part, but i'll assume you're having trouble with the LIKE statement. Remember LIKE will help you with special characters (like * or ?) but wont do with capital and non capital letters. For that, you should use UCASE. Perhaps this can be a solution:

    [CODE=vb]If Not IsNull(City) And Not IsNull(PostalCo de) Then
    Select Case City
    Case "Edmonton"
    If Not (UCase(PostalCo de) Like "T5*" Or UCase(PostalCod e) Like "T6*") Then
    MsgBox "Edmonton Postal Codes should begin with T5 or T6"
    GoTo CommonProcessin g
    End If
    End Select
    End If[/CODE]

    HTH

    Comment

    Working...