Forced Entry into a form field

P

porterlp

I have created a form and am trying to force entry in each field. Each
field is not to be null.

I tried entering is not null in the validation rule - but that only
works if the individual enters information then tries to remove it.

I entered the following in the before event

Private Sub Pass_Fail_BeforeUpdate(Cancel As Integer)

If IsNull(Me!Pass_Fail) Then
MsgBox "Entry required"
Cancel = True
End If


End Sub

it still allows the form to be closed without data.

Please let me know what vb script would be the correct one.

Thanks!
 
D

Dirk Goldgar

I have created a form and am trying to force entry in each field.
Each field is not to be null.

I tried entering is not null in the validation rule - but that only
works if the individual enters information then tries to remove it.

I entered the following in the before event

Private Sub Pass_Fail_BeforeUpdate(Cancel As Integer)

If IsNull(Me!Pass_Fail) Then
MsgBox "Entry required"
Cancel = True
End If


End Sub

it still allows the form to be closed without data.

Please let me know what vb script would be the correct one.

Thanks!

You can make the fields Required in the table definition. That will
keep a record from being saved with Null values in the fields. To catch
the problem before the form writes to the table, you can put code in the
form's BeforeUpdate event, in which you check for Null in any of those
required fields, and cancel the update (with the line "Cancel = True")
if you find Null in any of them.

It's not really practical to do this validation in events related to the
controls themselves, because such events only fire if the user actually
visits the control. But the user could use the mouse to move from one
control to another without entering any of the intervening controls.
 
G

Guest

So how do you fix it when so they can't enter information into a subform
until the main form's data has been filled out?
 
D

Dirk Goldgar

Stacey said:
So how do you fix it when so they can't enter information into a
subform until the main form's data has been filled out?

There are two parts to this: catching the situation when the main form
hasn't been filled out at all, and catching the situation when the main
form has only been partially filled out. The latter situation is
handled automatically by whatever logic you have in the main form's
BeforeUpdate event to check for required fields. If the main form is
"dirty" (that is, if any of its fields have been modified), Access will
force its record to be saved when you try to change focus to te subform.
If it can't be saved -- because it is missing some required fields --
Access won't let the focus go to the subform at all. So that's not a
problem.

The only case, then, that requires any special handling, is when the
user tries to fill out the subform before entering anything on the main
form. That will cause an error about a required field being Null when
the subform record tries to save. What I usually do is either

(A) Disable the subform in the main form's Current event, if
Me.NewRecord = True, and only enable it in the form's Dirty event.

or else

(B) Put code in the Enter event of the subform control (on the main
form) that checks to see if the main form appears to have been filled
out at least partially. If it has not, the code displays an error
message and sets the focus back to a control on the main form.
 

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