Verify first 2 digit of zip code

stevenyoo321

New member
Local time
Today, 06:39
Joined
Feb 2, 2007
Messages
6
Question:
You need to create an event procedure to verify the value entered for Postal Code textbox. If a user does not enter any value for postal code, a message box will open to alert the user that the field is required. If the state for the customer is TX, verify that the first two digits of the zip code (for Texas) start from 73 to 79. An error message box will open if the zip code is not in that range.


Currently I'm not sure how to verify the first 2 digits of the zip code for TX. This is what I have so far:

Private Sub Postal_Code_BeforeUpdate(Cancel As Integer)
If IsNull([Postal Code]) Then
strMsg = "You must enter a Zip Code"
strTitle = "Zip Code Required"
MsgBox strMsg, vbOKOnly, strTitle
Cancel = True
End If
End Sub

Private Sub State_BeforeUpdate(Cancel As Integer)




End Sub

Thank you for your time and effort I hope someone wil be able to answer my question. I'm very new to VBA and is just trying to learn and work out this problem. Thank you again.
 
This looks suspiciously like a homework assignment, and as I don't want to rob you of the process of discovery, I'll help you just a little.....

Code:
    If IsNull([Postal Code]) Then
        strMsg = "You must enter a Zip Code"
        strTitle = "Zip Code Required"
        MsgBox strMsg, vbOKOnly, strTitle
        Cancel = True
    Else
        If Me.State = "TX" Then
            strFirstTwo = Left([Postal Code], 2)
            'You finish
        End If
    End If

So now I have added an Else statement so that if there is data in the text box, the program checks the value of the state textbox, if the state is "TX" as the question states, we grab the first two numbers from the postal code using the left function (look it up in the help for how it works, right and mid are two other useful string manipulation functions as well). You now have the first two numbers, check your range and finish up!
 
Hello:

I agree with KernelK, this looks like a home-work assignment.

Regards
Mark
 
Thank you for your help KernelK I was confused with the question in the beginning as I was wondering if you had to create an event procedure for the State in order to verify the zip code for TX. I was wondering if there were any good site on where I could learn on how to define range of numbers that I want to verify. We weren't taught this in class. Thank you again for your help.
 
Private Sub Postal_Code_BeforeUpdate(Cancel As Integer)
If IsNull([Postal Code]) Then
strMsg = "You must enter a Zip Code"
strTitle = "Zip Code Required"
MsgBox strMsg, vbOKOnly, strTitle
Cancel = True

Else
If Not Left([Postal Code], 2) = "73" And Not Left([Postal Code], 2) = "74" And Not Left([Postal Code], 2) = "75" And Not Left([Postal Code], 2) = "76" And Not Left([Postal Code], 2) = "77" And Not Left([Postal Code], 2) = "78" And Not Left([Postal Code], 2) = "79" Then
Me.State = "TX"
strMsg = "Wrong Zip Code"
strTitle = "Zip Code Error"
MsgBox strMsg, vbOKOnly, strTitle
End If
End If

I was able to work things out...Thanks again Kernel K...would it be ok to ask you questions if I have problems next time? Take cares
 
Steven,

Overall, the zip code #s should be in a table. There are even some FREE
ones available.

You can clean up the code a bit with something like:

Code:
Select Case Left([Postal Code], 2)
    Case "73", "74", "75", "76", "77", "78", "79" 
      Me.State = "TX"
    Case Else
      strMsg = "Wrong Zip Code"
      strTitle = "Zip Code Error"
      MsgBox strMsg, vbOKOnly, strTitle
    End Select

hth,
Wayne
 

Users who are viewing this thread

Back
Top Bottom