Custom Input Mask Error Message

I have put in a custom input error message for when the user does not enter a valid input mask. The code is as follows...
In the forms properties onerror  I have...

Private Sub Form_Error(DataErr As Integer, Response As Integer)

   Const INPUTMASK_VIOLATION = 2279
   If DataErr = INPUTMASK_VIOLATION Then
      MsgBox "Sorry that is not a valid postal code.", , "Input Error"
      Response = acDataErrContinue
   End If
End Sub

This works however lets say the user decides I just want to exit the form. When they press the exit button the error message pops up and the user cannot exit unless they erase the entry in the text box of have a properly formatted input.

Any ideas how to let the user exit
cmcgregorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dannywarehamCommented:
You can have the exit button clear the entry automatically...
Add:

me.mypostcodefield.value = ""

before the close command
0
Bat17Commented:
Are you saying that you want to be able to override the input mask and let the user add an invalid code?

peter
0
cmcgregorAuthor Commented:
dannywareham: I cant even get to that point, when I click the exit button I still get that error. The button doesnt even click. It wont let me click anything until the value is correct.
 Bat17: If they press exit I want them to be able to override the error message and just leave the program
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

dannywarehamCommented:
Right...

What's happening is that the input mask tries to calculate the field when it attempts to lose focus.
Hence, when you try and click any other control on the form (including the exit button), it throws you an error.

What you can do is clear the input mask.

On the exit button, have a piece of code check the validity of the entry - if ok, save, if not msgbox and force the user to change it...

0
cmcgregorAuthor Commented:
Here is my exit button code. This is not data entry so I do not need the user to save the record, and the field doest not have to be cleared. It is a form that opens and the user types a value into a text box and then hits a "Find it" button, The value the user typed is then used as a parameter for a query.

Private Sub FindExit_Click()
End Sub
On Error GoTo Err_FindExit_Click

    Me.Input_fsaldu.Value = ""
    DoCmd.Quit

Exit_FindExit_Click:
    Exit Sub

Err_FindExit_Click:
    MsgBox Err.Description
    Resume Exit_FindExit_Click
   
End Sub
0
cmcgregorAuthor Commented:
Your right about the focus. It therefore wont let me change the focus so therefore the exit button wont work until it is cleared or correct format
0
cmcgregorAuthor Commented:
I cant rely on the user knowing that they have to clear the text box before they can exit.
0
dannywarehamCommented:
OK. Lose the input mask from your textbox....

What is the code you have for your "Find" button?

0
cmcgregorAuthor Commented:
Private Sub Command2_Click()
   
   DoCmd.OpenForm "SelectPostalCode", acViewNormal, acEdit
   DoCmd.Close acForm, "FindPostalCode"


End Sub

This is the input mask in using  >L0L\ 0L0;;_

The code takes the value entered and enters it as the parameter an second for opens to show the result.
0
harfangCommented:
You can update the error handler somewhat.
Everything else will fail (changing the code of the buttons, etc) because they will never be executed. The only improvement I can think of is to clear the control from the error handler, like so:


Private Sub Form_Error(DataErr As Integer, Response As Integer)

   Const INPUTMASK_VIOLATION = 2279
   
   If DataErr = INPUTMASK_VIOLATION Then
      If MsgBox("Sorry that is not a valid postal code." & vbCrLf _
         & "Clear entry?" _
         , vbOKCancel _
         , "Input Error") = vbOK Then
            Screen.ActiveControl.Undo
      End If
      Response = acDataErrContinue
   End If

End Sub


Most users will click OK whenever they see it, so that they will be able to "Exit" in a hurry :)

Good Luck
0
cmcgregorAuthor Commented:
Unfortunately that doesnt help. When the user clicks exit the message should come up that their entry is invalid, the program should just exit
0
harfangCommented:
Of course it helps.

Anyway, to stress the point once more, the user CANNOT CLICK ANYWHERE. The behavior will be exaclty the same if he uses Tab to go to the next field of if he clicks anywhere outside of the control, including any "Exit" button. You have no way of knowing why and how the users tries to exit the control with incomplete data.

The only place the user can click on the form is the [X] at the top right. It still generates the error, but then it proceeds with another informing that the exit will cause the record not to be saved...

Your choices are:
1) leave the message as it is (maybe adding some help in the form "press ESC to undo your editing")
2) use a message with a choice to cancel the edit (default yes or default no, your call)
3) always undo the control when it is incomplete.

Solution 3) would be (minimal version):

Private Sub Form_Error(DataErr As Integer, Response As Integer)

   Const INPUTMASK_VIOLATION = 2279
   
   If DataErr = INPUTMASK_VIOLATION Then
      Screen.ActiveControl.Undo
      Response = acDataErrContinue
   End If

End Sub

Nothing you can do with the Exit button will change the behavior because it will not be "clicked" before the "input mask violation error" has been handeld.

Good Luck!
0
cmcgregorAuthor Commented:
dannywareham had a good idea to remove the input mask and put the validation in the find button that way it will only check the input mask when that button is pressed. He hasnt responded with his solution yet though
0
harfangCommented:
> "idea to remove the input mask"... almost

Your input mask (>L0L\ 0L0;;_) reads:
* convert to upper case
* require letter, digit, letter (space) digit, letter, digit
* space not stored in the field
* use "_" as placeholder

If you use something less dramatic: >?9?\ 9?9;;_
* same mask, but nothing is *required*

Let's call the postal code control Me.txtPostal, then, basically, the postal code is well-formed when you have Len(Me.txtPostal) = 6, because the space is not stored in the database. You can use that as validation criteria:

Private Sub txtPostal_AfterUpdate()
   If Len(Nz(Me.txtPostal)) = 6 Then Me.txtPostal.Value = Null
End Sub

Input masks...

Good Luck
0
cmcgregorAuthor Commented:
For a postal code it is required though. It hase to be alpha # alpha # alpha #
0
harfangCommented:
Both "L0L\ 0L0" and "?9?\ 9?9" do that. In the first case, entry is required, in the second, it isn't.
Cheers!
0
cmcgregorAuthor Commented:
i want entry to be required. i dont want the user to enter two digits and then press find, i want a 6 character entry
0
Bat17Commented:
Thats what Harfang's afterUpdate code does! thou I suspect he meant

Private Sub txtPostal_AfterUpdate()
   If Len(Nz(Me.txtPostal)) <> 6 Then Me.txtPostal.Value = Null
End Sub

Nothing but 6 Chrs accepted

Peter
0
cmcgregorAuthor Commented:
It still gives the error message when they want to exit, and the it clears the text box and then they can exit. I want them to just be able to hit exit and leave no matter what is in the text box
0
Bat17Commented:
You probably need a differnt approach then.
Try setting the form as popup/modal with dialog borders, then they will have to use the close button to close the form rather than the X in the corner.
Rather than using Len(Nz(Me.txtPostal)) in the after update event add it to your find button.

   
 if Len(Nz(Me.txtPostal)) <> 6 then
  msgbox "Invalid Postcode, Please Correct",vbOK,"Error"
exit sub
end if
  DoCmd.OpenForm "SelectPostalCode", acViewNormal, acEdit
   DoCmd.Close acForm, "FindPostalCode"


Air code! but it should be close enough to give you the idea

peter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.