I'm trying to allow database users to click on a button on a form and send e-mail to certain clients based on what criteria they select from a drop-down box on said form.
The send e-mail function works fine - as long as there are no criteria/parameters/clauses (in my case, these criteria reference the combo boxes on the form) in the query from which the e-mail addresses are pulled. Criteria results in an "too few parameters, expected X#" error.
I'm pretty new to both Access and working with VBA. I've seen recommendations for building the query in VBA, but that does not fix the parameters error for me.
Though it seems like a bandaid, I thought that if I could write code to make and save a table based on the query, and then run the e-mail function based on that table, that might work - but I can't figure out how to do it.
I hope this makes any sense at all. Thanks for any suggestions!
Here's the code for send email function, in case that helps:
The send e-mail function works fine - as long as there are no criteria/parameters/clauses (in my case, these criteria reference the combo boxes on the form) in the query from which the e-mail addresses are pulled. Criteria results in an "too few parameters, expected X#" error.
I'm pretty new to both Access and working with VBA. I've seen recommendations for building the query in VBA, but that does not fix the parameters error for me.
Though it seems like a bandaid, I thought that if I could write code to make and save a table based on the query, and then run the e-mail function based on that table, that might work - but I can't figure out how to do it.
I hope this makes any sense at all. Thanks for any suggestions!
Here's the code for send email function, in case that helps:
Code:
Option Compare Database
Public Function SendMail()
Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Set fso = New FileSystemObject
Set MyOutlook = New Outlook.Application
Set db = CurrentDb()
Set MailList = db.OpenRecordset("qryVBATest")
Set MyMail = MyOutlook.CreateItem(olMailItem)
Do Until MailList.EOF
If IsNull(MailList("email")) Then MailList.MoveNext
If IsNull(MailList("email")) Then MailList.MoveNext
If IsNull(MailList("email")) Then MailList.MoveNext
MyMail.Recipients.Add MailList("email")
MailList.MoveNext
Loop
'MyMail.Send
On Error Resume Next
MyMail.Display
Set MyMail = Nothing
Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing
End Function
Comment