Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Emailing from Access using Docmd.SendObject

Emailing From Access

Emailing from Access using Docmd.SendObject

by  nat1967  Posted    (Edited  )
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

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top