On Error Event on a Form (different error message on specific fields) (1 Viewer)

anski

Registered User.
Local time
Today, 17:52
Joined
Sep 5, 2009
Messages
93
I have a form with 2 fields. If the data type entered is not the correct one entered for these fields, I would like to have a different message appear depending on which field trigerred the error. When I use the On Error event of the form, it applies to the whole form. How should I do this?
 

isladogs

MVP / VIP
Local time
Today, 09:52
Joined
Jan 14, 2017
Messages
18,186
Why not just use the built-in error messages provided by Access for this purpose?
 

Micron

AWF VIP
Local time
Today, 05:52
Joined
Oct 20, 2018
Messages
3,476
Access messages can be a bit cryptic.
Why not the validation text property in the table design itself?
 

June7

AWF VIP
Local time
Today, 01:52
Joined
Mar 9, 2014
Messages
5,425
Could use ValidationRule and ValidationText properties.

Otherwise, VBA in control's BeforeUpdate event.
 

anski

Registered User.
Local time
Today, 17:52
Joined
Sep 5, 2009
Messages
93
Why not just use the built-in error messages provided by Access for this purpose?
I want the messages to be short and simple. (Enter a date. Enter a number. Data entered should be a number...)
 

isladogs

MVP / VIP
Local time
Today, 09:52
Joined
Jan 14, 2017
Messages
18,186
I want the messages to be short and simple. (Enter a date. Enter a number. Data entered should be a number...)
Fair enough. Use validation rules/text as suggested by both Micron & June
 

anski

Registered User.
Local time
Today, 17:52
Joined
Sep 5, 2009
Messages
93
Access messages can be a bit cryptic.
Why not the validation text property in the table design itself?
The Validation Rule/Text does not meet my requirement. The field is a number. What I could use that for is specify a range or start with a specific number, etc. If the user enters "ABC" in the number field, Access will not use what I indicated in the Validation Text. It will show the standard message: "The value you entered isn't valid for this field. For example, you may have entered text in a numeric field..."

What I did instead to avoid entering of non-numbers is used the Input Mask property. Typing a letter in the number field is not possible at all.
 
Last edited:

Micron

AWF VIP
Local time
Today, 05:52
Joined
Oct 20, 2018
Messages
3,476
If your users complain too much about the mask (masks can be annoying at times) your validation event should be either BeforeUpdate on the form, or BeforeUpdate on each control that needs it. To use the form error event, you'd have to set the Response parameter of the event to acDataErrContinue so that Access doesn't present the built in message. While that will allow you to suppress that message, it will also allow the user to continue with the bad data.

Whether to present an error for each control on an individual basis (control BeforeUpdate) or all of them at once (form BeforeUpdate) is mostly personal and situational preference I'd say.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:52
Joined
Feb 28, 2001
Messages
27,001
Your original question was how to use the form's OnError event to do a value-specific error. The simple answer: Why?

Because there is such a thing as using a control's LostFocus event as a way to validate that it contains data in the correct format. And since each such event is specific to a single control, you have the ability to tailor your messages to the field/control in question.

More typing to enter the various routines, but they can be brief and will have absolute specificity.
 

missinglinq

AWF VIP
Local time
Today, 05:52
Joined
Jun 20, 2003
Messages
6,423
This kind of this has worked for me for years:
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)

If DataErr = 2113 Then 'Data entered does not match datatype of Control Source
   
  If Screen.ActiveControl.Name = "YourNumericField" Then
   Response = MsgBox("Data For This Field Must Be Entered as A Number!", vbExclamation, "Format is Not Correct for a Number!")
   Response = acDataErrContinue
  End If
   
  If Screen.ActiveControl.Name = "YourDateField" Then
   Response = MsgBox("Field Must Contain a Valid Date!", vbExclamation, "Data is Not a Valid Date!")
   Response = acDataErrContinue
  End If

End If

End Sub

Linq ;0)>
 

Micron

AWF VIP
Local time
Today, 05:52
Joined
Oct 20, 2018
Messages
3,476
Not that it really matters but why assign the value of an OK button to Response rather than just using the message box function, then assigning acDataErrContinue to Response? I'm just wondering if the OK value needs to be assigned.
 

Users who are viewing this thread

Top Bottom