Hi Steve, thanks for your reply. I managed to solve the problem by running the make table query from VBA rather than calling it. This allowed me to directly reference the variables, rather than having to put them into a function to be accessed from the query.
Here's my final code:
Code:
Set rsSelectedGroups = CurrentDb.OpenRecordset("SELECT tbl_List_Groups.GroupID, tbl_List_Groups.Current, tbl_List_Groups.Selected From tbl_List_Groups WHERE (((tbl_List_Groups.Current) = True) AND ((tbl_List_Groups.Selected) = True));")
' Open recordset and concatenate a criteria string
'Cycle through rsSelectedGroups recordset and create string
If Not (rsSelectedGroups.BOF And rsSelectedGroups.EOF) Then
rsSelectedGroups.MoveFirst ' move to first record in recordset
Do Until rsSelectedGroups.EOF = True
strStringHolder = rsSelectedGroups!GroupID
strGroupString = strGroupString & BuildCriteria("tbl_AccountBase.Group", dbInteger, strStringHolder)
rsSelectedGroups.MoveNext ' move to next record in recordset
If rsSelectedGroups.EOF Then Exit Do ' if at end of records, exit from loop
strGroupString = strGroupString & " Or "
Loop ' return to start of loop
rsSelectedGroups.Close ' close recordset
End If
' Create table using SQL
strSQL = "SELECT 'GB' AS GB, tbl_AccountBase.SAPNumber, '' AS CallDate1, '' AS StartTime, '' AS CallDate2, '' AS EndTime, '' AS EmployeeNumber, tbl_AccountBase.Group, tbl_AccountBase.Segmentation, tbl_AccountBase.CallFrequency INTO tbl_ExportCSV FROM tbl_AccountBase WHERE (" & strGroupString & ") AND (" & strSegmentString & ") AND (" & strCallWeek & ");"
This only shows part of the code that creates the strings I use to filter in the SQL, but hopefully if anyone else is trying to achieve the same thing it might be helpful.
Thanks,
Simon