Prompt before close - ms-access

Before the user closes the form I want to prompt with a confirmation box.
I am not sure how to do this.
I have tried the code below and it prompts the user but upon clicking no it closes the form anyway:
Private Sub Form_Close()
If MsgBox("Test", vbYesNo + vbExclamation, "Confirm close") = vbYes Then
Else
Cancel = True
End If
End Sub

You can't cancel the close event but you can cancel the unload event
Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Test", vbYesNo + vbExclamation, "Confirm close") <> vbYes Then
Cancel = True
End If
End Sub

Related

Access VBA - prevent closing form

I have a code in Before_Update event, for saving changes. For this I use Msgbox with YesNoCancel prompt, but I cannot prevent closing form when I hit Cancel button. Here is a short sample of my code:
Option Compare Database
Public SomeVariable As Integer
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("There has been done some changes. You wish to save these changes ?", vbQuestion + vbYesNoCancel, "Save changes") = vbYes Then
'do nothing and Access saves automatically
ElseIf vbNo Then
DoCmd.RunCommand acCmdUndo
ElseIf vbCancel Then
SomeVariable = 1
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
If SomeVariable = 1 Then
SomeVariable=0
Cancel = True
End If
End Sub
Any way to fix this ?
You must save the return value from MsgBox, then proceeed accordingly:
Dim vbAnswer
vbAnswer= MsgBox("There has been done some changes. You wish to save these changes ?", vbQuestion + vbYesNoCancel, "Save changes")
if vbAnswer = vbYes Then
'do nothing and Access saves automatically
ElseIf vbAnswer= vbNo Then
....
ElseIf vbAnswer= vbCancel Then

Msgbox yes or no always delete all changes

I have button on my form that should exit the form. When this button is clicked, a message box prompts you to "save changes?" with a yes or no. Unfortunately, regardless of whether 'yes' or 'no' is clicked the code below does not save changes:
Private Sub Prekid_Click()
If Me.Dirty Then If MsgBox("Da li želite da saèuvate promene?", vbYesNo) = vbYes Then Upisivanje_Click
DoCmd.RunCommand acCmdUndo
Forms!Pregled!LicencaList.Form.Requery
DoCmd.Close acForm, Me.Name
End Sub
Doing If ... Then ... in a single line can be confusing, as you have just demonstrated. :)
You code should be:
Private Sub Prekid_Click()
If Me.Dirty Then
If MsgBox("Da li želite da saèuvate promene?", vbYesNo) = vbYes Then
Call Upisivanje_Click
Else ' No case
DoCmd.RunCommand acCmdUndo
End If
End If
Forms!Pregled!LicencaList.Form.Requery
DoCmd.Close acForm, Me.Name
End Sub
acCmdUndo should only run in the Else case (if No was clicked).

How can I remove the pesky "The runCommand action was canceled" dialog box after a "cancel = true" statement

On the Form_beforeupdate() event of my form "Alias" I have this...
If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then
MsgBox "You must enter a contact!", vbokayonly, "Contact"
Cancel = True
End If
Anytime this cancels a runCommand code such as...
DoCmd.RunCommand acCmdSaveRecord
a dialog appears telling me that it was canceled. Is there a way to suppress those dialogs?
You can add an error handler to trap and ignore error 2501, which is triggered when the form's Before Update event cancels DoCmd.RunCommand acCmdSaveRecord
The following example is from my form which has a command button to save the current record. It suppresses that "RunCommand action was canceled" message as I think you wish.
Since your form's code apparently calls DoCmd.RunCommand acCmdSaveRecord from multiple locations, replace each of those calls with SaveRecord as I did in cmdSave_Click().
Option Compare Database
Option Explicit ' <-- NEVER troubleshoot VBA code without this!!!
Private Sub cmdSave_Click()
'DoCmd.RunCommand acCmdSaveRecord
SaveRecord
End Sub
Private Sub SaveRecord()
Dim strMsg As String
On Error GoTo ErrorHandler
DoCmd.RunCommand acCmdSaveRecord
ExitHere:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501 ' The RunCommand action was canceled.
' do nothing --> just ignore the error
Case Else
' notify user about any other error
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure SaveRecord"
MsgBox strMsg
End Select
Resume ExitHere
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtBlock_start.Value) Then
MsgBox "You must enter a start time!", vbOKOnly, "Start Time"
Cancel = True
End If
End Sub
I ended up doing this on a 'Save and Open the Receipts form' button.
Private Sub btnSaveOpenReceipts_Click()
'''''SaveRecord'''''
If (Me.Dirty) Then
SaveRecord
End If
'''''OpenReciepts'''''
If (Me.Dirty) Then 'unsure it saved
Exit Sub
Else
'blah blah blah
End If
End Sub

