Exporting Access Pivot tables to Excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michelle copper
    New Member
    • Aug 2011
    • 63

    Exporting Access Pivot tables to Excel

    Hi everyone,

    I need your help. I have a code which exports the query to excel but how do I add or in fact what code should I be adding to the existing code for me to export the pivot tables to excel as well?

    My code so far is:

    Code:
    Public Sub ExportXLS()
    
    #If Not CC_Debug Then
    On Error GoTo ErrProc
    #End If
        
        Const cQuery As String = "qryExportMetrics"
                     
      
        Dim fc As FileChooser
        Dim strFileName As String
    
        Set fc = New FileChooser
        fc.DialogTitle = "Select file to save"
        fc.OpenTitle = "Save"
        fc.Filter = "Excel Files (*.xls)"
        strFileName = Nz(fc.SaveFile, "")
        Set fc = Nothing
        
        ' If user selected nothing or canceled, quit
        If Len(strFileName) = 0 Then
            Exit Sub
        ' If file already exists, delete it
        ElseIf Len(Dir(strFileName)) > 0 Then
            Kill strFileName
        End If
        
        DoCmd.TransferSpreadsheet _
            acExport, _
            acSpreadsheetTypeExcel9, _
            cQuery, _
            strFileName, _
            HasFieldNames:=True
            
       
    
            
    ExitProc:
        Exit Sub
    ErrProc:
        ErrMsg Err, Err.Description, Err.Source
        Resume ExitProc
      
        End Sub
    Last edited by Niheel; Aug 22 '11, 08:39 PM. Reason: code tags
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    The following Code will Export ALL Data within Pivot Tables to individual Excel Spreadsheets. Specify the Base Path within the Constant conBASE_PATH, and the Table Name & .xls will be Appended to it. Be sure to substitute your own Table Names in place of the Demo Names. Is this what you are looking for?
    Code:
    Dim astrPivotTables As Variant
    Dim bytTblCtr As Byte
    Const conBASE_PATH As String = "C:\Exports\"
    
    'Populate an Array with ALL the Pivot Table Names
    astrPivotTables = Array("Customers", "Orders", "Order Details")
    
    'Export ALL Pivot Tables to their individual Spreadsheets
    For bytTblCtr = LBound(astrPivotTables) To UBound(astrPivotTables)
      DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, astrPivotTables(bytTblCtr), _
                                conBASE_PATH & astrPivotTables(bytTblCtr) & ".xls", True
    Next

    Comment

    • michelle copper
      New Member
      • Aug 2011
      • 63

      #3
      Hi ADezii,

      Thank you so much for your reply.
      I am still having problem with the codes you have given me.
      Sorry...I am very new to Access. The database which I am working on is built by one of the programmer who left the office.
      Do I put your code like this?


      Code:
      Public Sub ExportXLS()
      
      #If Not CC_Debug Then
      On Error GoTo ErrProc
      #End If
          
      Dim astrPivotTables As Variant
      Dim bytTblCtr As Byte
      Const qryExportMetrics As String =&nbs
      p;"C:\Exports\"
      
       
      'Populate an Array with ALL t
      he Pivot Table Names
      
      astrPivotTables = Array("MetricsID", "Activity", "Months")
      
       
      'Export ALL Pivot Tables to t
      heir individual Spreadsheets
      
      For bytTblCtr = LBound(astrPivotTables)
       To UBound(astrPivotTables)
      
        DoCmd.TransferSpreadsheet acExport,&n
      bsp;acSpreadsheetTypeExcel9, astrPivotTables(bytTb
      lCtr), _
      
               
               &
      nbsp;        &n
      bsp;conBASE_PATH & astrPivotTables(bytTbl
      Ctr) & ".xls", True
      
         
      
              
      ExitProc:
          Exit Sub
      ErrProc:
          ErrMsg Err, Err.Description, Err.Source
          Resume ExitProc
        
          End Sub

      and yes you are right... I wanted to export the data of the query in one sheet and the pivot tables in other spreadsheet.
      Last edited by Niheel; Aug 23 '11, 01:35 PM. Reason: please use code tags. [code] [/code]

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Michele, I do not fully understand the Syntax in Code Lines 9, 10, 31 and 32. Kindly explain.
        Code:
        Line # 9: Const qryExportMetrics As String =&nbs 
        Line #10: p;"C:\Exports\"
        Line #31: nbsp;        &n 
        Line #32: bsp;conBASE_PATH & astrPivotTables(bytTbl

        Comment

        • michelle copper
          New Member
          • Aug 2011
          • 63

          #5
          ADezii,
          I apologize for the mistake. I do not know how when I copy and paste the codes, it appeared to be different in the site. :S

          This is what I have been trying and it is giving me error:

          Code:
          Public Sub ExportXLS()
          
          #If Not CC_Debug Then
          On Error GoTo ErrProc
          #End If
              
          Dim astrPivotTables As Variant
          Dim bytTblCtr As Byte
          
          Const qryExportMetrics As String = "C:\Exports\"
           
          'Populate an Array with ALL the Pivot Table Names
          astrPivotTables = Array("Activity", "Visit Date")
           
          'Export ALL Pivot Tables to their individual Spreadsheets
          For bytTblCtr = LBound(astrPivotTables) To UBound(astrPivotTables)
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, astrPivotTables(bytTblCtr), _
                                      conBASE_PATH & astrPivotTables(bytTblCtr) & ".xls", True
          Next
                  
          ExitProc:
              Exit Sub
          ErrProc:
              ErrMsg Err, Err.Description, Err.Source
              Resume ExitProc
            
          End Sub
          I have also attached two image here regarding the query which I would like to export.
          I basically need to export the pivot tables and the data of 'qryExportMetri cs'

          Thank you so much again for your help. I really appreciate it.

          ** Edit **
          [imgnothumb]http://bytes.com/attachments/attachment/5342d1314126028/qryexportdata.j pg[/imgnothumb]
          [imgnothumb]http://bytes.com/attachments/attachment/5343d1314126028/qryexportpivot. jpg[/imgnothumb]
          Attached Files
          Last edited by NeoPa; Aug 24 '11, 01:09 PM. Reason: Tidied up post and added CODE tags

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            'Check and see if have have a similar type Statement somewhere in your code:
            Code:
            #Const CC_Debug = True
            then try the following and see if it works:
            Code:
            Public Sub ExportXLS()
            #If Not CC_Debug Then
              On Error GoTo ErrProc
            #Else
              On Error GoTo ExitProc
            #End If
            
            Dim astrPivotTables As Variant
            Dim bytTblCtr As Byte
            
            Const conBASE_PATH As String = "C:\Exports\"
            
            'Populate an Array with ALL the Pivot Table Names
            astrPivotTables = Array("Activity", "Visit Date")
            
            'Export ALL Pivot Tables to their individual Spreadsheets
            For bytTblCtr = LBound(astrPivotTables) To UBound(astrPivotTables)
              DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, astrPivotTables(bytTblCtr), _
                                        conBASE_PATH & astrPivotTables(bytTblCtr) & ".xls", True
            Next
            
            ExitProc:
              Exit Sub
              
            ErrProc:
              MsgBox Err.Description, Err.Source
                Resume ExitProc
            End Sub

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32645

              #7
              Michelle, see [CODE] Tags Must be Used for help with the tags. Don't post again without using them. If you're still confused after following the link then feel free to PM me for more help.

              PS. Check out When Posting (VBA or SQL) Code to save wasting time unnecessarily.
              Last edited by NeoPa; Aug 24 '11, 01:10 PM. Reason: Added PS.

              Comment

              • michelle copper
                New Member
                • Aug 2011
                • 63

                #8
                Thanks NeoPa! That was good info!

                Comment

                • michelle copper
                  New Member
                  • Aug 2011
                  • 63

                  #9
                  Adezii,

                  I tried those codes. It seems like there is an error with my pivot tables. I have attached an image again.

                  ** Edit **
                  [imgnothumb]http://bytes.com/attachments/attachment/5346d1314192167/error.jpg[/imgnothumb]
                  Attached Files
                  Last edited by NeoPa; Aug 24 '11, 02:21 PM. Reason: Made picture viewable

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    Michele, at this point there makes no sense going back and forth, and basically getting nowhere. If the Activity Table actually exists, I see no logical reason for the Error. Can you send me a sanitized Version of the Database so that I can see it first hand?

                    Comment

                    • michelle copper
                      New Member
                      • Aug 2011
                      • 63

                      #11
                      sure! I will PM you.
                      Thanks

                      Comment

                      • michelle copper
                        New Member
                        • Aug 2011
                        • 63

                        #12
                        I can't seemed to make attachment for PM. Anyways, I have attached a copy in here. The only problem I am having so far is not being able to export the Pivot Tables view.
                        I tried creating a form for exporting but it did not really help as well.
                        Attached Files

                        Comment

                        • ADezii
                          Recognized Expert Expert
                          • Apr 2006
                          • 8834

                          #13
                          1. Sorry Michele, I do not have Access 2003. Can you Convert the DB to this Version (20030 and Re-Attach?
                          2. Are you trying to Export the Pivot Tables View, or the Pivot Tables themselves?

                          Comment

                          • michelle copper
                            New Member
                            • Aug 2011
                            • 63

                            #14
                            sure.. I have attached two copies now..one in version 2002-2003 and one in 2000..

                            Yes..I am actually trying to export the pivot tables view as well as the raw data. Once you see my Access, I am sure you will understand what I am actually trying to do.

                            Thanks again
                            Attached Files

                            Comment

                            • ADezii
                              Recognized Expert Expert
                              • Apr 2006
                              • 8834

                              #15
                              As far as Exporting the Data in qryExportMetric s, the following adjustments will do it. To Export the actual Pivot Table View will be more difficult, and I'll have to look into it.
                              Code:
                              Public Sub ExportXLS()
                              #If Not CC_Debug Then
                                On Error GoTo ErrProc
                              #Else
                                On Error GoTo ExitProc
                              #End If
                               
                              Const conBASE_PATH As String = "C:\Exports\"
                              Const conEXPORT_OBJ  As String = "qryExportMetrics"
                              
                              'If the Folder C:\Exports does not exist, creatwe it
                              If Dir$(conBASE_PATH, vbDirectory) = "" Then
                               MkDir "C:\Exports"
                              End If
                               
                              DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, conEXPORT_OBJ, _
                                                          conBASE_PATH & conEXPORT_OBJ & ".xls", True
                               
                              ExitProc:
                                Exit Sub
                               
                              ErrProc:
                                MsgBox Err.Description, Err.Source
                                  Resume ExitProc
                              End Sub

                              Comment

                              Working...