Form event triggered when moving to a new record

C

chuck

Which form event is triggered when moving to a new record or when closing
the form ? I'm actually looking for the event that is triggered when data
added to the controls are saved to the underlying table. I tried the "On
Current" event but that does not work.

This is actually a follow up to another message with subject "Detect if a
control is empty when moving to a new record or closing a form" sent on
16-DC at 10:04.

Essentially, I need to capture the complete error description when a 3314
error is encountered. If I don't do any error handling, there is a
predifined error message that come up when I move to a new record. The
error message includes the name of the underlying table's field name that is
empty:

Example:
"The field 'MyTable.MyField' cannot contain a Null value because the
Required property is set to true. Enter a value in this field."


I need to capture this complete error message somehow so that I can extract
the name of the field that is empty. I have not been able to figure out how
to do this. If I use the form's OnError event, all I can get is the error
number and the error description WITHOUT the field name:

Example of procedure for OnError event:

Private Sub Form_Error(DataErr As Integer, Response As Integer).
Error_Number = DataErr
Error_Message = AccessError(DataErr)
endsub

Error_Message returns: "The field '|' cannot contain a Null value because
the Required property is set to true. Enter a value in this field."

As you can see, the field name is missing.

How can I capture the complete error message including the field name in a
variable and using which event would I need to use ?
 
D

Dirk Goldgar

chuck said:
Which form event is triggered when moving to a new record or when
closing the form ? I'm actually looking for the event that is
triggered when data added to the controls are saved to the underlying
table. I tried the "On Current" event but that does not work.

This is actually a follow up to another message with subject "Detect
if a control is empty when moving to a new record or closing a form"
sent on 16-DC at 10:04.

Essentially, I need to capture the complete error description when a
3314 error is encountered. If I don't do any error handling, there
is a predifined error message that come up when I move to a new
record. The error message includes the name of the underlying
table's field name that is empty:

Example:
"The field 'MyTable.MyField' cannot contain a Null value because
the Required property is set to true. Enter a value in this field."


I need to capture this complete error message somehow so that I can
extract the name of the field that is empty. I have not been able to
figure out how to do this. If I use the form's OnError event, all I
can get is the error number and the error description WITHOUT the
field name:

Example of procedure for OnError event:

Private Sub Form_Error(DataErr As Integer, Response As Integer).
Error_Number = DataErr
Error_Message = AccessError(DataErr)
endsub

Error_Message returns: "The field '|' cannot contain a Null value
because the Required property is set to true. Enter a value in this
field."

As you can see, the field name is missing.

How can I capture the complete error message including the field name
in a variable and using which event would I need to use ?

The form's BeforeUpdate event is triggered as a record is about to be
saved, and the AfterUpdate event is triggered immediately after the
record is saved. The best approach to your problem, I believe, is to
put your own code in the form's BeforeUpdate event that validates all
the fields. That code will then know which fields have failed
validation, and can thus cancel the update (by setting the event
procedure's Cancel argument to True) and display an appropriate message.
 
C

chuck

I was able to achieve waht I want with this code in the form's OnError
event:

Private Sub Form_Error(DataErr As Integer, Response As Integer)

' DataErr: The error code returned by the Err object when an error occurs.
You can use the DataErr
' argument with the Error function to map the number to the
corresponding error message.

' Response: The setting determines whether or not an error message is
displayed. The Response argument
' can be one of the following intrinsic constants:
' acDataErrContinue: Ignore the error and continue without
displaying the default
' Microsoft Access error message. You can supply
a custom error
' message in place of the default error message.
' acDataErrDisplay: (Default) Display the default Microsoft Access
error message.

Response = acDataErrContinue

Select Case DataErr
Case 3314
If Nz(Me!box1) = "" Then
MsgBox "The '" + Me!box1_label.Caption + "' field must have a
value."
ElseIf Nz(Me!box2) = "" Then
MsgBox "The '" + Me!box2_label.Caption + "' field must have
a value."
Else
Response = acDataErrDisplay
End If
Case Else
MsgBox "blah blah"
End Select

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top