Log in

View Full Version : E-mail with Access



crender2000
07-06-2012, 11:53 AM
Here is what I want to do. Seems like this should be possible I have a user form where the user inputs data. Base on this data I want access to email certain people base on two fields in this data. Say combo box 1 = something and combo box 2 = something e mail John Doe.

Case Combo box 1 = something else And
Case Como box 2 = something else
email Jane Doe.


Also this data is being added to a table if that helps any.

Is this possible? Any help would be apprectiated. I want to do this in the code with out using outlook.

MacroShadow
07-07-2012, 12:16 PM
It is pretty simple, but first you must decide which method of emailing you want to use. Refer to this (http://www.granite.ab.ca/access/email.htm) link for an outline of the different options.

Below is shown a method using CDO, configured for sending from a gmail account. All you have to do is update youe gmail account info (colored in red).

Public Sub SendAMessage(strFrom As String, _
strSubject As String, _
strTextBody As String, _
Optional strCC As String, _
Optional strBcc As String, _
Optional strAttachDoc As String)

On Error GoTo MyErrorHadler

Dim cdoMsg As Object
Set cdoMsg = CreateObject("CDO.message")
Dim strTo as String

Select Case Combobox1
Case x
If Combobox2 = y Then
strTo = "JohnDoe@msn.com"
End If
Case y
If Combobox2 = x Then
strTo = "JaneDoe@msn.com"
End If
End Select

With cdoMsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourMail@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YourPassword"
.Update
End With

With cdoMsg
.To = strTo
.From = strFrom
If Len(Trim$(strCC)) > 0 Then
.CC = strCC
End If
If Len(strBcc) > 0 Then
.BCC = strBcc
End If
.Subject = strSubject
.TextBody = strTextBody
If Len(strAttachDoc) > 0 Then
.AddAttachment strAttachDoc
End If

DoCmd.Hourglass True
.Send
If Err.Number <> 0 Then
MsgBox "CDO error " & Hex$(Err.Number) & vbNewLine & Err.Description
Else
MsgBox "Mail sent!"
End If
DoCmd.Hourglass False

End With

Set cdoMsg = Nothing

End Sub


NOTE:
The above code was not tested but should work nonetheless.

crender2000
07-07-2012, 12:42 PM
I am not sure if that will work or not but if I have to work on this project some more I will give it a try. To be honest I have no idea what kind of e-mail we use at work. I was asked to create a database using access and they would like it to be able to e-mail certain people depending on the values of certain combo boxes. What makes this project difficult is my employer does not trust us enough to give us e-mail and they have the internet so shut down I can not even search for solutions. We can not even use the help on Microsoft access.