Creating a custom error message for Input Mask violation (1 Viewer)

Coolkid4321

New member
Local time
Today, 19:19
Joined
Mar 29, 2021
Messages
2
Hey can someone help me create an error message for when an input mask is violated in a form? I want the error to appear specifically if an input mask is violated.

Thanks
 

Jon

Access World Site Owner
Staff member
Local time
Today, 16:19
Joined
Sep 28, 1999
Messages
7,304
Welcome to the forums! We are the most active Microsoft Access community on the internet by far, with posts going back over 20 years!

To get started, I highly recommend you read the post below. It contains important information for all new users to this forum. https://www.access-programmers.co.uk/forums/threads/new-member-read-me-first.223250/ We look forward to having you around here, learning stuff and having fun!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:19
Joined
May 7, 2009
Messages
19,169
Use the control's BeforeUpdate event:

If Not ([txtbox] like "theMaskHere") then
'Msgbox here
End if
 

Coolkid4321

New member
Local time
Today, 19:19
Joined
Mar 29, 2021
Messages
2
Use the control's BeforeUpdate event:

If Not ([txtbox] like "theMaskHere") then
'Msgbox here
End if
The input mask that im using makes it so the input has to be 5 numeric digits '00000', the code works but only if the input is less than 5, if its above 5 then it shows the same default error "The value you entered isn't valid for this field. For example, you..."
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:19
Joined
Oct 29, 2018
Messages
21,358
Hi. Welcome to AWF!

I have moved your thread out of the Introduction Forum. You can start a new thread there to introduce yourself.

Cheers!
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 16:19
Joined
Sep 12, 2006
Messages
15,613
I would set a flag of some sort on entry to that field then intercept an error with the form error event.

Then this sort of thing for the error handler. So see what error numbers you get, and then use a case select to replace the system error for those error numbers with one of your own choosing.

eg I entered letters in a date field, and got error 3313.

Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
     if specialflag then
         MsgBox "error: " & DataErr & "   Desc: " & AccessError(DataErr)
         Response = acDataErrContinue
    end if
End Sub
 

Isaac

Lifelong Learner
Local time
Today, 09:19
Joined
Mar 14, 2017
Messages
8,738
You also might be able to use the form's beforeupdate event to test the actual pattern of the data
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:19
Joined
May 21, 2018
Messages
8,463
If using the before update then I think this will return true if you have 5 and only 5 digits

Code:
Public Function PatternFound(StringToCheck As Variant) As Boolean
    Dim Pattern As String
    Dim re As Object
 
  'Pattern is: 5 numbers

    If Not IsNull(StringToCheck) Then
        Pattern = "^[0-9]{5}"
        Set re = CreateObject("VBScript.RegExp")
        re.Pattern = Pattern
        re.Global = False
        re.IgnoreCase = True
        PatternFound = re.test(StringToCheck)
    End If
End Function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:19
Joined
May 7, 2009
Messages
19,169
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2279 Then
    MsgBox "The value you entered isn't valid for this field. For example, you..." & vbCrLf & vbCrLf & _
        "55555 (only digit and 5 char long)."
    Response = acDataErrContinue
End If
End Sub
 

Users who are viewing this thread

Top Bottom