Validating required fields on click of close button

  • Thread starter Thread starter Dirk_Bob
  • Start date Start date
D

Dirk_Bob

What is the easiest way to validate that all required fields have been
populated when clicking a close form button?
 
Use the form's Before Update event and then you can do things like:

Dim blnFlag As Boolean

If IsNull(Me.MyTextBox) Or Me.MyTextBox = "" Then
blnFlag = True
End If

If IsNull(Me.MyComboBox) Then
blnFlag = True
End If

If blnFlag Then
If MsgBox( "You have not filled out the form completely. Do you want to
continue filling it out?, vbQuestion + vbYesNo, "Incomplete Form") = vbNo Then
Cancel = True
Me.Undo
End If


And that will cancel the update and clear the form if they don't want to
continue, but if they do then it will save the record (nothing needed to do
that).

In the command button to close the form just use

DoCmd.Close acForm, Me.Name, acSaveNo

You can use that as written (no changes necessary) as the Me.Name just
references the current form name without you having to type it. And the
acSaveNo part refers to DESIGN CHANGES on the form, not the record. The
record will be saved if they choose Yes from the message box.
--
Bob Larson
Access World Forums Super Moderator
Utter Access VIP
Tutorials at http://www.btabdevelopment.com
__________________________________
If my post was helpful to you, please rate the post.
 
If Len(Me.MyTextBox & vbNullString) = 0 Then
blnFlag = True
End If

is slightly more efficient than

If IsNull(Me.MyTextBox) Or Me.MyTextBox = "" Then
blnFlag = True
End If

(not that you'd ever notice the difference!)
 

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

Back
Top