+ Reply to Thread
Results 1 to 6 of 6

Export whole table with headers from Excel to Access

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Export whole table with headers from Excel to Access

    Hi,

    I am using similar code to transfer data from Excel to Access:

    http://stackoverflow.com/questions/1...s-access-table

    This first code is working fine but when you have >50 names of columns to write in string in SQL command - it can be hard.


    In the same source is another code connected with TransferSpreadsheet method -maybe it will be alternative method to copy whole table with headers to Access (create table automatically)?

    I am trying to use it to transport my table from sheet "Główny" to Access:

    Sub test()

    Dim Question As String
    Dim Connectstr As String
    Dim HurtowniaADO As Object
    Dim ZdanieSQL As String
    Dim Login As String
    Dim FileName As String
    Dim Lokalizacja_Pliku As String
    Dim Lokalizacja_Folderu As String
    Dim acc As Access.Application

    Set HurtowniaADO = CreateObject("ADODB.Connection")

    Question = MsgBox("Czy przegrać dane do Bazy?", vbYesNo)
    If Question = vbNo Then
    End
    End If

    Lokalizacja_Pliku = "C:\Users\ljar01\Desktop\Makro\Makro Brak Raportów\Brak_Raportow_Baza.accdb"
    Lokalizacja_Folderu = "C:\Users\ljar01\Desktop\Makro\Makro Brak Raportów\"

    FileName = "'" & ThisWorkbook.FullName & "'[Excel 8.0;]"

    Login = Environ("Username")

    Set acc = New Access.Application
    acc.OpenCurrentDatabase Lokalizacja_Pliku
    acc.DoCmd.TransferSpreadsheet , _
    transfertype:=acImport, _
    TableName:="tblExcelImport", _
    FileName:=Application.ActiveWorkbook.FullName, _
    HasFieldNames:=True, _
    Range:="Tabela29"
    acc.CloseCurrentDatabase
    acc.Quit
    Set acc = Nothing

    End Sub
    The example does not work. acImport - there is no variable here (why should i assing variable here if it is a property of TransferSpreadsheet method?)
    Maybe you have any ideas ?

    Jacek

    p.s. in attachment my workbook and in sheet This workbook on workbook_close you have my code there.
    Last edited by jaryszek; 09-02-2016 at 06:49 AM.

  2. #2
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Export whole table with headers from Excel to Access

    Hi,

    I am using similar code to transfer data from Excel to Access:

    http://stackoverflow.com/questions/1...s-access-table

    This first code is working fine but when you have >50 names of columns to write in string in SQL command - it can be hard.


    In the same source is another code connected with TransferSpreadsheet method -maybe it will be alternative method to copy whole table with headers to Access (create table automatically)?

    I am trying to use it to transport my table from sheet "Główny" to Access:

    Sub test()

    Dim Question As String
    Dim Connectstr As String
    Dim HurtowniaADO As Object
    Dim ZdanieSQL As String
    Dim Login As String
    Dim FileName As String
    Dim Lokalizacja_Pliku As String
    Dim Lokalizacja_Folderu As String
    Dim acc As Access.Application

    Set HurtowniaADO = CreateObject("ADODB.Connection")

    Question = MsgBox("Czy przegrać dane do Bazy?", vbYesNo)
    If Question = vbNo Then
    End
    End If

    Lokalizacja_Pliku = "C:\Users\ljar01\Desktop\Makro\Makro Brak Raportów\Brak_Raportow_Baza.accdb"
    Lokalizacja_Folderu = "C:\Users\ljar01\Desktop\Makro\Makro Brak Raportów\"

    FileName = "'" & ThisWorkbook.FullName & "'[Excel 8.0;]"

    Login = Environ("Username")

    Set acc = New Access.Application
    acc.OpenCurrentDatabase Lokalizacja_Pliku
    acc.DoCmd.TransferSpreadsheet , _
    transfertype:=acImport, _
    TableName:="tblExcelImport", _
    FileName:=Application.ActiveWorkbook.FullName, _
    HasFieldNames:=True, _
    Range:="Tabela29"
    acc.CloseCurrentDatabase
    acc.Quit
    Set acc = Nothing

    End Sub
    The example does not work. acImport - there is no variable here (why should i assing variable here if it is a property of TransferSpreadsheet method?)
    Maybe you have any ideas ?

    Jacek

    p.s. in attachment my workbook and in sheet This workbook on workbook_close you have my code there.
    Attached Files Attached Files
    Last edited by jaryszek; 09-02-2016 at 06:53 AM.

  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Export whole table with headers from Excel to Access

    I have found solution like this:

    Sub Example3()
    'the path to create the new access database
    Dim strPath As String
    'an Access object
    Dim objAccess As Access.Application
    Dim strExcelPath As String

    With ActiveSheet

    strPath = "C:\Users\ljar01\Desktop\Makro\Makro Brak Raportów\Brak_Raportow_Baza.accdb"
    strExcelPath = "C:\Users\ljar01\Desktop\Worksheet to Existing access table.xlsm"
    Set objAccess = New Access.Application

    Call objAccess.OpenCurrentDatabase(strPath)
    objAccess.Visible = True
    Call objAccess.DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel8, _
    "MyTable1", strExcelPath, True, "A1:D11")

    End With

    End Sub
    This is working good.

    The main question is: whe they are using "Call" here ?

    Jacek?

  4. #4
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Export whole table with headers from Excel to Access

    Anyone?

    Please help,
    Jacek

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Export whole table with headers from Excel to Access

    Hi,

    It is a matter of preference. If you do not use Call you must remove the brackets around the argument list.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Export whole table with headers from Excel to Access

    wow - ok very useful, thank you !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Using VBA in Excel to export from Excel into an Access database table
    By SemiAuto40 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-19-2014, 12:38 PM
  2. Dynamically create headers in Excelsheet using Access table values
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-08-2014, 07:11 AM
  3. Using Excel VBA to Export data to Ms.Access Table
    By ahmed_one in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-23-2013, 01:54 AM
  4. Export an Excel Tab to Access (Table)
    By ewong in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-04-2012, 07:12 PM
  5. Export Excel data into Access table - overwrite table data
    By Jonsocks in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-08-2012, 12:05 PM
  6. How do I export data with unkonwn numbers of table from Access to Excel?
    By graceey82 in forum Access Tables & Databases
    Replies: 0
    Last Post: 10-09-2009, 03:04 AM
  7. How to export Excel data to an Access table
    By Pixar in forum Excel General
    Replies: 4
    Last Post: 10-28-2008, 01:01 AM

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1