Hi Dom,
Brilliant thank you.
Its now sending an email to all the recipients, and each has a spreadsheet
attachment which is fantastic.
However, the spreadsheet has field names but contains no data. I must have
done something wrong in the query SQL. This is what I have. Is there an
error?
SELECT [Classes List].*, [Classes List].[AttendeeID]
FROM [Classes List] INNER JOIN [Attendee List] ON [Classes
List].[AttendeeID] = [Attendee List].[AttendeeID]
WHERE ([Classes
List].[AtendeeID])="rstRecipients.Fields(RECIP_ID_FIELD_NAME).Value";
Very nearly there I think...
Thanks,
Alison
DomThePom said:
Sorry - last code I posted was buggy - the following works!
Sub sendObjectClasses()
Dim rstRecipients As DAO.Recordset 'define
recorset of recipients to run through
Dim qdfClasses As DAO.QueryDef 'define
a queryddef of classes
Dim strSQL As String
'variable to hold SQL statement
Dim strAddress As String
'varialble to hold address
Dim strName As String
'varialble to hold name
Const RECIP_QRY_NAME As String = "qryRecipsWithClasses" 'set up
a query of recipients who have classes
Const RECIP_ID_FIELD_NAME As String = "MailAddressID"
'defines recipient ID field name
Const RECIP_ADDRESS_FIELD_NAME As String = "MailAddress"
'defines recipient address field name
Const CLASSES_TBL_NAME As String = "zzMailData"
'defines name of table which lists classes and recipient ID's
Const MESSAGE_TEXT As String = "Here is your data" 'define
message content
Const CLASSES_QRY_NAME As String = "qryClasses" 'set up
a query (any we will change SQL dynamically
'set up error handler
On Error GoTo Proc_Error
'open a recordset of recipipients with classes
Set rstRecipients = CurrentDb.OpenRecordset(RECIP_QRY_NAME,
dbOpenSnapshot)
'define the query whose SQL property we will manipulate
Set qdfClasses = CurrentDb.QueryDefs(CLASSES_QRY_NAME)
'for each recipipient with classes
Do Until rstRecipients.EOF
'define current address and name
strAddress = rstRecipients.Fields(RECIP_ADDRESS_FIELD_NAME).Value
strName = Left(strAddress, InStr(1, strAddress, "@") - 1)
'change the classes query SQL to extract classes for this
recipient only
strSQL = "Select " & CLASSES_TBL_NAME & ".* FROM " &
CLASSES_TBL_NAME _
& " WHERE " & CLASSES_TBL_NAME & "." &
RECIP_ID_FIELD_NAME & "=" _
& rstRecipients.Fields(RECIP_ID_FIELD_NAME).Value
qdfClasses.SQL = strSQL
'now send qry we have defined for this recipient as an excel
attachment
DoCmd.sendObject ObjectType:=acSendQuery, _
ObjectName:=CLASSES_QRY_NAME, _
OutputFormat:=acFormatXLS, _
To:=strAddress, _
Subject:="Data for " & strName, _
MessageText:=MESSAGE_TEXT, _
EditMessage:=False
'move to next recipient in recordset
rstRecipients.MoveNext
Loop
Proc_Exit:
'clean up
On Error Resume Next
rstRecipients.Close
Set rstRecipients = Nothing
Set qdfClasses = Nothing
Exit Sub
Proc_Error:
MsgBox Error(Err)
Resume Proc_Exit
End Sub