Access 2010 VBA: Why doesn't this sequence of form openings and closings work?

I have a form "Form1" with a command button that when clicked, hides itself, and then opens another form "Form2" as follows:
Private Sub Command1_Click()
Me.Visible = False
DoCmd.OpenForm "Form2"
End Sub
The Form_Open subroutine of Form2 contains a check for errors. If an error is found, I want Form2 to display an error, close Form1, and then close itself:
Private Sub Form_Open(Cancel As Integer)
AnError = 'check for an error
If Not AnError Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
DoCmd.Close acForm, "Form1"
DoCmd.Close
End If
End Sub
I've tried numerous variations on this scheme for opening and closing the forms, and all fail in different ways. This particular one seems to do everything except actually close Form2 after the error message is displayed.
Once again I'm baffled about the way Access 2010 VBA handles the opening and closing of forms; the reference information at MSDN is atrocious. Thanks for any help.
You are close. - this code will also close Form2:
Private Sub Form_Open(Cancel As Integer)
'AnError = 'check for an error
If Not True Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
DoCmd.Close acForm, "Form1"
DoCmd.Close acForm, Me.Name 'Me.Name is used for illustration purposes, you can also use "Form2"
End If
End Sub
I've always seen this done via the UnLoad keyword
Try this,
If Not AnError Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
Unload Form1
'Unload Me
'OR since you can cancel this form loading
Cancel = True
End If
Try changing to the Me object:
Private Sub Form_Open(Cancel As Integer)
AnError = 'check for an error
If Not AnError Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
DoCmd.Close acForm, "Form1"
Me.Close
' or possibly Unload Me
End If
End Sub

ms access form closing ask save yesnocancel

I have put the VBA in Unload Form Event in ms access 2010
Private Sub Form_Unload(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer
' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
' Check the user's response.
If iResponse = vbYes Then
' Undo the change.
DoCmd.RunCommand acCmdSave
End If
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo
End If
If iResponse = vbCancel Then
' Undo the change
Cancel = True
End If
End Sub
If data is changed then the above code is working fine, then yes to save & close, No to undo & close and cancel to cancel event and remain on the form
but when the data is unchanged then Yes button is working fine, but NO button do not close the form
Where I m mistaking ?
Your question is too vague because you simply state "NO button do not close the form."
You can do:
DoCmd.Close
See here:
Private Sub cmdCloseForm_Click()
On Error GoTo Err_cmdCloseForm_Click
DoCmd.RunCommand acCmdUndo 'OR Me.Undo - test which works best for your situation
DoCmd.Close
Exit_cmdCloseForm_Click:
Exit Sub
Err_cmdCloseForm_Click:
MsgBox Err.Description
Resume Exit_cmdCloseForm_Click
End Sub
On Me.Undo from Allen Browne:
Me.Undo cancels the edits in a specific form, so that it is no longer dirty.
Once the form is no longer dirty, no further undo is possible, because it
has reached the desired state. However, Me. represents the form that is in focus, so your form must have focus to perform the Me.Undo command.