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!

Access Form to be mailed on submitting.

Status
Not open for further replies.

aleenkhan

MIS
Sep 25, 2017
1
IN
Hi,

I am trying to make a form in access with few fields. I want the form records to be submitted to "MainTable" and also the values from the form fields to be mailed to 2 hardcoded email addresses. I also want to keep a track of the mails sent in another table "LogTable" with the content sent in the email.

Can someone help me as how to include form field values in the email and to keep a trail in another table.

A similar example code is as follows:

'******begin code******
Dim email, ref, origin, destination, notes As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

'**gathers information from your form. this sets the string variable to your fields
email = Me!email
ref = Me!ref
origin = Me!origin
destination = Me!destination
notes = Me!notes

'***creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

'***creates and sends email
With objEmail
.To = email
.Subject = ref & " " & origin & " " & destination
.Body = notes
.Send
End With

'**closes outlook
Set objEmail = Nothing
objOutlook.Quit

Exit Sub
'****end code****

 
Is the code something you wrote or something you found? Have you tried it to see how it runs? Are you getting any errors?

To keep the trail in another table, you can use a Query Object, SQL String, or a recordset.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
I would start simple, with [blue]hard-coded data[/blue] (something like this, code not tested!)

Code:
Option Explicit

Private Sub MyEMail()
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
[green]
'***creates an instance of Outlook[/green]
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
[green]
'***creates and sends email[/green]
With objEmail
    .From = [blue]"MySelf@domeain.com"[/blue]
    .To = [blue]"MySelf@domeain.com"[/blue]
    .Subject = [blue]"My Test Subject"[/blue]
    .Body = [blue]"I hope it works for me"[/blue]
    .Send
End With
[green]
'**closes outlook[/green]
Set objEmail = Nothing
objOutlook.Quit

End Sub

made sure it works, and than keep adding to it.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top