INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Emailing From Access

Emailing from Access using Docmd.SendObject by nat1967
Posted: 27 Nov 03

Hi everyone.  Sending email with Access is a very popular topic here at Tek-Tips and other newsgroups.  I have written a FAQ on using Outlook, FAQ702-2921, but there should be one on the simpler SendObject command.

SendObject will send your email by what ever default email program your computer uses.  If you are not sure what your default program is:

Goto: Control Panel> Internet Options> Programs tab and see what is listed for your email program.

For this FAQ, letÆs assume you just want a simple email sent.  Of course, this could be as complex as you want it but I want to keep it simple for now.

Create a form and put a command button on the form.  Go to your properties window for the command button, choose the Event tab, choose the OnClick event.  To the right hand side of the OnClick event, you will find 3 dots (à).  Click on the ellipse (à) and your VBA code window will open.

Notice your VBA window has pre-formatted the beginning and end of your sub routine.  Inside the sub routine is where we will place our code.

Example code:

Private Sub Command0_Click()   æ<---Pre-formatted

    ' Prevent error screen if user cancels without sending mail.
    On Error Resume Next
    
    Dim strToWhom     As String   '<---sets up a variable object for who we are going to send out email to
    Dim strMsgBody    As String   '<---sets up a variable object for the body of our email
    
    strToWhom = InputBox("Enter recipient's e-mail address.", _
                        "Enter Email Address")   '<--- uses an input box to ask the user what email address to send this message to
      
    strMsgBody = ôThis is a sample emailö

    ' Provide Subject title bar and message text.
    DoCmd.SendObject , , , strToWhom, , , "Sample email", strMsgBody, True

End Sub

In the example code above, I have declared two variables.  They are strToWhom and strMsgBody.  These variables will hold our email address and the body of our email.  They are declared as Strings or text objects.

StrToWhom will use an InputBox for the user to input the address to which the email will be sent.  StrMsgBox will be used to build the body of the email.  In the example, I am building the string within the code.

Putting it all togetherà.Straight out of the æol help file:

DoCmd.SendObject [objecttype][, objectname][, outputformat][, to][, cc][, bcc][, subject][, messagetext][, editmessage][, templatefile]

The help file for SendObject breaks down all the fields and what they mean.  I donÆt see the need to cut and paste the whole help file, soà IÆll just refer you there for more reading.

Please let me know if something is not explained as well as it should be.  I would like for this FAQ to be very helpful.

Nathan
Senior Software Test Lead


Back to Microsoft: Access Forms FAQ Index
Back to Microsoft: Access Forms Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close