So I am using buttons to run a query which then pulls the selected emails into an email. There is a singular function for this and then each button sends the corresponding query to act as the recordset
Sub EmailQuery(strQueryName As String)
'On Error GoTo Err_EmailRequery_Click
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strEmail As String
Set cn = CurrentProject.Connection
Set rs = New ADODB.Recordset
rs.Open strQueryName, cn
With rs
.MoveLast
.MoveFirst
Do While Not .EOF
strEmail = strEmail & .Fields("Email") & ";"
.MoveNext
Loop
.Close
End With
strEmail = Left(strEmail, Len(strEmail) - 1)
DoCmd.SendObject , , , , , strEmail, , , True, False
'Exit_EmailRequery_Click:
'
' Exit Sub
'
'Err_EmailRequery_Click:
'
' MsgBox Err.Description
'
' Resume Exit_EmailRequery_Click
End Sub
Private Sub cmdActive_Click()
EmailQuery ("qryActiveSuppliers")
End Sub
Private Sub cmdAllSuppliers_Click()
EmailQuery ("qryAllSuppliers")
End Sub
Private Sub cmdArrangements_Click()
EmailQuery ("qryAgreementEmail")
End Sub
Private Sub cmdInactive_Click()
EmailQuery ("qryInactiveSuppliers")
End Sub
Form where the buttons are located All queries runs correctly just by clicking it in access and all bar the arrangements query run correctly. I took out the criteria from it's SQL statement to see if it would run and it did. The criteria is matched against the combobox selection on the form. Below is the SQL statement for the Arrangements button.
SELECT DISTINCT tblSuppliers.SupplierName, Nz([BusinessEmail],[PersonalEmail]) AS Email
FROM ((tblSuppliers
INNER JOIN tblSuppliersAgreements ON tblSuppliers.ID = tblSuppliersAgreements.SupplierID)
INNER JOIN tblContacts ON tblSuppliers.ID = tblContacts.SupplierID)
WHERE ((tblSuppliersAgreements.AgreementID)=[Forms]![frmMainMenu]![cboAgreement]);
This is the error I am getting when I try to click the button to run it
I think it may have something to do with the way I open the query in the rs.open line and I need to call the criteria not just in the SQL statement? Any help on this issue or a solution would be greatly appreciated.
Edit
So I have changed my code to this with DAO to see if that would fix the issue. I now get an error on the line Set rs = db.OpenRecordset(strQueryname)
The error
I have left the previous way commented so if a solution is provided for that I can change back at any time.
Sub EmailQuery(strQueryName As String)
'On Error GoTo Err_EmailRequery_Click
' Dim cn As ADODB.Connection
' Dim rs As ADODB.Recordset
Dim strEmail As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(strQueryName)
' Set cn = CurrentProject.Connection
' Set rs = New ADODB.Recordset
MsgBox strQueryName
' rs.Open strQueryName, cn
With rs
' .MoveLast
' .MoveFirst
Do While Not .EOF
strEmail = strEmail & .Fields("Email") & ";"
.MoveNext
Loop
.Close
End With
strEmail = Left(strEmail, Len(strEmail) - 1)
DoCmd.SendObject , , , , , strEmail, , , True, False
'Exit_EmailRequery_Click:
'
' Exit Sub
'
'Err_EmailRequery_Click:
'
' MsgBox Err.Description
'
' Resume Exit_EmailRequery_Click
End Sub
Edit 2
Current code in main function
Sub EmailQuery(strQueryName As String)
'On Error GoTo Err_EmailQuery_Click
Dim strEmail As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(strQueryName)
MsgBox strQueryName
With rs
' .MoveLast
' .MoveFirst
Do While Not .EOF
strEmail = strEmail & .Fields("Email") & ";"
.MoveNext
Loop
.Close
End With
strEmail = Left(strEmail, Len(strEmail) - 1)
DoCmd.SendObject , , , , , strEmail, , , True, False
'Exit_EmailQuery_Click:
'
' Exit Sub
'
'Err_EmailQuery_Click:
'
' MsgBox Err.Description
'
' Resume Exit_EmailQuery_Click
End Sub
Current SQL
PARAMETERS [PrmID] Long;
SELECT DISTINCT tblSuppliers.SupplierName, IIf( IsNull(BusinessEmail) , PersonalEmail, BusinessEmail) AS Email
FROM (tblSuppliers
INNER JOIN tblSuppliersAgreements ON tblSuppliers.ID = tblSuppliersAgreements.SupplierID)
INNER JOIN tblContacts ON tblSuppliers.ID = tblContacts.SupplierID
WHERE ((tblSuppliersAgreements.AgreementID)=[PrmID]);
I know there are probably issues with the way the SQL is done, being halfway parameterised or just done wrong.

DAOin order to be able to pass just the query name. InADOyou will probably need to supply a valid command, e.g."SELECT * FROM " & strQueryName.