Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querydef novice in need of help

Status
Not open for further replies.

mattyp75

Programmer
Aug 28, 2002
16
GB
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top