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

Need to close form, but Unload event causing problems!

Status
Not open for further replies.

ksgirl

Programmer
Jul 9, 2002
12
0
0
US
I have been having problems with the following code. I have one form with two buttons on it: "undo_record" button and an "exit" button. On this form I have code set up in the Unload event to check if certain fields contain a value, before the user can close out of the form. This is the code I have for the missing field verification:

Dim strMsg As String, strTitle As String
strMsg = "Please enter a "
strTitle = " Missing Entry"

If me.Dirty Then
If IsNull(Me![Reason for Modification]) Then
MsgBox strMsg & "Reason for Modification", vbInformation + vbOKOnly, strTitle
Me![Reason for Modification].SetFocus
Cancel = True
End If

If IsNull(Me![txtEquipmentID]) Then
MsgBox strMsg & "Equipment ID", vbInformation + vbOKOnly, strTitle
Me![txtEquipmentID].SetFocus
Cancel = True
End If

If IsNull(Me![Position]) Then
MsgBox strMsg & "Position", vbInformation + vbOKOnly, strTitle
Me![Position].SetFocus
Cancel = True
End If
End If
End Sub

This is the code I have to undo the record and exit the form:

Private Sub undo_record_Click()

Dim strMsg As String, strTitle As String
Dim intResponse As Integer
strMsg = "You Chose to Cancel This Entry." & vbNewLine & vbNewLine & _
" Are You Sure?"
strTitle = "Are You Sure?"
intResponse = MsgBox(strMsg, vbQuestion + vbYesNo, strTitle)

If intResponse = vbYes Then
Me.Undo
DoCmd.Close
End If
End Sub

With what I have, one button works and other doesn't. The "undo_record" button works great. It close the form without saving the record to the table. But if a user is filling in the form and leaves a field blank, and hits the "exit button" the the form closes without checking for the null fields! I've tried everything, but can't figure out why the field validation code won't fire!! Any suggestions?


 

hi there


you could use the
sendkeys "{ESC}"

and this will undo your form if it hasn't been committed otherwise if you are using the built in menus the edit undo option of

DoCmd.DoMenuItem acFormBar, acEditMenu, acundo, , acMenuVer70

hope this helps

jo
 
I'm not using the built in menus.... I just thougt it would be nice to make this form as dummy-proof as possible. I'm designing this for some unusal people, so I know there might be that one instance where someone accidently opens this forms and says to themselves "Hmm...this is what I wanted".... and if they see a button on the bottom of the form to basically "Exit Without Saving" they would be more likely to use it. I thought I had this problem solved, but I can't figure out why my "undo_record" button works and cancels out of the form and the "exit" button just closes the form without checking if the fields are null. Why won't the field validation code fire???? Anyone please!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top