Results 1 to 2 of 2

Thread: [FAQ's: OD] How do I transfer data between Excel and Access?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I transfer data between Excel and Access?

    You can perform the transfer many ways. Using Access VBA, ActiveX Data Objects and Excel's Object Model, etc. I'm not going to cover using other databases in this thread, just Access. For MS SQL Server, see this FAQ Thread or this FAQ Thread.

    You can do the transfer from the Excel point of view or also from the Access point of view. See this FAQ Thread for the Access examples.


    Excel 2003 VBA Code Example:

    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS ActiveX Data Objects 2.x Library
    3. Private Sub Command1_Click()
    4.     Dim oRs As ADODB.Recordset
    5.     Dim oCnn As ADODB.Connection
    6.     Dim i As Integer
    7.     'Connect to your Access db
    8.     Set oCnn = New ADODB.Connection
    9.     oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\RobDog888.mdb;User Id=admin;Password=;"
    10.     oCnn.Open
    11.     'Create your recordset
    12.     Set oRs = New ADODB.Recordset
    13.     oRs.Open "SELECT * FROM Table1;", oCnn, adOpenKeyset, adLockReadOnly, adCmdText
    14.     'Add to your current workbook and add the field names as column headers (optional)
    15.     For i = 0 To oRs.Fields.Count - 1
    16.         Workbooks("Book1").Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
    17.     Next
    18.     Workbooks("Book1").Sheets(1).Range("1:1").Font.Bold = True
    19.     Workbooks("Book1").Sheets(1).Cells(2, 1).CopyFromRecordset oRs
    20.     'Clean up ADO Objects
    21.     oRs.Close
    22.     Set oRs = Nothing
    23.     oCnn.Close
    24.     Set oCnn = Nothing
    25. End Sub
    Last edited by RobDog888; Aug 17th, 2007 at 08:18 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I transfer data between Excel and Access?

    Here is how you export your Access db table.
    Populate an ADO recordset, create a new Excel workbook, and then copy the entire recordset into a sheet, ex. sheet1.

    VB 6 And ADO Code Example:

    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Excel xx.0 Object Library
    3. 'Add a reference to MS ActiveX Data Objects 2.x Library
    4. Private Sub Command1_Click()
    5.     Dim oRs As ADODB.Recordset
    6.     Dim oCnn As ADODB.Connection
    7.     Dim oApp As Excel.Application
    8.     Dim oWB As Excel.Workbook
    9.     Dim i As Integer
    10.     'Connect to your Access db
    11.     Set oCnn = New ADODB.Connection
    12.     oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\RobDog888.mdb;User Id=admin;Password=;"
    13.     oCnn.Open
    14.     'Create your recordset
    15.     Set oRs = New ADODB.Recordset
    16.     oRs.Open "SELECT * FROM Table1;", oCnn, adOpenKeyset, adLockReadOnly, adCmdText
    17.     'Create an instance of Excel and add a new blank workbook
    18.     Set oApp = New Excel.Application
    19.     oApp.Visible = False
    20.     Set oWB = oApp.Workbooks.Add
    21.     'Add the field names as column headers (optional)
    22.     For i = 0 To oRs.Fields.Count - 1
    23.         oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
    24.     Next
    25.     oWB.Sheets(1).Range("1:1").Font.Bold = True
    26.     oWB.Sheets(1).Cells(2, 1).CopyFromRecordset oRs
    27.     'Clean up ADO Objects
    28.     oRs.Close
    29.     Set oRs = Nothing
    30.     oCnn.Close
    31.     Set oCnn = Nothing
    32.     'Clean up Excel Objects
    33.     oWB.Close SaveChanges:=True, FileName:="D:\Test.xls"
    34.     Set oWB = Nothing
    35.     oApp.Quit
    36.     Set oApp = Nothing
    37. End Sub
    Last edited by RobDog888; Aug 17th, 2007 at 08:20 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width