The user has a report requirement on some data that I can only display in a spreadsheet. I have written a spreadhsheet that formats the results of a crosstab quey into the format they want. The last stage is to get the data from ACCESS to EXCEL, which I was going to do in a recordset query. I have a crosstab query that works fine so thought it wouldn't be any trouble to replicate in a recordset but I can't get any data into a recordset.
The crosstab query that works fine when I run it in the query designer uses the following SQL:
I need to create this as a recordset which I am doing with the following code:
If I remove the where clause it works. If I try to filter the query in any way, even with a "LIKE *" in a text field, it always returns 0 records. I've removed all but one field and that makes no difference. I've tried removing the forms reference and that makes no difference either.
Anyone any advice on this one?
Thanks,
Mark.
Mark Davies
Warwickshire County Council
The crosstab query that works fine when I run it in the query designer uses the following SQL:
Code:
TRANSFORM Sum([Q_HWRC-SS].NetTonnes) AS SumOfNetTonnes
SELECT T_SiteDetails.SiteName, [Q_HWRC-SS].OutputWasteType, [Q_HWRC-SS].UseTreatment
FROM (T_Months INNER JOIN [Q_HWRC-SS] ON T_Months.MonthRef = [Q_HWRC-SS].Month) INNER JOIN T_SiteDetails ON [Q_HWRC-SS].SiteID = T_SiteDetails.SiteID
WHERE ((([Q_HWRC-SS].OutputWasteType) Like [Forms]![F_Reports]![Waste]) AND ((T_SiteDetails.SiteName) Like "*HWRC*") AND (([Q_HWRC-SS].OutputDate) Between [Forms]![F_Reports]![FromDate] And [Forms]![F_Reports]![ToDate]))
GROUP BY T_SiteDetails.SiteName, [Q_HWRC-SS].OutputWasteType, [Q_HWRC-SS].UseTreatment
PIVOT T_Months.MonthName In ("Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan","Feb","Mar");
I need to create this as a recordset which I am doing with the following code:
Code:
Dim cnCurrent As ADODB.Connection ' Connection object
Dim rst As ADODB.Recordset ' recordset object
Dim sqlstr As String
'Instantiate connection object
Set cnCurrent = CurrentProject.Connection
' Instantiate recordset object
Set rst = New ADODB.Recordset
' Build Query
sqlstr = ""
sqlstr = sqlstr + "TRANSFORM Sum(T_Output.NetTonnes) AS SumOfNetTonnes "
sqlstr = sqlstr + "SELECT T_Output.UseTreatment, T_SiteDetails.SiteName, T_Output.OutputWasteType, Sum(T_Output.NetTonnes) AS [Total Of NetTonnes] "
sqlstr = sqlstr + "FROM T_SiteDetails RIGHT JOIN T_Output ON T_SiteDetails.SiteID = T_Output.SiteID "
sqlstr = sqlstr + "WHERE (((T_SiteDetails.SiteName) Like '*HWRC*') AND ((T_Output.OutputWasteType) Like '" & [Forms]![F_Reports]![Waste] & "') AND ((T_Output.OutputDate) Between #04/01/2006# And #03/31/2007#)) "
sqlstr = sqlstr + "GROUP BY T_Output.UseTreatment, T_SiteDetails.SiteName, T_Output.OutputWasteType, T_Output.OutputWasteType "
sqlstr = sqlstr + "PIVOT Format([OutputDate],'mmm') In ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');"
'Open recordset based on query
rst.Open sqlstr, cnCurrent
' If count has returned 1 or more then display appropriate message
If rst.BOF = True And rst.EOF = True Then
MsgBox "You query has returned no data"
Else
' CREATE SPREADSHEET
End If
If I remove the where clause it works. If I try to filter the query in any way, even with a "LIKE *" in a text field, it always returns 0 records. I've removed all but one field and that makes no difference. I've tried removing the forms reference and that makes no difference either.
Anyone any advice on this one?
Thanks,
Mark.
Mark Davies
Warwickshire County Council