How to distinguish btwn same error number for diff fields in a For

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

MS Access 2000, Windows XP
======================
Hi,

I'm trying to generate more user-friendly/less technical custom error
messages in a form. I've figured out to code this in the form's "On Error"
section using DataErr.

But, the issue I'm grappling with is that the same error can be generated by
more than one field on the form. In that case, how can I include the field
name in the error message I generate?

As an example, I have a Text control bound to ProjectDate, and another text
control bound to ProjectName. Both of these are required fields in the table,
and I'd like to keep it that way.

If I leave the ProjectDate text control blank, I get error # 3314 (generated
when a required field has no value). I get the same error number when I leave
the ProjectName text control blank.

So, how do I figure out which control on the form generated the error so
that I can include appropriate error message ("Please type in a Date" versus
"Please enter a name for the Project") and set focus to the control that
caused the error?

Thanks for any help/pointers.

-Amit
 
You may be able to tell the control name from:
Form.ActiveControl.Name
or the field name from:
Form.ActiveControl.ControlSource

However, the error could fire even if the control does not have focus (e.g.
when there is an attempt to save the record.)

The simplest solution is to open the table in design view, and set the
Required property of the field to No. Instead, set its Validation Rule to:
Is Not Null
and you can then set whatever message you want in its Validation Text
property.
 
Hi Allen:

I tried what you suggested, and it works great!!

I did have to let go of my previous way of thinking of making a field in a
table required by setting its Required property to "Yes".

Thanks again.

-Amit
 
Hi Allen:

I noticed a behavior after making the changes you suggested. When I leave a
field blank, after I get the message to fill in that field, the focus does
not always switch to the control that needs attention, and I have to click to
that control to type in the value. From what I can tell, the focus still
remains on the button.

Is this a "feature"?? Is there a way to set focus to the control?

Thanks.

-Amit
 
You can SetFocus to the control, but you need the logic to figure out which
control gave the error. Provided your Validation Text specifies what control
you are talking about, that's probably something the user can do.
 
Back
Top