OnClose

RycherX

Registered User.
Local time
Yesterday, 18:20
Joined
Dec 17, 2009
Messages
16
In the OnClose event, I added the following code. I want to prompt the user with a Yes/No question. I receive an run-time error '13' on the 3rd line. How do I get rid of this line.

Private Sub Form_Close()
Dim myReply
myReply = MsgBox("Are you sure?", vbYesNo)
If myReply = vbYes Then
DoCmd.Close
End If
End Sub
 
"Are you sure" of what? If you're asking about saving or not saving a record when you close the form, the Form OnClose event is too late. The record has already been saved. If you're trying to do something else let us know.

Next, a message box returns an Integer. You can use the Constant vbYes, which evaluates as an Integer, but you've Dimmed myReply as a Variant. While a Variant is supposed to accept most datatypes, Access does get confused sometimes if you don't explicitly assign a particular datatype to it, such as

Dim myReply
myReply = 1


I'd change

Dim myReply

to

Dim myReply as Integer

since the Error 13 you're getting means "datatype mismatch."
 
When the user presses the "close" button on the form, I want a a msgbox with the following message to pop up: "You will save data and form will close. Do you still want to close?"
Yes - will save and close form. No - will keep the form open.

I keep getting Error 13 "datatype mismatch." message.
 
The code needs to go in the form's UNLOAD event, not the Close event. The close event is too late to stop the close.

Code:
Private Sub Form_Unload(Cancel As Integer)
   If MsgBox("Are you sure?", vbYesNo) = vbNo Then
      Cancel = True
   End If
End Sub
 
Still getting the mismatch issue

The code needs to go in the form's UNLOAD event, not the Close event. The close event is too late to stop the close.

Code:
Private Sub Form_Unload(Cancel As Integer)
   If MsgBox("Are you sure?", vbYesNo) = vbNo Then
      Cancel = True
   End If
End Sub




I used the following code:

Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Are you sure that you want to close this form?", vbYesNo) = vbYes Then
Exit Sub
Else
Cancel = True
End If
End Sub


I still get the error 13 issue.
 
Sounds like possible corruption then. Try importing everything into a brand new database shell.
 
Sounds like possible corruption then. Try importing everything into a brand new database shell.


You are absolutely right. After creating a new db, the routine worked. Hopefully, only the form in the original DB is corrupted. I can simply recreate it.
 

Users who are viewing this thread

Back
Top Bottom