How can I get a "Are you sure you want to quit YES/NO" prompt on an access button - ms-access-2007

I have a quit button in Access 2007 called cmdQuit -
How I can have it so it brings up a warning prompt along the lines of
are you sure you want to quit? YES/NO
So that it doesn't just automatically quit the log form?
I have this as my command:
Private Sub cmdQuit_Click()
If MsgBox("Are you sure that you want to close this form?", vbYesNo) = vbYes Then
Exit Sub
Else
Cancel = True
End If
End Sub
So the No part of the prompt works but the Yes and then quit part does not
Thanks
Dan

There is nothing to cancel, so all you need is this:
Private Sub cmdQuit_Click()
If MsgBox("Are You Sure You Want To Close This Logger?", vbQuestion + vbYesNo, PraiseLogger) = vbYes Then
DoCmd.Quit
End Sub

I've solved it now - it was as follows:
Private Sub cmdQuit_Click()
If MsgBox("Are you sure that you want to close this form?", vbYesNo) = vbYes Then
DoCmd.Quit
Else
Cancel = True
End If
End Sub
Had Exit Sub instead of the Quit command
Thanks
Dan

There was also another way of doing it which made it a bit cleaner and didn't give me the "Microsoft Access" part at the top of the prompt
Private Sub cmdQuit_Click()
If MsgBox("Are You Sure You Want To Close This Logger?", vbYesNoCancel, PraiseLogger) = vbYes Then
DoCmd.Quit
Else: Cancel = True
End If
End Sub
Thanks
Dan

Related

How to execute code when user closes form?

I want a msg box to appear if certain fields are empty on close.
Private Sub Close_Click()
If IsNull(Me.startDate) Then
MsgBox “You are in Phase 1”
End If
End Sub
I want the pop up to appear when user clicks the exit x. I tried OnClose. Right now the code is attached to and works via a button.
I’d like the code to be executed when the user closes the form.
Edit New Code:
Private Sub Exit_Click ()
If IsNull(Me.startDate) Or IsNull(Me.stepOneA) Or IsNull(Me.stepOneB) Then
MsgBox “You are in Step I”
DoCmd.Close
ElseIf IsNull(Me.stepTwoA) or IsNull(Me.stepTwoB) or IsNull(stepTwoC)
MsgBox “You are in Step II”
DoCmd.Close
Else
DoCmd.Close
EndIf
End Sub
You can use the UnLoad event for this:
Private Sub Form_Unload(Cancel As Integer)
Cancel = IsNull(Me.startDate)
If Cancel = True Then
MsgBox "You are in Phase 1."
End If
End Sub

Pop up message before closing a file

I need a pop-up message to remind the person to check all the information before closing the presentation, with two choices (to cancel or to close it anyway).
Private Sub PPTApp_PresentationBeforeClose(Cancel As Boolean)
If MsgBox("Confirmo que as informações desta apresentação estão atualizadas no SAP", _
vbQuestion + vbYesNo) = vbNo Then
Cancel = True
End If
End Sub
Try This.
Private Sub PPTApp_PresentationBeforeClose(Cancel As Boolean)
Dim i As Long
i = MsgBox("your message will appear here", vbQuestion + vbYesNo)
If i = vbNo Then
'do this command
Else
' do this command
End If
End Sub

How do I run a macro on change to a field, but not on original entry of that field?

I created a Projects database which includes a form with fields for staff to complete on startup of a project.
The first field, 'ClientCode' is a compulsory dropdown list (Combobox) of Clients.
To avoid accidental changes to this field, I added 'On Change' code to display a warning message:
Private Sub ComboClientCode_Change()
If MsgBox("Are you sure you want to change the client?", vbQuestion + vbYesNo) = vbNo Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If
End Sub
It works, however I don't want it to run when someone starts a new record i.e. selects the client for the first time.
How do I make this message only show if it is a change to the original entry?
I tried moving it to 'AfterUpdate' but it does the same thing.
Right, I haven't tested any of the code below so let me know if it works for you. I propose 2 different solutions, and depending on if the logic works, the solution will work.
Solution 1:
Using the change event, you check the combobox value, and if it is blank then run the code. My concern with this is that when the event handler fires there will only be a value in there because the user would have changed the value to call the event handler. Nonetheless, the code would look like something below.
Private Sub ComboClientCode_Change()
If Me.ComboClientCode.Value <> "" Then
If MsgBox("Are you sure you want to change the client?", vbQuestion + vbYesNo) = vbNo Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If
End If
End Sub
Solution 2:
With this solution the logic seems a bit more sound than the above method. What will happen is you will set the default value of the combobox using the UserForm_Initialize() event handler. Then, when your user selects from the dropdown list it will set the value to that variable, and will prevent the unnecessary pop up. See below code. Your userform module should look like the below.
Option Explicit
Dim sClientCode As String
Private Sub UserForm_Initialize()
sClientCode = Me.ComboClientCode.Value
End Sub
Private Sub ComboClientCode_Change()
If sClientCode <> "" Then
If MsgBox("Are you sure you want to change the client?", vbQuestion + vbYesNo) = vbNo Then
'set the value now
sClientCode = Me.ComboClientCode.Value
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If
End If
End Sub
You could change one of the "irrelevant" properties of the combobox the first time it gets changed.
Private Sub ComboClientCode_Change()
If ComboClientCode.ColumnHeads = False Then
ComboClientCode.ColumnHeads = True
Else
If MsgBox("Are you sure you want to change the client?", vbQuestion + vbYesNo) = vbNo Then
'DoCmd.RunCommand acCmdUndo
Debug.Print "DoCmd.RunCommand acCmdUndo"
Else
Exit Sub
End If
End If
End Sub
Should work for this type:
Listed properties of above selected type:
You can run this to make the existing one change if it has a value:
Sub Switch()
If ComboClientCode.Value <> "" Then
ComboClientCode.ColumnHeads = True
Else
ComboClientCode.ColumnHeads = False
End If
End Sub

Updating/adding entry to table only when question is answered?

I'm setting up a database in MS Access 2013, and want to ask the user a yes/no if they want to save or discard their non-saved record or edit before navigating away from the current record in my Access form.
The user should not be met with the question if pressing either the "add record" or "save" buttons.
Can someone point to where my problematic code is / or what I need?
Also, I'm new to Access, so please be gentle.
I have tried a few different guides or other answers around the web, but haven't gotten exactly to where I want to be.
My code is as such (cbotxt_Change and Form_Load relate to other parts of the form)
Private blnGood As Boolean
Option Compare Database
Private Sub cbotxt_Change()
Me.txt1.Value = Me.test1.Column(2)
Me.txt2.Value = Me.test1.Column(3)
Me.txt3.Value = Me.test1.Column(4)
End Sub
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub button_addRecord_Click()
blnGood = True
DoCmd.GoToRecord , , acNewRec
blnGood = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not blnGood Then
strMsg = "want to abort?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "Yes") = vbYes Then
Me.Undo
Else
Yes = True
End If
End If
End Sub
Using the code above, "want to abort?" is asked whenever the user is attempting to navigate away from the current record + when "add record" or "save" buttons are pressed. If the user answers "No", then the entry will save and the action in question be performed, except the "add record" button, which only seems to save now, not add a new record.
Seems a little convoluted to me. Try with:
Private Sub button_addRecord_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
strMsg = "want to abort?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "New Entry") = vbYes Then
Cancel = True
Me.Undo ' or let the user press Escape.
End If
End Sub

Creating a msg on close when sheets are unprotected in Excel

Hi I'm trying to make it so that excel will look if two specific sheets are protected on closure and then if they are not I want a message box to come up warning of this. Currently i've got this far with VBA.
Sub Worksheet_BeforeClose(Cancel As Boolean)
If Sheets("Dashboard Page").ProtectContents = True And Sheets("Tracker Sheet").ProtectContents = True Then
MsgBox "Protected"
ElseIf
MsgBox("Workbook is not protected please protect before closing", _
vbQuestion + vbOKOnly) = vbOKOnly Then
Cancel = True
End If
End If
End Sub
if anyone could help out that would be great.
Thanks
Edit: I'm now having issues with changing this to just an OK button that cancels the close. changes are above. It will just close if the OK button is clicked
The problem is you can not open a sub inside another one, but you can call it.
Sub ProtectMsg_BeforeClose()
If Sheets("Dashboard Page").ProtectContents = True And Sheets("Tracker Sheet").ProtectContents = True Then
MsgBox "Protected"
Else
Workbook_BeforeClose
End If
End Sub
Sub Workbook_BeforeClose()
If MsgBox("Workbook is not protected please protect before closing", _
vbQuestion + vbYesNo) = vbNo Then
Cancel = True
End If
End Sub
Try if it helps you.