One thing you can almost be sure of is that your end users will have their own ideas on how they want to fill in a Record...and it frequently won't be the way you would do so!
Often the 1st and 3rd Controls' data will come from one hard-copy form, while the 2nd and 4th Controls' data may come from another hard-copy sheet...and so they'll input the 1st and 3rd Controls then the 2nd ad 4th.
Some users like using the keyboard for navigation...while others are more mouse-oriented....so you'll probably keep your users happier by letting them decide what order to enter the data in.
So you really need to concentrate on validating that all, in your case, Controls have been populated, using the Form_BeforeUpdate event as @June7 suggested.
There are a number of ways to approach data validation...for a few Controls, you can do this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Nz(Me.Control1,"") = "" Then
MsgBox "Control1 Must Not Be Left Blank!"
Cancel = True
Control1.SetFocus
Exit Sub
End If
If Nz(Me.Control2, "") = "" Then
MsgBox "Control2 Must Not Be Left Blank!"
Cancel = True
Control2.SetFocus
Exit Sub
End If
End Sub
and so forth.
You could loop through some or all Controls and do the same thing.
If the number of Controls makes the above too time consuming, given your situation, this will loop through all Textboxes and all Comboboxes and check that they're populated:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
Dim CName As String
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
If Nz(ctl, "") = "" Then
CName = ctl.Controls(0).Caption
MsgBox "Following field is required: " & vbCrLf & vbCrLf & CName
Cancel = True
ctl.SetFocus
Exit Sub
End If
End Select
Next ctl
End Sub
You could also use the Tag Property to mark certain Controls, and then loop through all Controls but only check on/address the status of these 'marked' Controls.
To set the Tag Property for multiple Controls, all at once:
- Go into Form Design View
- Holding down <Shift> and Left clicking on each Control in turn.
- Go to Properties – Other and enter Marked in the Tag Property (just like that, no Quotation Marks)
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
Dim CName As String
For Each ctl In Me.Controls
If ctl.Tag = "marked" Then
If Nz(ctl, "") = "" Then
CName = ctl.Controls(0).Caption
MsgBox "Following field is required: " & vbCrLf & vbCrLf & CName
Cancel = True
ctl.SetFocus
Exit Sub
End If
End If
Next ctl
End Sub
Notice that if Validation in the Form_BeforeUpdate event fails, you set Cancel = True, which aborts the Save, and tell the user where they've gone wrong, setting Focus back to the offending Control.
You could simply mark the Fields as 'Required,' either at the Table or Form level, but most experienced developers avoid this, as the error messages Access gives the users, when Required Fields are left empty can be, shall we say, less than helpful! Validating thru the Form_BeforeUpdate event allows you to pop up custom messages that will actually mean something to your users.
Linq ;0)>
The problem with making anything foolproof...is that fools are so darn ingenious!
All posts/responses based on Access 2003/2007