I am try to write some code that will inside a loop define and create two queries - the first a crosstab query, then from this run a make table query on it to make it permanent.
Inside my For Next loop I have the below code:
sql_1 = "TRANSFORM Sum([Sample].[" & sumvar & " 2002]) AS [The Value]"
sql_2 = " SELECT [Sample].[" & groupvar & "], Sum([Sample].[" & sumvar & " 2002]) AS [Total Of " & sumvar & "2002] FROM [Sample]"
sql_3 = " WHERE ((([Sample].[Postcode]) = 'C'))"
sql_4 = " GROUP BY [Sample].[" & groupvar & "]"
sql_5 = " PIVOT [Sample].[" & pivotvar & "];"
sql_ct = sql_1 & sql_2 & sql_3 & sql_4 & sql_5
sql_6 = "SELECT SQLQUERY1.* INTO [" & groupvar & pivotvar & sumvar & "] FROM SQLQUERY1;"
Set qdf = CurrentDb.CreateQueryDef("SQLQUERY1", sql_ct)
Set rstresults = qdf.OpenRecordset
Set rstTblOutput = CurrentDb.OpenRecordset(sql_ct, dbOpenDynaset, dbAppendOnly)
With rstresults
'.MoveLast
'.MoveFirst
For lLoop = 1 To .RecordCount
rstTblOutput.AddNew
rstTblOutput.Fields(0).Value = .Fields(0).Value
rstTblOutput.Fields(1).Value = .Fields(1).Value
rstTblOutput.Fields(2).Value = .Fields(2).Value
rstTblOutput.Fields(3).Value = .Fields(3).Value
'rstTblOutput.Fields(4).Value = .Fields(4).Value
rstTblOutput.Update
.MoveNext
Next lLoop
End With
DoCmd.RunSQL sql_6
rstresults.Close
rstTblOutput.Close
DoCmd.DeleteObject acQuery, "SQLQUERY1"
I can create my first query, but it doesn't output, I get an error message saying: 'Run time error 3027 Can't Update. Database or object is read only'. I can't see what is the problem, but as I say I have done this before. Any help would be much appreciated,
Matt
Inside my For Next loop I have the below code:
sql_1 = "TRANSFORM Sum([Sample].[" & sumvar & " 2002]) AS [The Value]"
sql_2 = " SELECT [Sample].[" & groupvar & "], Sum([Sample].[" & sumvar & " 2002]) AS [Total Of " & sumvar & "2002] FROM [Sample]"
sql_3 = " WHERE ((([Sample].[Postcode]) = 'C'))"
sql_4 = " GROUP BY [Sample].[" & groupvar & "]"
sql_5 = " PIVOT [Sample].[" & pivotvar & "];"
sql_ct = sql_1 & sql_2 & sql_3 & sql_4 & sql_5
sql_6 = "SELECT SQLQUERY1.* INTO [" & groupvar & pivotvar & sumvar & "] FROM SQLQUERY1;"
Set qdf = CurrentDb.CreateQueryDef("SQLQUERY1", sql_ct)
Set rstresults = qdf.OpenRecordset
Set rstTblOutput = CurrentDb.OpenRecordset(sql_ct, dbOpenDynaset, dbAppendOnly)
With rstresults
'.MoveLast
'.MoveFirst
For lLoop = 1 To .RecordCount
rstTblOutput.AddNew
rstTblOutput.Fields(0).Value = .Fields(0).Value
rstTblOutput.Fields(1).Value = .Fields(1).Value
rstTblOutput.Fields(2).Value = .Fields(2).Value
rstTblOutput.Fields(3).Value = .Fields(3).Value
'rstTblOutput.Fields(4).Value = .Fields(4).Value
rstTblOutput.Update
.MoveNext
Next lLoop
End With
DoCmd.RunSQL sql_6
rstresults.Close
rstTblOutput.Close
DoCmd.DeleteObject acQuery, "SQLQUERY1"
I can create my first query, but it doesn't output, I get an error message saying: 'Run time error 3027 Can't Update. Database or object is read only'. I can't see what is the problem, but as I say I have done this before. Any help would be much appreciated,
Matt