Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error trapping for forms (making sure all required fields are filled)

Status
Not open for further replies.

momon

Vendor
May 22, 2002
42
CA
I was wondering how I would make sure all the fields are filled in... right now i am trying to make an order checklist form. so there is a checkbox beside each item. if the checkbox is selected, then the corresponding field must be filled in.

i read another post which had a similar problem and it suggested putting error trapping in
"Private Sub Form_AfterUpdate()" but this doesn't seem to work.

then i tried to put the necessary error trapping in
the close event like this

Private Sub Form_Close()

If Me![LV Bushing] = -1 And IsNull(Me![Combo413]) Then
MsgBox "Please key in LV Bushing info", vbExclamation, "Missing Info"
Me![LV Bushing].SetFocus
Exit Sub
End If
End Sub

But this just causes an error message to be displayed and the form exits...
IN A NUTSHELLL: How would I make the form not exit until all the required fields are filled in?
 
The easiest way to require that fields are filled in is to set the fields to REQUIRED in the table format.

This will use the default error messages from Access, but it's quick and dirty.

Jim "Get it right the first time, that's the main thing..." [wavey]
 
hmmm i think you misunderstood my question... maybe i should restate it...

Let say that there is a checkbox bound to a field in a table. If that checkbox is selected, another text field (bound to another field in the table) must be filled in.

How would I do this?
 
Try:-

Private Sub Form_Close()

If [LV Bushing] = -1 And IsNull(Combo413) Then
Beep
MsgBox "Please key in LV Bushing info"
DoCmd.GoToControl "Combo413"
Exit Sub
End If
End Sub

and so on.
 
Thanks Gazer44...
but the problem still seems to persist.
the DoCmd.GoToControl works...
The form goes to the required field, but
a split second later, the form exits...

any additional help would be greatly appreciated.
 
Try:-
Private Sub Form_Close()

If [LV Bushing] = -1 And IsNull(Combo413) Then
Beep
MsgBox "Please key in LV Bushing info"
DoCmd.GoToControl "Combo413"
Exit Sub
End If

DoCmd.close

End Sub

(test the LV Bushing field with the above, if ok put in all others with the 'docmd.close' at end.
 
thanks for your help gazer!

uh... with the new code it still seems to want to exit
but i figured out a way to cancel the exit event with
"DoCmd.CancelEvent"
I had to put the code under the "unload form event"

i guess the final code looks something like this

Private Sub Form_Unload(Cancel As Integer)
If [LV Bushing] = -1 And IsNull(Combo413) Then
Beep
MsgBox "Please key in LV Bushing info"
DoCmd.GoToControl "Combo413"
DoCmd.CancelEvent
Exit Sub
End If
End Sub
 
Seems strange because the 'exit sub' line should do the job

Anyway, at least it's sorted !
 
If you use the On Unload event, it passes a parameter named Cancel. If you set Cancel = True within your code, the unload will be cancelled, leaving you in the form. You can also give the focus back to one of the problem controls.

The other thing you might think about doing is using On Exit instead of On Lost Focus on the individual fields because the On Exit event gives you Cancel as a parameter so as above, if you set Cancel = True the code will keep your focus on the text field in question.

Maybe this will help:

Sub CheckBoxFld_AfterUpdate()

If CheckBoxFld
CorrespondingTextBox.setfocus
End If

End Sub

Sub CorrespondingTextBox_Exit(Cancel As Integer)

If Len(CorrespondingTextBox) = 0 Then
msgbox "CorrespondingTextBox is required", _
vbOK + vbCritical
Cancel = True
End If

End Sub

End Sub




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top