I disagree on putting an exit sub line in each conditional. Good practice
says only have have one exit point for a procedure, and often in a
BeforeUpdate procedure there's other stuff besides this going on... Exit Sub
in each conditional will give no opportunity to clean up the procedure.
Op is looking to make sure that ALL fields are filled in, not just a
specific field. If it were a specific field then we could easily set the
focus to that field (op may want to do this anyway), but if you'll notice in
my code, there's only one message box at the bottom that displays a msg
stating that all fields need to be filled in. Otherwise, if the message box
we're to specifically tell what field needs to be filled in, the would then
think that only this field needs to be filled in, and would probably be a bit
aggrevated when he got this field specific message for every empty field.
An alternative would be to contencate field names into a msg string that is
to be displayed at the end of the procedure. Or, rather than using Exit Sub
in each conditional, use Goto Exit_Procedure to maintain a single exit point.
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Proc
Dim bValid As Boolean
Dim sMsg As String
bValid = True
sMsg = "The following fields require values:" & vbCrLf
If Len(Nz(Me.Control1, "")) = 0 Then
bValid = False
sMsg = sMsg & Me.Control1.Name & vbCrLf
'Goto Exit_Proc
End If
If Len(Nz(Me.Control2, "")) = 0 Then
bValid = False
sMsg = sMsg & Me.Control2.Name & vbCrLf
'Goto Exit_Proc
End If
....
....
Exit_Proc:
If Not bValid Then
If Len(sMsg) <> 0 Then MsgBox sMsg
Cancel = True
End If
Exit Sub
Err_Proc:
bValid = False
sMsg = ""
MsgBox Err.Number & " " & Err.Description
Resume Exit_Proc
End Sub
In any case, I don't mean to shoot down your ideas (they are good ones,
depending on how Op wants to interact with his users), but I personally
wouldn't use an Exit Sub except at the exit point of a procedure as indicated
above.
But, as some people say, there's ALWAYS more than one way to skin a cat! :-)
Hopefully between everyone's various ideas Op can conjure up something that
will work.
--
Jack Leach
www.tristatemachine.com
"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
"Linq Adams via AccessMonster.com" wrote:
> If you're doing multiple Validations, you also need to add the line
>
> Exit Sub
>
> and then set focus to the control in question.
>
> Private Sub Form_BeforeUpdate(Cancel As Integer)
>
> If Len(Nz(txtFieldA, "")) = 0 Then
> MsgBox "You Must Enter Data in Field A"
> Cancel = True
> txtFieldA.SetFocus
> Exit Sub
> End If
>
> If Len(Nz(txtFieldB, "")) = 0 Then
> MsgBox "You Must Enter Data in Field B"
> Cancel = True
> txtFieldB.SetFocus
> Exit Sub
> End If
>
> End Sub
>
> If the
>
> Exit Sub
>
> line is omitted and more than one field is empty, Access will display both
> messages without giving the user the chance to enter data in the first empty
> filed.
>
> Likewise, if multiple fields are empty, the SetFocus allows Access to go to
> the control the current warning is talking about.
>
> --
> There's ALWAYS more than one way to skin a cat!
>
> Answers/posts based on Access 2000/2003
>
> Message posted via AccessMonster.com
> http://www.accessmonster.com/Uwe/For...dules/200907/1
>
>