Error Handling for null value

A

Alex

Hello anyone,

I have a form. If a user changes his/her mind while
inserting a new record, and tries to close the form, there
is the message pops up:
"some field can not contain a null value...bla - bla"
Next pop up window"
"can't save the date..would you like to close it? Y/N"

My question is:
Is it possible to handle this error?

I would like to
1. Know the code for this error (could not find and Access
does not give it)
2. Be able to close that form without any prompts to the
user. Just close it.

For this purpose, I did try to use OnError event but it
seems Access just ignores it and pops up the previous
messages.
Is the only way is:
a. to change the null value to some specific.
b. let the access save the record.
c. delete the last record?

Thanks for your time and advices.
Alex
 
A

Andrew Smith

This will suppress the default messages for these two errors. It may be
better to give the user the option to continue editing as it could be rather
irritating to find that a record they thought had been entered was not
actually there!

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Select Case DataErr
Case 3314
Response = acDataErrContinue
Case 2169
Response = acDataErrContinue
Case Else
End Select
End Sub

To get the Form_Error event to do anything you have to inspect the value of
DataErr which is the what the error number would be if it were to be
triggered. The easy way to find the error number is to put in the line
"MsgBox DataErr" or "Debug.Print DataErr" in the Form_Error code.
 
J

John Spencer (MVP)

Are you attempting to trap the error in the form's On Error Event? That is
where you should be doing it in this case. I don't know the exact error number
you will get, but you can grab that the first time your code runs.

Private Sub Form_Error(DataErr As Integer, Response As Integer)
MsgBox DataErr & " Occurred"


End Sub


However, you could add a CANCEL button to your form with code to undo the
changes or if it is a new record cancel the new record.

In the click event's code, you would need the following (as a mininum)

Private Sub btnCancel_Click()

If Me.Dirty then
Me.Undo

'Optionally the next line would close the form
DoCmd.Close acForm, Me.Name
End if

End Sub
 
G

Guest

Thank you guys,

Your explanations have been just what a doctor would
prescribed..:)

Respectfully,
Alex
 
D

Dragon

Hi, Alex,

On your VBA editor adding a auto_close command line after you understand the
following;

"On Error Resume Next" -- causes execution to continue with the statement
immediately following the statement that caused the run-time error, or with
the statement immediately following the most recent call out of the
procedure containing the "On Error Resume Next" statement. This statement
allows execution to continue despite a run-time error. You can place the
error-handling routine where the error would occur, rather than transferring
control to another location within the procedure. An "On Error Resume Next"
statement becomes inactive when another procedure is called, so you should
execute an "On Error Resume Next" statement in each called routine if you
want inline error handling within that routine.

Note The "On Error Resume Next" construct may be preferable to "On Error
GoTo" when handling errors generated during access to other objects.
Checking Err after each interaction with an object removes ambiguity about
which object was accessed by the code. You can be sure which object placed
the error code in Err.Number, as well as which object originally generated
the error (the object specified in Err.Source).

"On Error GoTo 0" disables error handling in the current procedure. It
doesn't specify line 0 as the start of the error-handling code, even if the
procedure contains a line numbered 0. Without an "On Error GoTo 0"
statement, an error handler is automatically disabled when a procedure is
exited.

To prevent error-handling code from running when no error has occurred,
place an "Exit Sub", "Exit Function", or "Exit Property" statement
immediately before the error-handling routine, as in the following fragment:
----------------------------------------------------------
Sub InitializeMatrix(Var1, Var2, Var3, Var4)
On Error GoTo ErrorHandler
. . .
Exit Sub
ErrorHandler:
. . .
Resume Next
End Sub
-----------------------------------------------------------
Here, the error-handling code follows the "Exit Sub" statement and precedes
the "End Sub" statement to separate it from the procedure flow.
Error-handling code can be placed anywhere in a procedure.

And you may use the property in your problemed form by
got to "Data Bank" in your problemed form; right click that "Form"; on "Menu
Bar" click "View" then "design view mode"; find the using entry block and
right click; in pop down menu, you may change the data configuration or the
Mash to limit the current user to enter the right data.

The above only my experience.

Bye-bye
Regards
et1
 

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