email notification on success only

Babynod

Board Regular
Joined
Aug 10, 2022
Messages
56
Office Version
  1. 365
Platform
  1. Windows
Hi All,

currently i have my Submit button set to email a notification, the button gets clicked, email is sent to me.
the issue with this is the email is sent event if there is an error, (duplicate validation failed, empty field that must be filled in, etc)
i want the email to only be sent on a successful update from my userform, not every time the button is clicked

VBA Code:
Private Sub cmdSubmit_Click()
    
    Application.EnableEvents = False
    
    
    Dim msgvalue As VbMsgBoxResult
    
    msgvalue = MsgBox("Do you want to save the data?", vbYesNo + vbInformation, "confirmation")
    
    If msgvalue = vbNo Then Exit Sub
    If ValidateEntries() = True Then
    
        Call Submit
        Call reset
    End If
    
    
'email notification

Dim xOutApp As Object
    Dim xOutMail As Object
    On Error Resume Next
    
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
      
             
                  On Error Resume Next
    With xOutMail
        '.To = "MarkP@bapcor.com.au; markjpollock91@gmail.com"
        .To = "MarkP@Bapcor.com.au"
        .CC = ""
        .BCC = ""
        .Subject = "WMS LOGIN REGISTER"
        .Body = "Account Creation Requested By " & (Application.UserName)
        .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing

    Application.EnableEvents = True
    

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,
Untested & bit of a guess but see if this update to your code does what you want

VBA Code:
Private Sub cmdSubmit_Click()
    
    Dim xOutApp     As Object
    Dim xOutMail    As Object
    Dim msgvalue    As VbMsgBoxResult
    
    On Error GoTo myerror
    
    msgvalue = MsgBox("Do you want To save the data?", vbYesNo + vbInformation, "confirmation")
    
    If msgvalue = vbNo Then Exit Sub
    
    If ValidateEntries() = True Then
        Application.EnableEvents = False
        Call Submit
        Call Reset
    Else
        Err.Raise 600, , "duplicate validation failed or empty field must be filled"
    End If
    
    'email notification
    
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    
    With xOutMail
        '.To = "MarkP@bapcor.com.au; markjpollock91@gmail.com"
        .To = "MarkP@Bapcor.com.au"
        .CC = ""
        .BCC = ""
        .Subject = "WMS LOGIN REGISTER"
        .Body = "Account Creation Requested By " & (Application.UserName)
        .Send
    End With
    
myerror:
    Application.EnableEvents = True
    Set xOutMail = Nothing
    Set xOutApp = Nothing

    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
    
End Sub

Dave
 
Upvote 0
Hi Dave,

that seems to work so far, how would i not have the error 600 message box show up though, as currently when a empty field and duplicate entry is found i have a msgbox pop up, this is just doubling up.
 
Upvote 0
Hi Dave,

that seems to work so far, how would i not have the error 600 message box show up though, as currently when a empty field and duplicate entry is found i have a msgbox pop up, this is just doubling up.
i ended up just commenting out the
VBA Code:
If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
and it worked
 
Upvote 0
i ended up just commenting out the
VBA Code:
If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
and it worked
ok for some reason when i use your submit code, it doesnt add my latest data entry to the first row like it used to. it adds it to the last row but im not sure what part of the code does that as i cant see anything in the cmdsubmit that would do that
 
Upvote 0
Hi Dave,

that seems to work so far, how would i not have the error 600 message box show up though, as currently when a empty field and duplicate entry is found i have a msgbox pop up, this is just doubling up.

As I stated, solution untested & a bit of a guess as you had not shared all the codes your procedure is calling in particular, ValidateEntries function.
If this returns false then as per your requirement, no email will be sent & I added msgbox informing user

Dave
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,934
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top