Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76

    the critieria for exporting single query to multiple worksheets

    Hi dear All, I am trying to export single query "DivideGroup" to multiple excel worksheets, however the critieria I transfered to my "DivideGroup" is In (""),


    I wanted critieria is In my first record in RecordExcel.grouping like In("bike"), where bike is the first record in "RecordExcel" query, and then second record and so on...

    Can anyone take a look at my code here? 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 = ""","""
    arrStr = """" & Join(arrCount, JOIN_SEP) & """"
    
    Set db = CurrentDb()
    Set qdf1 = db.QueryDefs("RecordExcel")
    Set qdf2 = db.QueryDefs("DivideGroup")
    Set rst = qdf1.OpenRecordset
    
    recordCnt = rst.RecordCount
    ReDim arrCount(1 To recordCnt) As String
    
    For recordNm = 1 To recordCnt
       arrCount(recordNm) = rst!grouping
       sql = "TRANSFORM Sum(Format_Step3.SumOfCatch) AS SumOfSumOfCatch " _
            & "SELECT (Format_Step3.TimeGroup)As Week, (RecordExcel.grouping) As Species FROM Format_Step3 INNER JOIN RecordExcel ON " _
            & "Format_Step3.grouping = RecordExcel.grouping WHERE (((RecordExcel.grouping) In (" & arrStr & ")))GROUP BY Format_Step3.TimeGroup, " _
            & "RecordExcel.grouping PIVOT Format_Step3.ClnVar;"
        
       qdf2.sql = sql
        
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "DivideGroup", "C:\Documents and Settings\WangA\My Documents\Export_Excel_Files.xls", True, arrCount(recordNm)
        rst.MoveNext
    Next recordNm
        
        
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Not how I would do it, but arrStr is set before it contains anything.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Right...I try to put
    Code:
    arrStr = """" & Join(arrCount, JOIN_SEP) & """"
    in between
    Code:
     arrCount(recordNm) = rst!grouping
    
              arrStr = """" & Join(arrCount, JOIN_SEP) & """"
    
               sql = "TRANSFORM Sum(Format_Step3.SumOfCatch) AS SumOfSumOfCatch " _
            & "SELECT (Format_Step3.TimeGroup)As Week, (RecordExcel.grouping) As Species FROM Format_Step3 INNER JOIN RecordExcel ON " _
            & "Format_Step3.grouping = RecordExcel.grouping WHERE (((RecordExcel.grouping) In (" & arrStr & ")))GROUP BY Format_Step3.TimeGroup, " _
            & "RecordExcel.grouping PIVOT Format_Step3.ClnVar;"
    It give me that my sql is error and expect INSECT, SELECT.....but what I need here is crosstab query.

    So if this is not a good way to do it, would you please give me some idea of how do you think? Thanks a lot!

    Quote Originally Posted by pbaldy View Post
    Not how I would do it, but arrStr is set before it contains anything.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    To fix this I'd use this technique to check the SQL:

    http://www.baldyweb.com/ImmediateWindow.htm

    As to how I'd do it, presuming the first recordset is returning a set of values you want filtered on, I'd get rid of the array and simply use the recordset:

    "...WHERE RecordExcel.grouping ='" & rst!grouping & "' GROUP BY..."
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Thank you for the website, it is very useful and I fixed the SQL problem
    However, I still have a question on the rst!grouping part, I need to transfer the different grouping to the different worksheets in one excel file, could you provide me some solution or web source show me how to do this in VBA? MANY MANY THANKS!!!

    Quote Originally Posted by pbaldy View Post
    To fix this I'd use this technique to check the SQL:

    http://www.baldyweb.com/ImmediateWindow.htm

    As to how I'd do it, presuming the first recordset is returning a set of values you want filtered on, I'd get rid of the array and simply use the recordset:

    "...WHERE RecordExcel.grouping ='" & rst!grouping & "' GROUP BY..."

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I know I've exported multiple queries to the same file name with TransferSpreadsheet and had it create multiple sheets. For more control, automation:

    http://support.microsoft.com/kb/247412
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Thanks pbaldy, but I didn't find any multiple queries transfer to the same file in the webset,
    I use
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "DivideGrp", "C:\Documents and Settings\WangA\My Documents\Export_Excel_Files\FNexcel.xls", True, rst!grouping

    in my VBA code, but it gives me nothing, nothing happened when I click the export button... Do you think there is anything wrong with my code?
    Again, Thank you so much!
    Quote Originally Posted by pbaldy View Post
    I know I've exported multiple queries to the same file name with TransferSpreadsheet and had it create multiple sheets. For more control, automation:

    http://support.microsoft.com/kb/247412

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The last argument is for a range, so I don't think you're providing that. I'd try without.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    I delete the range but my data still transfered on one worksheet, I try to put the code: "Set qdf2=Nothing" above "rst.MoveNext", and my access give me error that the Object variable or With block variable not set...

    Thank you so much for your patient....I am so despered now.....


    Quote Originally Posted by pbaldy View Post
    The last argument is for a range, so I don't think you're providing that. I'd try without.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you get this sorted? If memory serves, you can specify a sheet in the range argument like

    "SheetName!"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Sorry, I was out for meeting the whole week and wasn't able to reply you in time. I got the code work but now my supervisor want me to put a title in the cell A1 and push all the data start from cell A2 in excel...So I think I may need to change my code a lot to accomplish that....
    Also, if you have any idea on how to do it, any hits would be appreciate, Thanks a lot!!!

    Quote Originally Posted by pbaldy View Post
    Did you get this sorted? If memory serves, you can specify a sheet in the range argument like

    "SheetName!"

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Your options include modifying the query so it outputs the title first (perhaps a UNION query that adds that above the other query), and using automation to either do the whole process, or open the spreadsheet after running the existing process and inserting a row at the top. Automation gives you a lot of control:

    http://support.microsoft.com/kb/247412
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Thanks for the quick reply, I will dig into the automation and post my answer once I figure it out. Thanks again

    Quote Originally Posted by pbaldy View Post
    Your options include modifying the query so it outputs the title first (perhaps a UNION query that adds that above the other query), and using automation to either do the whole process, or open the spreadsheet after running the existing process and inserting a row at the top. Automation gives you a lot of control:

    http://support.microsoft.com/kb/247412

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, post back if you get stuck.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Hi, hate to bother you, but I did got stucked on how to code the automation
    Here is my code:

    Code:
    Private Sub ExprDetail_Click()
    Dim db As DAO.Database
    Dim qdf1 As DAO.QueryDef
    Dim qdf2 As DAO.QueryDef
    Dim qdf3 As DAO.QueryDef
    Dim rst As Recordset
    Dim recordCnt As Integer
    Dim recordNm As Integer
    Dim sql As String
    Dim sqlFull As String
    Dim arrStr As String
    Dim arrCount() As String
    Dim path As String
    Dim strPrmp As String
    Dim strttl As String
    Dim DflPath As String
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object
    
    'Start a new workbook in Excel
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.Workbooks.Add
    
    DflPath = "C:\Documents and Settings\WangA\My Documents\Export_Excel_Files\FNexcel.xls"
    strPrmp = "Would you like to name your Excel File?"
    strttl = "Excel export"
    path = InputBox(strPrmp, strttl, DflPath)
     
    Const JOIN_SEP = ""","""
    
    Set db = CurrentDb()
    Set qdf1 = db.QueryDefs("RecordExcel")
    Set rst = qdf1.OpenRecordset
    recordCnt = rst.RecordCount
    
    'Creat array for transfer excel
    ReDim arrCount(1 To recordCnt) As String
    rst.MoveFirst
    For recordNm = 1 To recordCnt
        Set qdf2 = db.QueryDefs("DivideGroup")
        Set qdf3 = db.QueryDefs("ExportFullWeek")
        
        arrCount(recordNm) = rst!grouping
        arrStr = """" & arrCount(recordNm) & """"
              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
        sqlFull = "SELECT weeks.Date AS [Week Ending], DivideGroup.* FROM DivideGroup RIGHT JOIN weeks ON DivideGroup.Date = weeks.date " _
                 & "WHERE (((weeks.year) Between Year(getstartdate()) And Year(getenddate()))) ORDER BY weeks.Date;"
        qdf3.sql = sqlFull
           
        'Add headers to the worksheet on row 1
        Set oSheet = oBook.Worksheets(recordNm)
        oSheet.Range("A1:E1").Value = "Add Title Test"
        
        'Transfer the Array to the worksheet starting at cell A2
        oSheet.Range("A2:Z1000").Value = qdf3
      
        oBook.SaveAs path
        oExcel.Quit
    
        rst.MoveNext
        qdf2.Close
        qdf3.Close
        
        Set qdf2 = Nothing
        Set qdf3 = Nothing
       
    Next recordNm
    
    End Sub
    The line in red always give me error said there is an invalid argument there. I am so not familiar with automation, Would you please have a look at my code and tell me how can I fix it? Thanks!!!!
    Quote Originally Posted by pbaldy View Post
    No problem, post back if you get stuck.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exporting to Excel - All Worksheets Highlighted
    By kristyspdx in forum Import/Export Data
    Replies: 3
    Last Post: 02-26-2013, 05:42 PM
  2. Exporting Records to Specific Worksheets and Fields
    By Kapelluschsa in forum Import/Export Data
    Replies: 1
    Last Post: 02-28-2012, 02:11 PM
  3. Replies: 7
    Last Post: 08-05-2011, 10:59 AM
  4. Exporting single query and single report.
    By rfhall50 in forum Programming
    Replies: 2
    Last Post: 02-18-2011, 12:08 PM
  5. Replies: 3
    Last Post: 11-02-2009, 04:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums