Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MsgBox before sending email

Posted on 2010-08-16
14
Medium Priority
?
342 Views
Last Modified: 2013-11-28
Hello Experts,

Currently, I have a button where once it clicked, it will automatically send an email without opening the outlook. Some of the information will be extract from the form such as customer email, name and etc.

Below is part of my code:

Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
     
    On Error Resume Next
    With OutMail
        .to = Me.Email
        .CC = ""

What I am trying to do now is, to set up a MsgBox when any of the data is not entered, such as if the email or customer name has blank records, then a MsgBox will appear.

Thanks!
0
Comment
Question by:rutlandict
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33445157
Before the .send command, check all the variables


dim sVar(), j
sVar=array("sTo","sCC","sSubj")

for j=lbound(sVar) to ubound(sVar)
     if len(trim(sVar(j)))=0 then
       msgbox "Please enter information for " & sVar(j)
     end  if
next

0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 33445194
Besides the messagebox, what do you want to do?

I would do this before the line starting:  Set OutApp

Dim strErrMsg as string
If me.email = ""  Then strErrMsg = "Email address missing!
if me.Subject = "" then strErrMsg = (strErrMsg + vbcrlf) & "Subject missing!"
if me.Body = "" then strErrMsg = (strErrMsg + vbcrlf) & "Message body missing!"

If Len(strErrMsg) > 0 Then
    msgbox strErrMsg, vbOKOnly
    Exit Sub
End If


0
 

Author Comment

by:rutlandict
ID: 33445271
Hey guys, thanks for the fast reply.

Have tried both of the codes and the email still sent without any warning.

Here's my code:

Dim strErrMsg As String
If Me.Email = "" Then strErrMsg = "Email address missing!"
If Me.Extension = "" Then strErrMsg = (strErrMsg + vbCrLf) & "Extension Number missing!"
If Me.VoicemailPin = "" Then strErrMsg = (strErrMsg + vbCrLf) & "Voicemail Pin missing!"

If Len(strErrMsg) > 0 Then
    MsgBox strErrMsg, vbOKOnly
    Exit Sub
End If

    Dim OutApp As Object
    Dim OutMail As Object
   
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
     
    On Error Resume Next
    With OutMail
        .to = Me.Email
        .CC = ""
        .BCC = """"
        .Subject = "Test"
        .HTMLBody = "<HTML>Dear " & Me.ClientName & "<BR>" _
           
       
        .Send
       
        MsgBox ("Email has been sent successfully")
       
        .Sent
     
    End With
   
    On Error GoTo 0
     
    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

Have also tested Capricorn's code before .Send

Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33445287
<Have also tested Capricorn's code before .Send>

post the codes that you used , the whole sub..
0
 

Author Comment

by:rutlandict
ID: 33445345
Hello Capricorn, here's the code:

Private Sub Command550_Click()

    Dim OutApp As Object
    Dim OutMail As Object
   
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
     
    On Error Resume Next
    With OutMail
        .To = Me.Email
        .CC = ""
        .BCC = "i_am@alvinluo.me"""
        .Subject = "Test"
        .HTMLBody = "<HTML>Dear " & Me.ClientName & "<BR>" _
       
        Dim sVar(), j
        sVar = Array("sTo", "sCC", "sClientName")

        For j = LBound(sVar) To UBound(sVar)
        If Len(Trim(sVar(j))) = 0 Then
        MsgBox "Please enter information for " & sVar(j)
        End If
        Next
        .Send
       
        MsgBox ("Email has been sent successfully")
       
        .Sent
       
    End With
   
    On Error GoTo 0
     
    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

Thanks!
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33445391
where are your variables
"sTo", "sCC", "sClientName"



test this one

Private Sub Command550_Click()
dim sTo, sCC, sClientName
    Dim OutApp As Object
    Dim OutMail As Object

