Send Email from Access

S

Simon

Hi

I hope some one can help, as i am not very good on Access

I have built a database for placing orders for customers, On the Order
Form i would like a button that would send out a email to say that the
order has been placed, then another button that order has been
despatched.

I have looked on the internet for code and found sum but do not
understand it.
As the order form only has Customer Number im not sure how to code it
to look up customers email address from tblCustomers

If any one could help i would be very greatful i am able to send the
database to you if u want to have a look but i can warn you it not
great.

Thanks

Simon
 
S

SJ

paste this SUB into the form's module, and call it from your button click
event.

you'll need to do a DLOOKUP or create a recordset to get the customer info
from the database first...

basically the code fires up an outlook mail item and fills in the email then
sits there and waits for you to send it / edit it from outlook.

if you just want to send it transparently, then use the .send instead of
the .Display......

see how it works then refine the variablers for your own use.



Private Sub SendMail_Click()
#If 6 = 7 Then
'declarations like this require a reference to the Outlook Type Library
'this can be a nuisance for installation and compilation
'therefore just declare the variables as objects and trap the run time
errors
'if Outlook is not installed
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
#End If

Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object
If IsNull(Me.EmailAddress) Then
If MsgBox("There is no Email Recipient specified in the [Email To:]
box." & vbCrLf _
& "Do you wish to create the Email anyway?", vbQuestion _
+ vbYesNo, "No Recipient(s) Specified") = vbNo Then Exit Sub
End If

On Error Resume Next
'Create the Outlook session.
Set objOutlook = GetObject(, "Outlook.Application") 'get reference
if Outlook is already running.
If Err.Number = 429 Then 'Error 429
occurs if Outlook is NOT running.
Err.Number = 0
Set objOutlook = CreateObject("Outlook.Application") 'Create a new
instance of the Outlook application.
If Err.Number = 429 Then 'Error 429 will
occur if MS Outlook is not installed.
MsgBox "MS Outlook is not installed on your computer"
Exit Sub
End If
End If

On Error GoTo Err_SendMail_Click

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0) 'olMailItem

With objOutlookMsg
' Add the To recipient(s) to the message.
If Not IsNull(Me.EmailAddress) Then
Set objOutlookRecip = .Recipients.Add(Me.EmailAddress)
objOutlookRecip.Type = 1 'olTo
End If

' Set the Subject and Body of the message.
.Subject = Me.Jobs_Description
.Body = "Re:- " & vbCrLf & "Our Job Code:- " & Me.JobCode _
& vbCrLf & "Our Job Name:- " & Me.Jobs_Description & vbCrLf &
vbCrLf

If Not IsNull(Me.DirectoryName) Then
Set objOutlookAttach = .Attachments.Add(Me.DirectoryName & "\" &
Me.FileName)
Else
'there is no attachment
' Set the Subject and Body of the message.
.Subject = Me.DocumentsOut_Description
.Body = vbCrLf & Details
End If

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' display message
.Display

'.send ' if you want to send it automatically

End With
Set objOutlook = Nothing

Exit_SendMail_Click:
Exit Sub

Err_SendMail_Click:
MsgBox Err.Description, vbInformation, "Email Error"
Resume Exit_SendMail_Click
Resume
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top