Link to home
Start Free TrialLog in
Avatar of cmcgregor
cmcgregor

asked on

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
Avatar of dannywareham
dannywareham
Flag of United Kingdom of Great Britain and Northern Ireland image

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

me.mypostcodefield.value = ""

before the close command
Avatar of Bat17
Bat17

Are you saying that you want to be able to override the input mask and let the user add an invalid code?

peter
Avatar of cmcgregor

ASKER

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
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...

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
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
I cant rely on the user knowing that they have to clear the text box before they can exit.
OK. Lose the input mask from your textbox....

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

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.
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
Unfortunately that doesnt help. When the user clicks exit the message should come up that their entry is invalid, the program should just exit
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!
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
> "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
For a postal code it is required though. It hase to be alpha # alpha # alpha #
Both "L0L\ 0L0" and "?9?\ 9?9" do that. In the first case, entry is required, in the second, it isn't.
Cheers!
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Bat17
Bat17

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial