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.
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
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: