Creating a msg on close when sheets are unprotected in Excel - vba

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.

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

Access VBA Preventing form record entry on close

I am working with Access Database VBA.
I have noticed if a user has filled a few of the text boxes in and then clicks the windows close button, the form logs that into the records.
I am wondering what is the best way to prevent the form from entering the uncompleted record on close?
There were a few sites pointing to placing a code in the beforeupdate function.
This is what I have tried.
Private Sub frmRecLog_BeforeUpdate(Cancel As Integer)
DoCmd.SetWarnings False
Me.Undo
Cancel = True
Exit Sub
End Sub
This code does not work at all for me.. I tried my best, haha.
Anything helps.
So, what you need is to insert a command button Save. If user do not hit on Save then it will not save any records. They will get a warning that data is not saved. You need declare a private boolean variable and write codes to save and warning. So full code will be like below.
Option Compare Database
Option Explicit
Private blnSaveRecord As Boolean
Private Sub cmdSave_Click()
blnSaveRecord = True
DoCmd.RunCommand (acCmdSaveRecord)
blnSaveRecord = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not blnSaveRecord Then
Cancel = True
strMsg = "Please save the record.," & _
vbNewLine & "or press ESC from keyboard to cancel the operation."
Call MsgBox(strMsg, vbInformation, "Save Record")
'Me.Undo 'You can set undo option here if you do not want to press ESC.
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

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

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

My vba has stopped working again?

This time I was making option boxes so I put in the code with the speech marks around the answer then I tried using it but it just wouldn't work. The code I put in was:
Private Sub OptionButton1_Click()
If OptionButton1.Value = "Stores all the components" Then
MsgBox "That is correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Sorry, that is not right. Try again"
End If
End Sub
Please can someone help me?
I think you are trying this
If OptionButton1.Caption = "Stores all the components"
or This
If OptionButton1.Value = True Then
An optionButton in VBA has a Value of 0 (unchecked) or -1 (checked). If you are looking for what the optionbutton says, I believe you want .Caption instead of .Value
EDIT:
After looking at your code again, I see a fundamental flaw. You are assigning this to just option click, so if the first button does not have the correct value, then nothing will happen. I think what you need is a subroutine like this
Private Sub checkAnswer (answer as string)
dim expecTedAnswer as string
expectedAnswer = "The correct answer"
if answer = expectedanswer then
msgbox "Correct"
else
msgbox "Incorrect"
end if
End sub
Then in the option click, just call the Sub
CheckAnswer(OptionButton1.Caption)
Note that this is extremely ugly (you would want to create an easily usable calling method and way of setting the correctAnswer), but it should give you something to go on.
A little prettier perhaps ...
Each OptionButton click event calls CheckAnswer:
Private Sub OptionButton1_Click()
If CheckAnswer(Me.OptionButton1, "Correct answer") Then
MsgBox "Good user. GOOD!"
Else
MsgBox "Bad user! BAD! Go stand in the corner."
End If
End Sub
Private Sub OptionButton2_Click()
If CheckAnswer(Me.OptionButton2, "Correct answer") Then
MsgBox "Good user. GOOD!"
Else
MsgBox "Bad user! BAD! Go stand in the corner."
End If
End Sub
Function CheckAnswer(oCtl As Object, sCorrectAnswer As String) As Boolean
If UCase(oCtl.Caption) = UCase(sCorrectAnswer) Then
CheckAnswer = True
Else
CheckAnswer = False
End If
End Function