Can anyone point me in the right direction to create a report based on a recordset built from a SQL Server Stored procedure? I am using Access 2000 ADO....
As a static recordsource for a report an ADO recordset populated by calling a SQL stored procedure is no different from an ADO recordset populated by passing a SQL statement to the JET Engine. You just can't rely on the Access report wizard and must create your report manually, referring to the elements of you recordset to supply values to your report objects.
Yes, so I have read. However, when I create the recordset in the Report_Open event and then try and assign it to the reports recordset/recordsource property, I get errors such as invalid property or type mismatch etc.
The problem is that there is plenty of statements around to confirm that this can be done, but I have yet to see some working code.
Anyone who can provide me with a sample will get a squillion stars....
I could be wrong but I don't think it is possible to set an Access report's recordsource property to an existing recordset object. You could set the recordsource property to an SQL call to the SQL server however. If you want to call the stored procedure instead and populate a recordset object you could then loop the recordset and insert records into a temporary local table and then set the report recordsource to the temp table.
Thanks for the info guys. Basically because I'm prototyping in Access, i.e. front and back-end's are Access.mdb's, I can't use coc86's suggestion until I move the database into SQL Server - Access won't let me do a pass-through query to another Access database.
In the meantime I was doing what wtmckown has suggested with the temp table, but it just seemed like a bit of a messy solution. But if there is no other way, then so be it.
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.