VBA code to verify phone numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SLMQC
    New Member
    • Jan 2013
    • 2

    VBA code to verify phone numbers

    I am new to VBA (Access), and I'm trying write a proceedure to verify Phone field values for a particular City. Example: if the City is Cleveland, the two 'valid' area codes are 216 and 440.

    I am using the VBA statement below, and the message block is popping up even when the first three numbers meet the 216 or 440 criteria. Can anyone help me identify how to correct?



    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        Dim PhoneFirstThree As Integer
        If Not IsNull([City]) And Not IsNull([Phone]) Then
            PhoneFirstThree = Val(Left([Phone], 3))
            Select Case [City]
                Case "Cleveland"
                    If PhoneFirstThree <> 216 Or PhoneFirstThree <> 440 Then
                    DoCmd.CancelEvent
                    MsgBox "Cleveland phone numbers must start with 216 or 440"
                    Me.Undo
                    [Phone].SetFocus
                End If
            End Select
        End If
    End Sub
    Last edited by TheSmileyCoder; Jan 19 '13, 10:17 PM. Reason: When posting code, please wrap it on [code]...[/code] to improve readability.
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    Use AND instead of OR at your if statement
    Code:
    If PhoneFirstThree <> 216 AND PhoneFirstThree <> 440 Then

    Comment

    • SLMQC
      New Member
      • Jan 2013
      • 2

      #3
      Thank you Anas! Tested and confirmed.

      Comment

      • Anas Mosaad
        New Member
        • Jan 2013
        • 185

        #4
        Welcome, we are here to help. Good luck.

        Comment

        Working...