Solved

MsgBox before sending email

Posted on 2010-08-16
14
306 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 119

Expert Comment

by:Rey Obrero
Comment Utility
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 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
Comment Utility
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
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
<Have also tested Capricorn's code before .Send>

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

Author Comment

by:rutlandict
Comment Utility
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 119

Expert Comment

by:Rey Obrero
Comment Utility
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 119

Expert Comment

by:Rey Obrero
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rutlandict
Comment Utility
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 119

Expert Comment

by:Rey Obrero
Comment Utility
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
Comment Utility
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 119

Expert Comment

by:Rey Obrero
Comment Utility
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
Comment Utility
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 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
test this


TS001018467-2-.mdb
0
 

Author Comment

by:rutlandict
Comment Utility
That's perfect!

Thanks Capricorn!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now