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 November 2nd, 2007, 09:36 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default export data to excel using templates

Hi,

I'm trying to export data to an Excel template and saving it to the right location. Everything works fine, except for the exportation itself. Opening the template, saving it to the location, changing sheet names,... all works.

Here is the code I'm using:

Dim xl As Excel.Application
Dim wbk As Excel.Workbook
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim intAnswer As Integer
Dim dtDate As Date
Dim intDay As Integer
Dim intMonth As Integer
Dim intYear As Integer
Dim sTemplate As String
Dim intColonne As Integer
Dim intLigne As Integer
Dim fld As DAO.Field

If IsNull(Me.cboEinddatum) Or Me.cboEinddatum = "" Then
    MsgBox "Gelieve een einddatum in te vullen." & vbCrLf & vbCrLf & _
        "Veuillez entrer une date de fin.", vbExclamation, "CobelAdmin"
    Me.cboEinddatum.SetFocus
    Exit Sub
Else
    dtDate = Me.cboEinddatum
    intDay = DatePart("d", dtDate, vbMonday, vbFirstFourDays)
    intMonth = DatePart("m", dtDate, vbMonday, vbFirstFourDays)
    intYear = DatePart("yyyy", dtDate, vbMonday, vbFirstFourDays)
End If

        sTemplate = Application.CurrentProject.Path & "\Templates\Archief sleutels.xlt"
        'open the query qryARCHIVESleutels
        Set db = Application.CurrentDb
        sSQL = "SELECT tblOpvolgingSleutels.Autonumber, tblOpvolgingSleutels.DatumUit, " & _
                    "tblOpvolgingSleutels.UurUit, tblOpvolgingSleutels.NaamSleutel, " & _
                    "tblOpvolgingSleutels.Firma, tblOpvolgingSleutels.NaamUitlener, " & _
                    "tblOpvolgingSleutels.GsmUitlener, tblOpvolgingSleutels.Centralist, " & _
                    "tblOpvolgingSleutels.DatumIn, tblOpvolgingSleutels.UurIn, " & _
                    "tblOpvolgingSleutels.InvoerDoor, tblOpvolgingSleutels.InvoerOp, " & _
                    "tblOpvolgingSleutels.WijzigingDoor, tblOpvolgingSleutels.WijzigingOp " & _
                "FROM tblOpvolgingSleutels " & _
                "WHERE (((tblOpvolgingSleutels.DatumUit) <=" & dtDate & ") And " & _
                    "((tblOpvolgingSleutels.Centralist) Is Not Null) And " & _
                    "((tblOpvolgingSleutels.DatumIn) Is Not Null) And " & _
                    "((tblOpvolgingSleutels.UurIn) Is Not Null)) " & _
                "ORDER BY tblOpvolgingSleutels.DatumUit, tblOpvolgingSleutels.UurUit, " & _
                    "tblOpvolgingSleutels.NaamSleutel;"
        Set rst = db.OpenRecordset(sSQL, dbOpenDynaset)
        'start excel
        Set xl = New Excel.Application
        xl.Visible = False
        With xl
            'open the existing template
            Set wbk = .Workbooks.Open(sTemplate)
            'Change name of first sheet
            wbk.Sheets(1).Name = "Gegevens tem " & intYear & "_" & intMonth & "_" & intDay
            With wbk.Sheets(1)
                'transfer the data
                intLigne = 3
                While Not rst.EOF
                    intColonne = 1
                    For Each fld In rst.Fields
                        .Cells(intLigne, intColonne) = fld.Value
                        intColonne = intColonne + 1
                    Next
                    'next record
                    rst.MoveNext
                    intLigne = intLigne + 1
                Wend
            End With
            'close workbook and save it
            wbk.SaveAs Application.CurrentProject.Path & "\Archives\Archief sleutels tem " & _
                intYear & "_" & intMonth & "_" & intDay & ".xls"
            wbk.Close
        End With
        'close recordset and connection
        xl.Quit
        Set xl = Nothing
        rst.Close
        Set rst = Nothing
        Set db = Nothing

I have put the part of the code that is not working in red.
The exportation should start on row 3 in Excel, because row 1 and 2 are already in use on the template...
However, no data gets exported, the query is fine, I tested it.

Does anyone have any ideas why it is not working?

Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel (Data only) nikks9 ASP.NET 1.0 and 1.1 Professional 3 February 9th, 2011 05:01 AM
Export Data to Excel seananderson Access 2 January 16th, 2007 12:30 PM
Export Data to Excel msbsam ASP.NET 2.0 Basics 0 November 8th, 2006 02:09 AM
How to export data into Excel lily611 General .NET 8 January 11th, 2006 10:02 PM
export data to excel tulincim Classic ASP Databases 0 August 22nd, 2005 03:34 PM





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