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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help validate on required fieds

Status
Not open for further replies.

khicon73

MIS
Jan 10, 2008
36
I have 2 forms, one for data entry and one for edit. Data Entry form is working fine, all required fields needed to fill in for record to be saved. However, on the edit form...when someone deleted some values on the required fields and hit the closed button on the menu (tool bar), it just closed the form and validate message won't work...

For example:

Private Sub Form_Close()
If IsNull(Me.txtText1) Or (Me.txtText1) = "" Then
MsgBox "[Text1] Cannot be empty. Please Re-enter!"
Me.txtText1.SetFocus

ElseIf IsNull(Me.txtText2) Or (Me.txtText2) = "" Then
MsgBox "[Text2] Cannot be empty. Please Re-enter!"
Me.txtText2.SetFocus

ElseIf IsNull(Me.txtText3) Or (Me.txtText3) = "" Then
MsgBox "[Text3] Cannot be empty. Please Re-enter!"
Me.txtText3.SetFocus
Else
DoCmd.Close
End If
End Sub


Even though I hit OK when the message pop up, but the form still close.

Please help, thanks.
 
Use the BeforeUpdate event procedure to enforce all your validation rules.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV, thanks for your response...

Even I place the code under BeforeUpdate, it's still doing the same when close form using close button on the menu (tool bar)....(Close Button on form set equal to Yes)

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtText1) Or (Me.txtText1) = "" Then
MsgBox "[Text1] Cannot be empty. Please Re-enter!"
Me.txtText1.SetFocus

ElseIf IsNull(Me.txtText2) Or (Me.txtText2) = "" Then
MsgBox "[Text2] Cannot be empty. Please Re-enter!"
Me.txtText2.SetFocus

ElseIf IsNull(Me.txtText3) Or (Me.txtText3) = "" Then
MsgBox "[Text3] Cannot be empty. Please Re-enter!"
Me.txtText3.SetFocus
Else
DoCmd.Close
End If

End sub

 
What abiut setting Cancel to True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top