Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 5th, 2008, 10:38 AM
Registered User
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Export to multiple Excel files using template

Newly registered here thanks to a friend's advice. My issue is the following...

I have some VBA code that gets a list of taxcodes and creates a crosstab query for each tax code. Each resulting crosstab is exported to an Excel spreadsheet using the taxcode as the name. example 010.xls, 020.xls, etc.

I can do this just fine and the code works great. Where i run into problems is when i attempt to use an Excel template for the exported files. This template contains a macro, and some headings that i need in each Excel spreadsheet that is exported.

With the code below, some strangeness happens. The first loop creates the 010.xls file, using the correct template, but it is blank. It then proceeds to open up multiple other Excel spreadsheets. These are in order of the taxcodes that i was looping through. For example....

When I open up the exported 010.xls file, it contains no data, but it also opens up 0101, 0202, 0303, 0404, etc. Now these "extra" Excel spreadsheets contain the correct data and use the template, but they arent really there! They seem to be some ghost files that are help temporarily because if i close everything, they dont come back.


Here is the code that i currently have running...

Quote:
quote:


Public Sub NewExport()

'Start of code
Dim qdf As DAO.QueryDef
Dim dbs As DAO.Database
Dim rstMgr As DAO.Recordset
Dim strSQL As String

'Excel object variables
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim sTemplate As String
Dim sTempFile As String
Dim sOutput As String
Dim iRow As Integer
Dim iCol As Integer
Dim iFld As Integer

Const cTabTwo As Byte = 1
Const cStartRow As Byte = 5
Const cStartColumn As Byte = 1


' Define the query that will be used for exporting
Const strQName As String = "qselExportCompare"

Set dbs = CurrentDb()

' Create tax entity recordset that will be looped through
strSQL = "SELECT DISTINCT sTaxCode FROM dbo_TIFTRFd_Entity;"
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)

' Loop through list of entities and export data to excel file by entity
If rst.EOF = False And rst.BOF = False Then
   rst.MoveFirst
   Do While rst.EOF = False

       strSQL = "TRANSFORM Sum(qSelExportFormat.nPYAmount) AS SumOfnPYAmount " _
           & "SELECT qSelExportFormat.sTaxCode, qSelExportFormat.sStateID, Sum(qSelExportFormat.nPYAmount) AS [Total Of nPYAmount] " _
           & "FROM qSelExportFormat " _
           & "WHERE sTaxCode = '" & rst!sTaxCode.Value & "' " _
           & "GROUP BY qSelExportFormat.sTaxCode, qSelExportFormat.sStateID " _
           & "PIVOT qSelExportFormat.[Full Account]; "

       Set qdf = CurrentDb.QueryDefs(strQName)
       qdf.Name = strQName
       qdf.sql = strSQL

       ' Set the locations of the template and the output
       sTemplate = "O:\TECHNOLOGY\TaxWeb\IFTRF\templates\IFTRFTemplat e.xls"
       sOutput = "O:\TECHNOLOGY\TaxWeb\IFTRF\Export\" & rst!sTaxCode.Value & ".xls"
       If Dir(sOutput) <> "" Then Kill sOutput
           FileCopy sTemplate, sOutput

                    ' Create the Excel Applicaiton, Workbook and Worksheet and Database object

                    Set appExcel = Excel.Application
                    Set wbk = appExcel.Workbooks.Open(sOutput)
                    Set wks = appExcel.Worksheets("Compare")
                    'sSQL = "select * from qrySales"
                    Set dbs = CurrentDb
                    Set erst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

                    ' For this template, the data must be placed on the 4th row, third column.
                    ' (these values are set to constants for easy future modifications)
                    iCol = cStartColumn
                    iRow = cStartRow
                    If Not erst.BOF Then erst.MoveFirst
                        Do Until erst.EOF
                           iFld = 0
                           lRecords = lRecords + 1
                           'Me.lblMsg.Caption = "Exporting record #" & lRecords & " to SalesOutput.xls"
                           'Me.Repaint

                           For iCol = cStartColumn To cStartColumn + (erst.Fields.Count - 1)
                              wks.Cells(iRow, iCol) = erst.Fields(iFld)

                              'If InStr(1, rst.Fields(iFld).Name, "Date") > 0 Then
                              ' wks.Cells(iRow, iCol).NumberFormat = "mm/dd/yyyy"
                              'End If

                              wks.Cells(iRow, iCol).WrapText = False
                              iFld = iFld + 1
                           Next

                           wks.Rows(iRow).EntireRow.AutoFit
                           iRow = iRow + 1

                           erst.MoveNext
                        Loop

                       ' Cleanup all objects (resume next on errors)
                       On Error Resume Next
                       'Set wks = Nothing
                       'Set wbk = Nothing
                       Set appExcel = Nothing
                       Set erst = Nothing
                       Set dbs = Nothing
                       DoCmd.Hourglass False


                    'End If

                    'ExportRequest = "Total of " & lRecords & " rows processed."
                    'Me.lblMsg.Caption = "Total of " & lRecords & " rows processed."
          'End If

       qdf.Close
       Set qdf = Nothing
       rst.MoveNext
   Loop
End If

rstMgr.Close
Set rstMgr = Nothing

dbs.Close
Set dbs = Nothing
'End of code

End Sub

This has to be something simple that i have overlooked because "theoretically" it does what it is supposed to do, just doesnt place the data in the Excel file that is generated.

Any help is GREATLY appreciated.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing Multiple Files Into Excel Using VB bsac Excel VBA 4 January 27th, 2014 11:37 AM
Export query param from Access form 2 XLS template gfranco Access VBA 0 April 14th, 2008 03:19 PM
export excel files collie ASP.NET 1.x and 2.0 Application Design 2 January 3rd, 2008 09:09 AM
How to export SQL RS data to Excel template johnilett BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 August 4th, 2005 03:39 AM
How to read multiple text files into excel? Together Excel VBA 4 March 5th, 2004 06:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.