Results 1 to 4 of 4

Thread: Access to Excel spreadsheet, sheet name?

  1. #1
    Join Date
    Jan 2012
    Posts
    2

    Access to Excel spreadsheet, sheet name?

    Hello,
    I have the following code (which i found), it basically reads a query from an Access database then exports it to an Excel

    spreadsheet. The only issue i have is i can't seem to SPECIFY A SHEETNAME? I wanna place it in Sheet2. Any ideas thanks, how

    can i incorporate a sheet reference into this code.
    Thanks,
    Jac

    Sub DAOCopyFromRecordSet(DBFullName As String, tablename As String, fieldname As String, targetrange As Range)

    Dim db As Database
    Dim rs As Recordset
    Dim intColIndex As Integer

    Set targetrange = targetrange.Cells(1, 1)

    Set db = OpenDatabase(DBFullName)
    Set rs = db.OpenRecordset(tablename, dbOpenTable) ' all records

    Set rs = db.OpenRecordset("SELECT * FROM " & tablename, dbReadOnly) ' filter records

    For intColIndex = 0 To rs.Fields.Count - 1

    targetrange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name

    Next


    ' write recordset
    targetrange.Offset(1, 0).CopyFromRecordset rs
    Set rs = Nothing
    db.Close
    Set db = Nothing

    End Sub

  2. #2
    Join Date
    May 2009
    Posts
    543

    Re: Access to Excel spreadsheet, sheet name?

    It is possible to define the sheet name while exporting access database to Microsoft Excel. I am just providing the code reference and hope that you can make changes in your script.

    Have a look on this :
    • Dim strExcelFile As String -- The Excel File
    • Dim strWorksheet As String -- The Worksheet
    • Dim strDB As String -- The Database Connection
    • Dim strTable As String -- The TABLE
    • Dim objDB As Database --- The Database

    So the changes that can be done is :
    • strExcelFile = "C:\My Documents\name-of-excel-file.xls"
    • strWorksheet = "name-of-worksheet"
    • strDB = "C:\My Documents\name-of-database.mdb"
    • strTable = "the-table"

  3. #3
    Join Date
    May 2009
    Posts
    511

    Re: Access to Excel spreadsheet, sheet name?

    There are certain more methods mentioned on the following link which can guide you for the process. Other than you can also use some readymade tool which can export the database directly in Excel without manipulating the codes. This is less time consuming.

    Methods for transferring data to Excel from Visual Basic

  4. #4
    Join Date
    May 2009
    Posts
    637

    Re: Access to Excel spreadsheet, sheet name?

    There is another code which I found on Microsoft forums and tried. It worked for me. All it does that it can help you to export the databse in a new sheet of said Excel file. So that you can later on rename the same as you want.

    Code:
    Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\...\My Documents\My Database\Access\database.accdb;")
    
    AccessConn.Open()
    
    'New sheet in Workbook
    Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [Excel 12.0 Xml;DATABASE=C:\Users\...\Documents\My Database\Excel\Excelfile.xlsx;HDR=Yes;].[Customers] from [Customers]", AccessConn)
    
        AccessCommand.ExecuteNonQuery()
        AccessConn.Close()

Similar Threads

  1. Replies: 6
    Last Post: 24-02-2012, 01:50 PM
  2. Replies: 2
    Last Post: 23-02-2012, 02:59 PM
  3. Apple ipad: how to work with spreadsheet or excel sheet
    By Baijnath in forum Portable Devices
    Replies: 3
    Last Post: 04-01-2011, 02:08 PM
  4. Importing Excel Spreadsheet
    By Theodore Long in forum Microsoft Project
    Replies: 3
    Last Post: 05-06-2009, 09:02 AM
  5. Corrupt spreadsheet in excel
    By Indrani in forum Windows Software
    Replies: 3
    Last Post: 06-05-2009, 03:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,727,025,490.74658 seconds with 17 queries