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