Hello, I have this code that is giving me a runtime error "3241: Data type conversion error".
I am trying to click a command button that will create an outlook email, populating the email address field with the emails gathered in QryConfirmedEmail.
Here is my code:
Cheers and Happy Haloween!
I am trying to click a command button that will create an outlook email, populating the email address field with the emails gathered in QryConfirmedEmail.
Here is my code:
Code:
Dim strConn As String
Dim myQueryDef As QueryDef
Dim rst As Recordset
Dim oOApp_001 As Outlook.Application
Dim oOApp_001_B As Outlook.Application
Dim oOMail_001 As Outlook.MailItem
Dim conn As ADODB.Connection
Dim eTotal As Long
Dim distro As String
Dim i As Long
Dim strQuery As String
Set oOApp_001 = CreateObject("Outlook.Application")
Set oOMail_001 = oOApp_001.CreateItem(olMailItem)
'q below just calls all recs from table with emails
strQuery = "QryConfirmedEmail"
Set myQueryDef = CurrentDb.QueryDefs("QryConfirmedEmail")
Set rst = myQueryDef.OpenRecordset(strQuery, dbOpenDynaset)
'This DCOUNT will count the number of records and put it in eTotal
eTotal = DCount("[E-Mail Address]", "tblContacts")
'Sets the email distro to blank
distro = ""
'Starts a for loop for the number of email addresses in the table
For i = 1 To eTotal
'concats each name together with ;
distro = distro & ";" & rst.Fields("[E-Mail Address]")
'In the loop, need to move to the next record to concat email address
'If trys to move pass last rec, then cause error, so checked for last rec before moving
If i <> eTotal Then
rst.MoveNext
End If
Next i
With oOMail_001
'now use the variable disto with all the email names in the to field of outlook
.To = distro
.Body = "Please find attached."
End With
End Sub
Cheers and Happy Haloween!