Hi dear all, I have a piece of code about export different queries into multiple worksheet in one excel file. The query "DivideGroup" is a crosstab query, and the data value in "DivideGroup" should change in each loop depending on the grouping factors. Now I can get my first loop right, but the second time it will add the value to my first query and pass to the second worksheet
Can anyone have a look at my code and help me to delete the data at the end of each loop? Thanks a lot!!!
Can anyone have a look at my code and help me to delete the data at the end of each loop? Thanks a lot!!!
Code:
Private Sub ExprDetail_Click()
Dim db As DAO.Database
Dim qdf1 As DAO.QueryDef
Dim qdf2 As DAO.QueryDef
Dim rst As Recordset
Dim recordCnt As Integer
Dim recordNm As Integer
Dim sql As String
Dim arrStr As String
Dim arrCount() As String
Const JOIN_SEP = ""","""
Set db = CurrentDb()
Set qdf1 = db.QueryDefs("RecordExcel")
Set rst = qdf1.OpenRecordset
recordCnt = rst.RecordCount
ReDim arrCount(1 To recordCnt) As String
rst.MoveFirst
For recordNm = 1 To recordCnt
Set qdf2 = db.QueryDefs("DivideGroup")
arrCount(recordNm) = rst!grouping
arrStr = """" & Join(arrCount(), JOIN_SEP) & """"
sql = "TRANSFORM Sum(Format_Step3.SumOfCatch) AS SumOfSumOfCatch SELECT Format_Step3.TimeGroup AS [Date] " _
& "FROM Format_Step3 INNER JOIN RecordExcel ON Format_Step3.grouping = RecordExcel.grouping WHERE (((RecordExcel.grouping) In (" & arrStr & "))) " _
& "GROUP BY Format_Step3.TimeGroup, RecordExcel.grouping ORDER BY Format_Step3.TimeGroup PIVOT Format_Step3.ClnVar;"
qdf2.sql = sql
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "DivideGroup", "C:\Documents and Settings\WangA\My Documents\Export_Excel_Files\FNexcel.xls", True, arrStr
rst.MoveNext
qdf2.Close
Set qdf3 = Nothing
Next recordNm
End Sub