sTo=Me.Email
sCC="whatever"
sClientName=Me.ClientName  
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
     
    On Error Resume Next
    With OutMail
        .To = sto
        .CC = ""
        .BCC = "i_am@alvinluo.me"""
        .Subject = "Test"
        .HTMLBody = "<HTML>Dear " & sClientName & "<BR>" _
       
        Dim sVar(), j
        sVar = Array("sTo", "sCC", "sClientName")

        For j = LBound(sVar) To UBound(sVar)
        If Len(Trim(sVar(j))) = 0 Then
        MsgBox "Please enter information for " & sVar(j)
        exit sub
        End If
        Next
        .Send
       
        MsgBox ("Email has been sent successfully")
       
        .Sent
       
    End With
   
    On Error GoTo 0
     
    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33445399
better
Private Sub Command550_Click()
dim sTo, sCC, sClientName
    Dim OutApp As Object
    Dim OutMail As Object

sTo=Me.Email
sCC="whatever"
sClientName=Me.ClientName  

        Dim sVar(), j
        sVar = Array("sTo", "sCC", "sClientName")

        For j = LBound(sVar) To UBound(sVar)
        If Len(Trim(sVar(j))) = 0 Then
        MsgBox "Please enter information for " & sVar(j)
        exit sub
        End If
        Next


    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
     
    On Error Resume Next
    With OutMail
        .To = sto
        .CC = ""
        .BCC = "i_am@alvinluo.me"""
        .Subject = "Test"
        .HTMLBody = "<HTML>Dear " & sClientName & "<BR>" _
       

        .Send
       
        MsgBox ("Email has been sent successfully")
       
        .Sent
       
    End With
   
    On Error GoTo 0
     
    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

Open in new window

0
 

Author Comment

by:rutlandict
ID: 33445612
Hi Capricorn, tested your better script and did not work either.

here's my code:

Private Sub Command550_Click()
    Dim sTo, sTel, sClientName
    Dim OutApp As Object
    Dim OutMail As Object
   
        sTo = Me.Email
        sTel = Me.Tel
        sClientName = Me.ClientName

        Dim sVar(), j
        sVar = Array("sTo", "sClientName", "sTell")

        For j = LBound(sVar) To UBound(sVar)
        If Len(Trim(sVar(j))) = 0 Then
        MsgBox "Please enter information for " & sVar(j)
        Exit Sub
        End If
        Next
   
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
     
    On Error Resume Next
    With OutMail
        .To = "Me.Email"
        .CC = ""
        .BCC = """"
        .Subject = "Test"
        .HTMLBody = "<HTML>Dear " & Me.ClientName & "<BR>" _
       
       
        .Send
       
        MsgBox ("Email has been sent successfully")
       
        .Sent
       
    End With
   
    On Error GoTo 0
     
    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

Inside my form has a tab control, the textboxes are in Tab1 and my button is in Tab2. Will that affect the code?

Thanks
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33445701
that will work if any of the variables sTo, sTel, sClientName is empty or ""
did you try the codes without anything assign to any of the variables?

also in your code, this is incorrect
  sVar = Array("sTo", "sClientName", "sTell")

should be
    sVar = Array("sTo", "sClientName", "sTel")
0
 

Author Comment

by:rutlandict
ID: 33446274
Hi Capricorn,

I have changed that to "sTel" and also I have left blank for all the variables.

When I click 'Send', the email automatically sent and I did received the email with the blank variables in it.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33446326
can you upload your db..

but before that, do a compact and repair of your
from VBA window do Debug>Compile
correct any errors raised


try the operation again
0
 

Author Comment

by:rutlandict
ID: 33446639
Hi Capricorn,

I have used a sample from Microsoft and added the code inside. Still didn't work either.

Variables are: ContactFirstName, PhoneNumber and Country (I changed country as email)

Thanks
TS001018467-2-.mdb
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 33446909
test this


TS001018467-2-.mdb
0
 

Author Comment

by:rutlandict
ID: 33447008
That's perfect!

Thanks Capricorn!
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question