VBA code to verify required fields have been entered

G

Guest

This is for a project and my VBA code knowledge is minimal although I think I
can figure it out if pointed in the right direction. I already have a save
button on the form with the code to save the data. Would I put this here? I
know I need a message box, I just don't know where and how to begin. Any help
would be great. Thanks!
 
A

Allen Browne

Use the BeforeUpdate event of the *form* (not controls) to check if your
control IsNull().

Private Sub Form_BeforeUpate(Cancel As Integer)
If IsNull(Me.[SomeControl]) Then
Cancel = True
MsgBox "Hey: you forgot SomeControl."
End If
End Sub
 
G

Guest

No, it would go in the Form's Before Update event. Here is an example where
No Last Name was entered:

If IsNull(Me.txtLastName) Then
MsgBox "No Last Name Entered"
Cancel = True
Me.txtLastName.SetFocus
End If

If no last name was entered, then the value of the text box will be Null.
The Message Box tells the user. Cancel = True cancels the update. Then
txtLastName is given the focus so the user can enter it.

As to your Save button. It really is of no use. There are other events
that occur that will save the record without you explicitly doing so. If you
move to another record or close the form, the record is updated, for example.
 

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