Pop up message before closing a file - vba

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

Related

BeforeUpdate with a "new record" button

I have an Access form users sometimes forget to save. I put in a BeforeUpdate trigger to pop up message reminding users to save or cancel before taking any action.
I found this code on the net and it works for everything except my "New Record" button.
As far as I know Me.Dirty should do the trick.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
On Error GoTo Err_BeforeUpdate
' The Dirty property is True if the record has been changed.
If Me.Dirty Then
' Prompt to confirm the save operation.
If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
"Save Record") = vbNo Then
Me.Undo
End If
End If
Exit_BeforeUpdate:
Exit Sub
Err_BeforeUpdate:
MsgBox Err.Number & " " & Err.Description
Resume Exit_BeforeUpdate
End Sub
The code for the new record
Private Sub new_Click()
njno = NewJobNbr()
Job.Value = njno
RnK.Value = ""
Date_Requested.Value = ""
Est_Time.Value = ""
Originator.Value = ""
Date_Required.Value = ""
Description.Value = ""
Reason_for_Request.Value = ""
Comments.Value = ""
Priority_Tasks.Value = ""
TName.Value = ""
Required.Value = ""
Costing.Value = ""
Completed.Value = ""
Date_Completed.Value = ""
End Sub
I don't really understand what njno is. If the textboxes are bound to the form's RecordSource then.
On form Current event use:
Private Sub Form_Current ()
If Me.NewRecord Then
Job.Value = njno
End if
End Sub
Then on the button Click event use:
Private Sub new_Click()
DoCmd.GoToRecod,,acNewRec
End sub

warning message doesn't work for user-form

My content in sheet2 was hidden and only after user form popped up and enter the password before the content can unhide. However with the warning message, even after I select "yes to terminate" my sheet content still unhides.
Expected outcome:
After the user selects "yes to terminate" on the warning message, it is suppose to drop the user form. Can anyone help me solve it?
Under worksheet(sheet2):
Private Sub Worksheet_Activate()
If Sheets("Reference").Columns("A:K").EntireColumn.Hidden = True Then
Password.Show
Call unhide
Else
Sheets("Reference").Columns("A:K").EntireColumn.Hidden = False
End If
End Sub
Under userform (Password):
Private Sub Submit_Click()
If Me.Pword.Value = "123" Then
Unload Me
Call unhide
Else
Me.Hide
Retry = MsgBox("The password is incorrect. Do you wish to try again?", _
vbYesNo, "Retry?")
Select Case Retry
Case Is = vbYes
Me.Pword.Value = ""
Me.Pword.SetFocus
Me.Show
Case Is = vbNo
Unload Me
End Select
End If
End Sub
Private Sub cmdExit_Click()
If ExitAsk = vbYes Then Unload Password
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
If Not ExitAsk = vbYes Then Cancel = True
End If
End Sub
Private Function ExitAsk() As VbMsgBoxResult
Dim Smsg As String
Smsg = "Do you really want to exit? Click Yes to Quit or No to Continue."
ExitAsk = MsgBox(Smsg, vbYesNo + vbDefaultButton2 + vbQuestion, "Exit!")
End Function

How to check sending email address before sending?

I use multiple accounts in Outlook. I want to give a warning box if sending from an address I should not be sending from.
I have two addresses that I should never send from (they are receive only accounts).
This example is almost what I am looking for.
Example - Checking the "To" address.
I believe a string comparison (StrComp) and Item.SenderEmailAddress is what I need.
Here is my attempt for giving a warning for a single email address (bad.email#gmail.com).
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
' use lower case for the address
' LCase converts all addresses in the To field to lower case
If StrComp((Item.SenderEmailAddress), "bad.email#gmail.com") Then
Exit Sub
End If
Prompt$ = "You sending this from " & Item.SenderEmailAddress & ". Are you sure you want to send it?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End Sub
Ideally I would to check two or more addresses with the same code. Something like in the example should work.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
Select Case LCase(Item.To)
Case "alias#domain.com", "alias2#domain3.com", "alias3#domain3.com"
Item.Send
Case Else
Prompt$ = "You are not sending this to " & Item.To & ". Are you sure you want to send the Mail?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End Select
End Sub
Also, where do I place the code to ensure that it is constantly running and ready?
I think it should be MailItem.SendUsingAccount Property (Outlook)
Which returns or sets an Account object that represents the account under which the MailItem is to be sent. Read/write, also see MailItem.SendUsingAccount Property.
Example
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Prompt As String
Prompt = "Are you sure you want to send from 0m3r#Email.com?"
If Item.SendUsingAccount = "0m3r#Email.com" Then
If MsgBox(Prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End If
End Sub
Try using SendUsingAccount - as noted, SenderEmailAddress doesn't exist on unsent mail.
Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim sendAddress As String
Dim prompt As String
' Check Send_from name
sendAddress = Item.SendUsingAccount.SmtpAddress
Select Case sendAddress
Case "alias#domain.com", "alias2#domain3.com", "alias3#domain3.com"
' send
Case Else
prompt = "You are currently sending this email from " & sendAddress & ". Are you sure you want to proceed?"
If MsgBox(prompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground + vbDefaultButton2, "Check Address") = vbNo Then
Cancel = True
End If
End Select
End Sub
You should paste this code in ThisOutlookSession, under Microsoft Outlook Objects in the VBA Editor.

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

Access/VBA: "Run-time error 2169. You can't save this record at this time"

Using: Access 2013 with ADO connection to SQL Server back-end database
A form in my Access database is dynamically bound at runtime to the results of a SELECT stored-procedure from SQL Server, and allows the user to make changes to the record.
It has 2 buttons: Save and Cancel.
It is shown as a pop-up, modal, dialog form, and it has a (Windows) Close button at the top right corner.
I've put VBA code to ask the user whether he wants to Save, Ignore or Cancel the close action.
But there are problems and it gives the aforementioned error if Cancel is clicked. There are also other problems, like, after the error occurs once, then any further commands (Save or Cancel or closing the form) don't work - I think this is because the VBA interpreter has halted due to the earlier error. Another complication is that arises - I now need to end the MS-Access process from Windows Task Manager, doing this and then restarting the database and then opening this form will give an error and the form won't load. When the form is then opened in Design mode, I can see the connection string for the form is saved in the Form's Record Source property (this happens only sometimes), and which looks something like this:
{ ? = call dbo.tbBeneficiary_S(?) }.
Here is my code:
Dim CancelCloseFlag As Boolean
Dim SavePrompt As Boolean
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim a As Integer
If SavePrompt Then
a = MsgBox("Do you want to save changes?", vbQuestion + vbYesNoCancel, "Changes made")
Select Case a
Case vbNo:
Me.Undo
CancelCloseFlag = False
Case vbYes:
'do nothing; it will save the changes
CancelCloseFlag = False
Case vbCancel:
Cancel = True
CancelCloseFlag = True
End Select
End If
End Sub
Private Sub Form_Dirty(Cancel As Integer)
SavePrompt = True
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2169 Then
Response = acDataErrContinue
End If
End Sub
Private Sub Form_Load()
LoadBeneficiaryDetails
End Sub
Private Sub Form_Unload(Cancel As Integer)
If CancelCloseFlag Then
Cancel = True
End If
End Sub
Private Sub btCancel_Click()
If Me.Dirty Then
SavePrompt = True
End If
DoCmd.Close
End Sub
Private Sub btSave_Click()
SavePrompt = False
DoCmd.Close
End Sub
I'm stuck and would like to know how others go about this issue? Basically I want to offer the user the choice Save, Ignore, Cancel when the user attempts to close the form with either Cancel button or the (Windows) close button. If the user chooses Cancel, then it should just return to the form without changing or undoing any changes to the data. The solution may be simple but it escapes my overworked mind.
Thanks in advance!
Please try the following code - I tested against all six scenarios and the proper action is taken.
Option Compare Database
Option Explicit
Dim blnAction As Integer
Dim blnBeenThereDoneThat As Boolean
Private Sub Form_BeforeUpdate(Cancel As Integer)
If blnBeenThereDoneThat = True Then Exit Sub
blnBeenThereDoneThat = True
blnAction = MsgBox("Do you want to save changes?", vbQuestion + vbYesNoCancel, "Changes made")
Select Case blnAction
Case vbNo:
Me.Undo
Case vbYes:
'do nothing; it will save the changes
Case vbCancel:
Cancel = True
End Select
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2169 Then
Response = acDataErrContinue
End If
End Sub
Private Sub Form_Load()
LoadBeneficiaryDetails
End Sub
Private Sub Form_Unload(Cancel As Integer)
If blnAction = vbCancel Then
blnBeenThereDoneThat = False
Cancel = True
End If
End Sub
Private Sub btCancel_Click()
If Me.Dirty Then
Form_BeforeUpdate (0)
End If
If blnAction = vbCancel Then
blnBeenThereDoneThat = False
Exit Sub
ElseIf blnAction = vbYes Then
DoCmd.Close
Else
DoCmd.Close
End If
End Sub
Private Sub btSave_Click()
If Me.Dirty Then
Form_BeforeUpdate (0)
End If
If blnAction = vbCancel Then
Exit Sub
Else
DoCmd.Close
End If
End Sub