[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MsgBox before sending email

Posted on 2010-08-16
14
Medium Priority
?
353 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
  • 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 49

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

873 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