How do I ensure all fields are entered in form before saving

007stuart

Registered User.
Local time
Today, 18:48
Joined
Mar 20, 2009
Messages
23
Hi

I have a mix of combo boxes and text boxes on a form that records service calls.

I propose using the before update event to check that each Combo and Text box is completed but am unsure of the correct coding to achieve this.

Can anybody assist

Thanks in advance
 
I'm pretty new to Access, but have you just set the fields in the table to required?
 
In the before update event of your form, use something along the lines of:-

Code:
If IsNull(Me.yourcontrolnamehere) Then
MsgBox "your message here"
Cancel = True
End If

And repeat for each control that cannot contain a null value
 
I usually set focus back on the offending control as well. And if you're going to be checking for more than one control, you need to add a line to exit the sub after each check. Otherwise the messageboxes for each check will pop one after the other without giving the user time to correct their error(s).
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(txtItemA.Value) Then
  MsgBox "You Must Enter a Number in Item A"
  Cancel = True
  txtItemA.SetFocus
  Exit Sub
End If

If IsNull(txtItemB.Value) Then
  MsgBox "You Must Enter a Number in Item B"
  Cancel = True
  txtItemB.SetFocus
  Exit Sub
End If
End Sub
 
Thanks for your replies.

To Missingling I like your code and understand what it is looking to do.

Do I place a before update event on each control that requires data?
 
No! It has to go in the Form_BeforeUpdate event as my code demonstrated!

The control's BeforeUpdate event will only fire if the user enters data into it, which would make the code a moot point. Tying it to any other event of the control is also useless; no event will fire if the user simply ignores it and never enters the control!

Some rules of thumb for validation:

To validate that data entered is correct in format, value range, etc, use the ControlName_BeforeUpdate event.

To validate that one or more controls actually has data, use the Form_BeforeUpdate event.

To do validation of one control in relation to another control, i.e. StartDate must be before EndDate, etc. use Form_BeforeUpdate.
 
missinglinq: Why dont you post this code in a sticky with the examples you have mentioned..... It is such a common question!
 

Users who are viewing this thread

Back
Top Bottom