how do i customise an error message with regards to an input mask

G

Guest

I want to create a custom error message for when data is entered into a field
with an input mask in the incorrect format eg for if i typed in 123abc into a
field that had an input mask of 000000. I have tried entering the phrase i
want to appear in the validation text part of the table design, but it wont
accept it. Any ideas anyone?
 
G

Guest

The purpose of an input mask is that it constrains what can be entered into a
control, so with the example you give it would not be possible to enter
non-numeric characters as the 0 character in a mask allows only numbers. If
you want to inform the user why nothing happens when they try to enter a
non-numeric character (other than a Backspace or ESC for editing) in this
example you could put the following in the control's KeyPress event procedure:

Private Sub Text21_KeyPress(KeyAscii As Integer)

Const conMESSAGE = "Only numbers can be entered into this field."
Const conBACKSPACE = 8
Const conESC = 27

If Not IsNumeric(Chr(KeyAscii)) Then
If KeyAscii = conBACKSPACE Or KeyAscii = conESC Then
' do nothing
Else
MsgBox conMESSAGE, vbExclamation, "Invalid Operation"
End If
End If

End Sub

With more complex masks you'd have to do a little bit more to ascertain the
position in the control and respond accordingly. Say the mask was 000LLL,
enforcing three numbers then three letters you could use:

Const conMESSAGENUM = _
"Only numbers can be entered into the first three positions in this
field."
Const conMESSAGELET = _
"Only letters can be entered into the last three positions in this field."
Const conMESSAGEEND = _
"Only 6 characters can be entered into this field."
Const conBACKSPACE = 8
Const conESC = 27
Dim ctrl As Control

Set ctrl = Me.ActiveControl

If KeyAscii = conBACKSPACE Or KeyAscii = conESC Then
' do nothing
Else
If ctrl.SelStart = 6 Then
MsgBox conMESSAGEEND, vbExclamation, "Invalid Operation"
Else
If ctrl.SelStart < 3 Then
If Not IsNumeric(Chr(KeyAscii)) Then
MsgBox conMESSAGENUM, vbExclamation, "Invalid Operation"
End If
Else
If KeyAscii < 65 Or KeyAscii > 122 _
Or (KeyAscii > 90 And KeyAscii < 97) Then
MsgBox conMESSAGELET, vbExclamation, "Invalid Operation"
End If
End If
End If
End If
 
W

Wayne Morgan

An input mask doesn't give any error when an incorrect item is entered. It
just won't let you enter the incorrect item. If the users are confused, you
may want to change the "mask character" to indicate what type of character
should be entered.

Example:
000000;;#

Will cause a # sign to appear in each space while editing the field,
indicating a number should be entered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top