Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Custom Input Mask Error Message

Posted on 2004-11-03
20
Medium Priority
?
592 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:cmcgregor
  • 10
  • 4
  • 3
  • +1
20 Comments
 
LVL 26

Expert Comment

by:dannywareham
ID: 12485369
You can have the exit button clear the entry automatically...
Add:

me.mypostcodefield.value = ""

before the close command
0
 
LVL 9

Expert Comment

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

peter
0
 

Author Comment

by:cmcgregor
ID: 12485464
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 26

Expert Comment

by:dannywareham
ID: 12485509
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
 

Author Comment

by:cmcgregor
ID: 12485554
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
 

Author Comment

by:cmcgregor
ID: 12485563
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
 

Author Comment

by:cmcgregor
ID: 12485588
I cant rely on the user knowing that they have to clear the text box before they can exit.
0
 
LVL 26

Expert Comment

by:dannywareham
ID: 12485648
OK. Lose the input mask from your textbox....

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

0
 

Author Comment

by:cmcgregor
ID: 12485673
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
 
LVL 58

Expert Comment

by:harfang
ID: 12502754
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
 

Author Comment

by:cmcgregor
ID: 12503671
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
 
LVL 58

Expert Comment

by:harfang
ID: 12504013
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
 

Author Comment

by:cmcgregor
ID: 12504052
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
 
LVL 58

Expert Comment

by:harfang
ID: 12504317
> "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
 

Author Comment

by:cmcgregor
ID: 12504707
For a postal code it is required though. It hase to be alpha # alpha # alpha #
0
 
LVL 58

Expert Comment

by:harfang
ID: 12506705
Both "L0L\ 0L0" and "?9?\ 9?9" do that. In the first case, entry is required, in the second, it isn't.
Cheers!
0
 

Author Comment

by:cmcgregor
ID: 12506756
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
 
LVL 9

Expert Comment

by:Bat17
ID: 12506832
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
 

Author Comment

by:cmcgregor
ID: 12507116
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
 
LVL 9

Accepted Solution

by:
Bat17 earned 150 total points
ID: 12507330
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question