Jacqui (and others),
I had the same problem and concluded that you can't use a recordset as the record source for a report. The temp table works and it was not too messy. My queries were extremely complicated, combining the user input off three lists (time period, company subsidiary and topic) and then applying the selected records to a variety of possible report recipients. I could not (I believe) simply link a query and a report because I found no way to do the complex query I had in a "Query" per se, even going into the SQL view.
Disclaimer -- you mentioned a server. It is not necessary in my work for me to appreciate the fine points of storage on a server. So, nothing in this posting covers that aspect.
In essence, I use the user input to construct the SQL SELECT statement to select data from the main table. Then, I open an ADODB recordset to hold the selected records. CAUTION - since I manipulate the data temporarily for the report, I opened the recordset Static not Dynamic. The latter pushes the changes through to the source table and I didn't want that at this stage. I made a template table, mostly a duplicate of the main table, sans some fields I knew I would not need. I open it and delete any data from the last report. Then, I use SQL INSERT to store the selected records in the temp table. Then, close the recordset, SELECT all the records into a second recordset (open Dynamically), and manipulate the data. The changes are made to the temp table, too, which is precisely what I want. The temp table has been defined to be the source for the report so I open the report. And the manipulated data is printed.
Here are the major steps of the code (note that the below statements are not complete syntax or logic):
Public strSQL As String
-----------------------
Public Sub Report_Open(Cancel As Integer)
Dim conADOConnection As Connection
Dim conADO As New Connection
Set conADOConnection = CurrentProject.Connection
conADO.ConnectionString =
CurrentProject.BaseConnectionString
On Error GoTo err_expt...
DoCmd.OpenForm "XYZSelection", acNormal, , , acFormAdd, acDialog
' XYZSelection is a separate procedure which uses a form
containing 2, 3, or 4 listboxes for picking criteria.
Which list boxes are visible depends on the type of
report, which the user has selected already in a
PrintReportDialog box.
' next, the program constructs the SQL
' I have a SELECT CASE set for each criterion preceded
by the list of fields which is common to all types and
followed by the ORDER BY clause to set the order,
again, common to all types
strSQL = "SELECT company, topic, etc FROM " & _
"maintable WHERE close_open <> 'C'"
' now get portion of SQL statement relating to company
Select Case Company
Case "All"
' nothing to do here - all companies chosen
Case "Subs"
strSQL = strSQL & " AND company <> 'parent'"
Case Else
If strCompany = "parent" Then
strSQL = strSQL & " AND (company = '" & Company & _
"' OR company = 'All')"
Else
strSQL = strSQL & " AND (company = '" & strCompany & _
"' OR rep_company = 'All' OR company = 'Subs')"
End If
End Select
Dim objRSetRpt As New ADODB.Recordset
objRSetRpt.Open strSQL, CurrentProject.Connection, _
adOpenStatic, adLockOptimistic
DoCmd.OpenTable "PrntTmp", acViewNormal, acAdd
Do Until objRSetRpt.EOF
strSQL = "INSERT INTO PrntTmp (fields) "
strSQL = strSQL & "VALUES('"
strSQL = strSQL & objRSetRpt.Fields.Item(0) & "','"
etc. through 9 more fields
DoCmd.RunSQL strSQL
objRSetRpt.MoveNext
Loop
strSQL = "SELECT company, topic, etc. FROM PrntTmp"
Dim obj2RSetRpt As New ADODB.Recordset
obj2RSetRpt.Open strSQL, CurrentProject.Connection, _
adOpenDynamic, adLockOptimistic
DoCmd.OpenReport "report", acViewPreview
End Sub
Good luck. I'm new to Access in 2002 (have Access 2000) and I've really struggled with it. If there's an easier way than the above, it eluded me.
GregGol
greggol@prodigy.net