Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: [RESOLVED] Export From Microsoft Access to Excel

  1. #1

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Resolved [RESOLVED] Export From Microsoft Access to Excel

    How do I export data from Microsoft Access to Microsoft Excel?
    Both are Version 2002. Thank you.

  2. #2
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Export From Microsoft Access to Excel

    You could take your pick from about 10 ways. Are you trying to do it with VBA? Are you looking for a whole table? A built in query? Will the SQL statement be variable?

  3. #3

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    Yes I'm trying to do it with VBA and whole table too. I'm not sure about the other 2.

  4. #4
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Export From Microsoft Access to Excel

    Set a reference to the Microsoft DAO Object Library by clicking "Tools" then "References".

    Then edit the following.
    Code:
    Sub PasteTable()
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
        Dim i As Integer
        
        Set dbs = OpenDatabase("c:\mydatabase.mdb")
        Set rst = dbs.OpenRecordset("SELECT * FROM mytable;")
        
        If Not IsNull(rst.Fields(0)) Then
            For i = 0 To rst.Fields.Count - 1
                Cells(1, i + 1).Value = rst.Fields(i).name
            Next i
            Range("A2").CopyFromRecordset rst
        End If
        
        rst.Close
        dbs.Close
        Set rst = Nothing
        Set dbs = Nothing
    End Sub

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Export From Microsoft Access to Excel

    If you are going to run it from Access then just write this
    Code:
    DoCmd.RunCommand acCmdExport
    @mikeyc1204:
    The above process will be slow and DAO is too Old now. Use of ADO is always preffered over DAO.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Export From Microsoft Access to Excel

    I just assumed the process would be done in Excel. Not sure why :\

    And I was under the impression that ADO is better in most cases, but DAO is faster for simple jet interaction. Wrong?

  7. #7
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Export From Microsoft Access to Excel

    IF this process is going to be done in Excel then it is even more easier. This is how I would do it
    1. Open a Excel File.
    2. Select Record New Macro from Tools-->Macro Menu.
    3. Once the recording has started, open Data Menu and select Import Data from Import External Data Menu.
    4. Select the Database, and then select the table you want to import.
    5. All other dialogs are self Explanatory.
    6. Once you are done with this, stop the macro recording.
    7. Press ALT + F11 and you will see that VBA has written all the code for you.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: Export From Microsoft Access to Excel

    One line of code in an Access module. alot easier.
    [Highlight=VB]Application.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", "D:\Book1.xls", False, "Sheet1$"[/vbcoe]
    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

  9. #9
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Export From Microsoft Access to Excel

    Quote Originally Posted by RobDog888
    One line of code in an Access module. alot easier.
    [Highlight=VB]Application.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", "D:\Book1.xls", False, "Sheet1$"[/vbcoe]
    I had also shown a similar one line in my previous post
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: Export From Microsoft Access to Excel

    Yes, but then you get the dialog popup.

    Booooo! *SLAP*
    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

  11. #11
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Export From Microsoft Access to Excel

    Quote Originally Posted by RobDog888
    Yes, but then you get the dialog popup.

    Booooo! *SLAP*
    But then you have the liberty of saving the file anywhere in your PC (even on your network share)

    slap slap..
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: Export From Microsoft Access to Excel

    But if you want to export the table or query results to a specific range in a specific sheet you cant do that with the runcommand. Plus you can always prompt the user for the export location/file if need be. The main plus is the ability to place the data in a particular sheet/cell.


    Ps, SLAP SLAP SLAP
    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

  13. #13
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Export From Microsoft Access to Excel

    Lets do it then

    Your are n MVP so you will be better.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: Export From Microsoft Access to Excel

    In a module...
    Code:
    Private Sub ExportMeDynamically()
        Dim strPath As String
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = False
            .ButtonName = "Export As"
            .Filters.Add "Excel file types only (*.xls)", "*.xls", 1
            .FilterIndex = 1
            .InitialFileName = "Book1.xls"
            .InitialView = msoFileDialogViewDetails
            .Title = "RobDog888's Access to Excel Export™"
        End With
        If Application.FileDialog(msoFileDialogFilePicker).Show = -1 Then
            strPath = Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Item(1)
            Application.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", strPath, False, "Sheet2$"
        End If
    End Sub
    HA HA Great smilie

    Thanks
    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

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

    Re: Export From Microsoft Access to Excel

    Note: my code above is for Appending to an EXISTING workbook only. If you want it to work with new files only then you need to use a different file picker dialog.
    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

  16. #16

    Re: Export From Microsoft Access to Excel

    Does SP change anything?
    Last edited by DakDarie; Feb 6th, 2006 at 08:18 AM.

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

    Re: Export From Microsoft Access to Excel

    SP ?
    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

  18. #18

    Re: Export From Microsoft Access to Excel

    Service Pack

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

    Re: Export From Microsoft Access to Excel

    Office SP, considering this code, I highly doubt it. Windows SP, still unlikely.
    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

  20. #20

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    Wow, there are so many options that I duno which one to follow.

    How do we open data menu after the recording has started?

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

    Re: Export From Microsoft Access to Excel

    When your recording a macro just use your mouse to click the menu items that you would normally do to export it.
    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

  22. #22

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    I dun have an existing Excel workbook so how do I change the file picker dialog?

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

    Re: Export From Microsoft Access to Excel

    Quick and simple example of a basic save as dialog.
    Code:
    Dim oDLG As FileDialog
    Dim sFile As String
    Set oDLG = Application.FileDialog(msoFileDialogSaveAs)
    oDLG.Show
    If oDLG.SelectedItems.Count = 1 Then
        sFile = oDLG.SelectedItems.Item(1)
    End If
    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

  24. #24

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    This is what I have done. Can u check whether it is correct?

    'from Shuja Ali
    1. Open a Excel File.
    2. Select Record New Macro from Tools-->Macro Menu.
    3. Once the recording has started, open Data Menu and select Import Data from Import External Data Menu.
    4. Select car.mdb
    5. Stop the macro recording.
    7. Press ALT + F11

    In the Macro..module window i type
    VB Code:
    1. Private Sub ExportMeDynamically()
    2.     Dim oDLG As FileDialog
    3. Dim sFile As String
    4. Set oDLG = Application.FileDialog(msoFileDialogSaveAs)
    5. oDLG.Show
    6. If oDLG.SelectedItems.Count = 1 Then
    7.     sFile = oDLG.SelectedItems.Item(1)
    8. End If
    9. End Sub

    Did I miss something? Sorry I'm new in this.

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

    Re: Export From Microsoft Access to Excel

    No, if you want to go the recording a macro route then all you need to do is record the macro doing your manual steps necessary to import the access data.
    Then press Alt+F11 to view the generated code in the VBA IDE's Modules folder.

    You were mixing two different proposed methods
    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

  26. #26

    Re: Export From Microsoft Access to Excel

    You need a chat people :P

  27. #27
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Export From Microsoft Access to Excel

    Lets cheat.. here's one I prepared earlier..

    http://www.vbforums.com/showthread.php?t=352849

    Look at my first post
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  28. #28

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    RobDog888, I had saw one post which you helped and I tried writing it in my VB application.

    VB Code:
    1. Private Sub cmdData_Click()
    2. Dim oRs As adodb.Recordset
    3.     Dim oCnn As adodb.Connection
    4.     Dim oApp As Excel.Application
    5.     Dim oWB As Excel.Workbook
    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=" & _
    10.     App.Path & "\Car.mdb;Persist Security Info=False"
    11.     oCnn.Open
    12.  
    13.     'Create your recordset
    14.     Set oRs = New adodb.Recordset
    15.     oRs.Open "SELECT * FROM Table1;", oCnn, adOpenKeyset, adCmdText
    16.  
    17.     'Create an instance of Excel and add a new blank workbook
    18.     Set oApp = New Excel.Application
    19.     oApp.Workbooks.Open (App.Path & "\Book1.xls")
    20.     oApp.Visible = True
    21.     Set oWB = oApp.Workbooks.Add
    22.  
    23.     'Add the field names as column headers (optional)
    24.     For i = 0 To oRs.Fields.Count - 1
    25.         oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
    26.     Next
    27.     oWB.Sheets(1).Range("1:1").Font.Bold = True
    28.     oWB.Sheets(1).Cells(2, 1).CopyFromRecordset oRs
    29.     oRs.Close
    30.     Set oRs = Nothing
    31.     oCnn.Close
    32.     Set oCnn = Nothing
    33.     Set oWB = Nothing
    34.     Set oApp = Nothing
    35. End Sub

    May I know why does duplicate worksheets of Book1 appear? Eg: Book2, 3, 4 of the same data...

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

    Re: Export From Microsoft Access to Excel

    Not sure what context the code was concerning for the thread but its adding a new workbook with this line. If you dont need/want it then use the ammedments below.
    Code:
    oApp.Workbooks.Open (App.Path & "\Book1.xls")
    'Should be
    Set oWB = oApp.Workbooks.Open (App.Path & "\Book1.xls")
    
    'Take this line out.
    'Set oWB = oApp.Workbooks.Add
    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

  30. #30

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    Great. Thank u so much. I hope I did the correct thing of exporting Microsoft Access to Excel dynamically.
    I think I'm starting to like programming, but I'm still far from good

  31. #31
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  32. #32

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    Thanks Radjesh for the tips but I tink I just stick to RobDog888's since it worked. One question though.
    After using the code below, I managed to export the whole table from Access to Excel. How do I specify that I want to export eg 10 out of 14 fields from Access to Excel?
    Meaning though my table has 14 fields, I want to show only the first 10 fields in Excel.
    I tried to use oApp.Columns = 10 but instead all the Excel cells has digit 10 in it.

    VB Code:
    1. Private Sub cmdData_Click()
    2. Dim oRs As adodb.Recordset
    3.     Dim oCnn As adodb.Connection
    4.     Dim oApp As Excel.Application
    5.     Dim oWB As Excel.Workbook
    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=" & _
    10.     App.Path & "\Car.mdb;Persist Security Info=False"
    11.     oCnn.Open
    12.  
    13.     'Create your recordset
    14.     Set oRs = New adodb.Recordset
    15.     oRs.Open "SELECT * FROM Table1;", oCnn, adOpenKeyset, adCmdText
    16.  
    17.     'Create an instance of Excel and add a new blank workbook
    18.     Set oApp = New Excel.Application
    19.     Set oWB = oApp.Workbooks.Open (App.Path & "\Book1.xls")
    20.     oApp.Visible = True
    21.    
    22.     'Add the field names as column headers (optional)
    23.     For i = 0 To oRs.Fields.Count - 1
    24.         oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
    25.     Next
    26.     oWB.Sheets(1).Range("1:1").Font.Bold = True
    27.     oWB.Sheets(1).Cells(2, 1).CopyFromRecordset oRs
    28.     oRs.Close
    29.     Set oRs = Nothing
    30.     oCnn.Close
    31.     Set oCnn = Nothing
    32.     Set oWB = Nothing
    33.     Set oApp = Nothing
    34. End Sub
    I think I'm starting to like programming, but I'm still far from good

  33. #33
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Export From Microsoft Access to Excel

    In your select query instead of writing * explicitly mention the Field Names that you want to export
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  34. #34

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    Like this? But Excel still shows all the fields in my table
    oRs.Open "SELECT CustId,CustCom,CustName,ComCertNo,CustAddr,CustOPhone,CustMobile,CustFax,CustEmail,CustRemark FROM Table1 ", oCnn, adOpenKeyset, adCmdText
    I think I'm starting to like programming, but I'm still far from good

  35. #35
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Export From Microsoft Access to Excel

    I don't see why Excel should be doing this. Your recordset contains just 10 fields and it should export only 10 fields.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: Export From Microsoft Access to Excel

    This is an easy one, my code contains a loop where its listing out the columns in the sql query. So if your adding the field names to the select list then your loop will only contain that number of fields. You have 10 fields listed in your sql statement so that is how many columns your going to have.

    The MaxColumns argument is another way to limit the number of columns copied from the recordset.

    VB Code:
    1. oRs.Open "SELECT CustId, CustName FROM Table1 ", oCnn, adOpenKeyset, adCmdText
    2.     'Now you will have only 2 columns listed
    3.     'Add the field names as column headers (optional)
    4.     For i = 0 To oRs.Fields.Count - 1
    5.         oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
    6.     Next
    7.     oWB.Sheets(1).Range("1:1").Font.Bold = True
    8.     oWB.Sheets(1).Cells(2, 1).CopyFromRecordset Data:=oRs, ,MaxColumns:=2
    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

  37. #37

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    Sorry but I still can't get it to show me the specified no. of fields. I've been using Adodc1 as ADO Data Control. Is this the case cos I didn't define it in the code.
    The reason I've to show lesser fields is becos, my MSHFlexgrid only shows 10 of the fields. I kind of want to show what is on the MSHFlexgrid, is displayed in Excel.
    I think I'm starting to like programming, but I'm still far from good

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

    Re: Export From Microsoft Access to Excel

    Can you post your code? I dont think it may be the same if using the ADODC Data control. Not 100% sure as I havent tried it with it but it is an ADO control so lets see what we can do.
    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

  39. #39

    Thread Starter
    Lively Member yanty's Avatar
    Join Date
    Nov 2005
    Location
    Singapore
    Posts
    83

    Re: Export From Microsoft Access to Excel

    This is the code i usually connect to my database.
    VB Code:
    1. Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    2. App.Path & "\Car.mdb;Persist Security Info=False"
    3.  
    4. Adodc1.RecordSource = "SELECT * FROM Table1 "

    I tried to change to Adodc and it gives me a headache.
    VB Code:
    1. Private Sub cmdData_Click()
    2. Dim oRs As adodb.Recordset
    3.     Dim oApp As Excel.Application
    4.     Dim oWB As Excel.Workbook
    5.     Dim i As Integer
    6.     'Connect to your Access db
    7.     'Set oCnn = New adodb.Connection
    8.     Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    9.     App.Path & "\Car.mdb;Persist Security Info=False"
    10.     Adodc1.Recordset.Open
    11.  
    12.     'Create your recordset
    13.     Set oRs = Adodc1.Recordset
    14.     oRs.Open "SELECT Custid, CustName FROM Table1;", Adodc1, adOpenKeyset, adCmdText
    15.  
    16.     'Create an instance of Excel and add a new blank workbook
    17.     Set oApp = New Excel.Application
    18.     Set oWB = oApp.Workbooks.Open(App.Path & "\Book1.xls")
    19.     oApp.Visible = True
    20.         'Add the field names as column headers (optional)
    21.     For i = 0 To oRs.Fields.Count - 1
    22.         oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
    23.     Next
    24.     oWB.Sheets(1).Range("1:1").Font.Bold = True
    25.     oWB.Sheets(1).Cells(2, 1).CopyFromRecordset Data:=oRs, MaxColumns:=2
    26.     oRs.Close
    27.     Set oRs = Nothing
    28.     Adodc1.Recordset.Close
    29.     'Set oCnn = Nothing
    30.     Set oWB = Nothing
    31.     Set oApp = Nothing
    32. End Sub
    I think I'm starting to like programming, but I'm still far from good

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

    Re: Export From Microsoft Access to Excel

    Are you getting any errors?
    Looks like your running tow recordsets. One adodc and one ors.
    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

Page 1 of 2 12 LastLast

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