Where to put validation code on a form's module?

  • Thread starter Thread starter GZ
  • Start date Start date
G

GZ

Hi.When user input a new record and about to leave, close, or move to
another record, the newly input record is supposed to be saved
automatically. I am just wondering, where can I put validating code to check
if there is anything wrong before the data save to table? If anything wrong,
what property of me.recordset I should use to cancel AddNew or update? I am
asking this because me.recordset. does not give me any choice after the dot.
thanks!
GZ
 
If I am not wrong, I think I've find it. It's Form_BeforeUpdate(Cancel As
Integer) and set Cancel = true if validating failed.
It seems the form takes all jobs from a recordset in Access.
Thanks!
 
GZ said:
Hi.When user input a new record and about to leave, close, or move to
another record, the newly input record is supposed to be saved
automatically. I am just wondering, where can I put validating code
to check if there is anything wrong before the data save to table? If
anything wrong, what property of me.recordset I should use to cancel
AddNew or update? I am asking this because me.recordset. does not
give me any choice after the dot. thanks!
GZ

Hello,

You should place validating code in the BeforeUpdate event. I usually
do my validating by field, not by form, so I am not sure whether it can
be done at the recordset level. Validating by field looks something
like this:

Private Sub [FieldName]_BeforeUpdate(Cancel As Integer)

If Me.[FieldName] Is Incorrect Then
MsgBox ("[FieldName] is not valid.")
[Fieldname].Undo
Cancel = True
End If

End Sub


Hope this helps,
Matt
 
Validation logic can be put in the form's BeforeUpdate
eventhandler. This handler includes a Cancel parameter
that may be set in order to prevent an update should
anything be amiss with the data entered on the form.

Hope This Helps
Gerald Stanley MCSD
 
where can I put validating code to check
if there is anything wrong before the data save to table?

In the Form's BeforeUpdate event. Just set the sub's Cancel property
to true (after issuing a warning to the user of course) to cancel the
update.

You should not need to do anything with the Form's Recordset property
at all... could you explain why you're referencing it? perhaps post
the code?
 
Thanks for everyone's reply!
Now the problem is I cannot get out of Form_BeforeUpdate(). I mean, unless I
change the data to pass the validating, I cannot abandon AddNew record. What
should I do if I don't want add new record and move to previous record or do
other data search after the validating failed?
could you explain why you're referencing it?
I am used to be a VB programmer, that's why I always look for properties of
recordset.

Thanks!
 
GZ said:
Thanks for everyone's reply!
Now the problem is I cannot get out of Form_BeforeUpdate(). I mean, unless I
change the data to pass the validating, I cannot abandon AddNew record. What
should I do if I don't want add new record and move to previous record or do
other data search after the validating failed?

You can add Me.Undo which will cancel the changes entirely, but what you have
now is consistent with almost all other data validation so I don't see any
reason to change it.

For Example:
If you enter text into a number field and try to tab out you get an error. When
you acknowledge the error the invalid entry doesn't automatically go away. It
stays there and you continue to get the error until you do something about it.
Table constraints and validation rules all have this same behavior.
 
Thanks for the reply!
The reason why I prefer have the option to undo is there may be a time the
user does not really want to create a new record but just pressed the button
by mistake.
GZ
 
It was great being able to find this when I needed to, but I am getting a
Runtime error: '424' Object Required with the following code
Private Sub ParticipantName_BeforeUpdate(Cancel As Integer)
cmbxAccount = sAcc
tDate = dDate
[Call] = bCall + 1
If Me.[ParticipantName] Is Incorrect Then
MsgBox ("Particpant is not valid.")
[ParticipantName].Undo
DoCmd.GoToControl ("ParticipantName")
Cancel = True
End If

End Sub

If I Debug and do a Mouse Over Me.[ParticipantName] = "Test Name 1" and
Mouse Over Incorrect = Empty. Most of the time a 424 means I do not have my
field names correct, but since debug is returning a value I know that is not
the case.

What am I doing wrong????

Matthew DeAngelis said:
GZ said:
Hi.When user input a new record and about to leave, close, or move to
another record, the newly input record is supposed to be saved
automatically. I am just wondering, where can I put validating code
to check if there is anything wrong before the data save to table? If
anything wrong, what property of me.recordset I should use to cancel
AddNew or update? I am asking this because me.recordset. does not
give me any choice after the dot. thanks!
GZ

Hello,

You should place validating code in the BeforeUpdate event. I usually
do my validating by field, not by form, so I am not sure whether it can
be done at the recordset level. Validating by field looks something
like this:

Private Sub [FieldName]_BeforeUpdate(Cancel As Integer)

If Me.[FieldName] Is Incorrect Then
MsgBox ("[FieldName] is not valid.")
[Fieldname].Undo
Cancel = True
End If

End Sub


Hope this helps,
Matt
 
Back
Top