Msgbox yes or no always delete all changes - ms-access

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).

Related

FormLoad() does not fire

Building forms in Microsoft Access 2013. As the title says,
Public Sub FormLoad()
'Do Stuff!
End Sub
does not fire. I am completely mystified. Extensive Googling has shown that other people have had this problem, but it was caused by subforms and other complexities that I am not dealing with.
This form's full code is below:
Option Compare Database
Private Sub FormLoad()
Me.Label6.Caption = PrevForm
MsgBox "Main Form/Form Load has Fired!"
'Set previous form to the last form open and reset "this form" to ME
PrevForm = ThisForm
ThisForm = Me.Name
End Sub
Private Sub CmdMngDbCreateEvent_Click()
PrevForm = Me.Name
DoCmd.OpenForm "CreateEvent"
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Private Sub CmdMngDbManageEvents_Click()
PrevForm = Me.Name
DoCmd.OpenForm "ManageEvents"
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Private Sub CmdMngDbUploadRoster_Click()
PrevForm = Me.Name
DoCmd.OpenForm "UploadRoster"
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Private Sub CmdMngDbManagePersonnel_Click()
PrevForm = Me.Name
DoCmd.OpenForm "ManagePersonnel"
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Private Sub CmdMngDbMainMenu_Click()
PrevForm = Me.Name
DoCmd.OpenForm "Home"
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Obviously, this is an exceedingly simple form. The only objects on the form are the objects referenced in the code, and everything other than FormLoad() works as intended.
Any ideas? I'm utterly stumped.
It's
Private Sub Form_Load()
not
Private Sub FormLoad()
Always build Event procedures via the property sheet or the dropdowns in the VBE, don't type them by hand.

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

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

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.

Prompt before close

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