Solved

Output MS Access Table to Excel using VB5

Posted on 1999-01-28
4
202 Views
Last Modified: 2013-12-25
OK.. I want to use VB to output an Access Table to an Excel Spreadsheet systematically, ie. simulate the OutputTo Command in MS ACCESS. Preferably something quiet simple.

Anyone got any ideas ??? Let me know and the points are YOURS !!!!!

0
Comment
Question by:bradyj
  • 2
4 Comments
 
LVL 2

Expert Comment

by:kswinney
Comment Utility
1)  Create a new EXE project in VB
2)  Add a reference to Microsoft DAO 3.5 library
3)  Add a reference to Microsoft Excel library
4)  Create a button named 'Command1' on the default form
5)  Insert the code below:
6)  Suggestion:  Add error handling also (not in the example)


Dim objExcel As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim dbNorthwind As DAO.Database

Private Sub Command1_Click()
    Dim sSQL As String
    Dim rsNorthwind As DAO.Recordset
   
    Set objExcel = CreateObject("Excel.Application")
    Set objBook = objExcel.Workbooks.Add
    Set objSheet = objBook.Worksheets(1)
   
    Set dbNorthwind = Workspaces(0).OpenDatabase("D:\VB5\NWIND.MDB")
    If Not dbNorthwind Is Nothing Then
        sSQL = "SELECT * FROM Employees"
        Set rsNorthwind = dbNorthwind.OpenRecordset(sSQL, dbOpenSnapshot)
        If (Not rsNorthwind Is Nothing) And (rsNorthwind.RecordCount > 0) Then
            'Set up the field names
            lRow = 1
            For lCol = 0 To rsNorthwind.Fields.Count - 1
                objSheet.Cells(lRow, lCol + 1) = rsNorthwind.Fields(lCol).Name
            Next
           
            'Now export the actual data (except for photos - dbLongBinary)
            Do Until rsNorthwind.EOF
                lRow = lRow + 1
                For lCol = 0 To rsNorthwind.Fields.Count - 1
                    If Not rsNorthwind(lCol).Type = dbLongBinary Then
                        vFieldValue = rsNorthwind(lCol)
                        If Not IsNull(vFieldValue) Then
                            objSheet.Cells(lRow, lCol + 1) = vFieldValue
                        End If
                    End If
                Next
               
                rsNorthwind.MoveNext
            Loop
        End If
        Set rsNorthwind = Nothing
    End If
    Set dbNorthwind = Nothing
   
    objExcel.Visible = True
   
    Set objSheet = Nothing
    Set objBook = Nothing
    Set objExcel = Nothing
End Sub


Cheers!

kswinney
0
 

Author Comment

by:bradyj
Comment Utility
Thanks for that answer... it works Yes, but its damn slow...

The table I want to output to excel via VB contains no less than 4000 records with 20 fields, therefore you are talking about a loop of 80000 approx. This makes it too slow.

The OutputTo Command in Access output the tables in about 3-5 seconds, therefore your code simulates this is the sense that it output the data correctly, but it does simulate the speed.

Sorry, I would like to give you the points... but...


0
 
LVL 2

Expert Comment

by:kswinney
Comment Utility
So... what you're saying is you're unable to modify the code in any way?

If you'd like for me to spell it out for you... C H A N G E  I T.

If you want it to export to a text file and then import, do it.

I've given you everything else you need.

sheesh!

kswinney.
0
 
LVL 3

Accepted Solution

by:
kfrick earned 100 total points
Comment Utility
Assuming that Access will be present on the Target Machine, why not just call the Access function?
In Access, create a Macro to do the Export for you. Then in VB5, use this sub:

Sub ExportToExcel
    Screen.MousePointer = vbHourglass
    Dim objAccess As New Access.Application
    objAccess.OpenCurrentDatabase (App.Path + "\" + gszDatabaseName)
    objAccess.DoCmd.SetWarnings False
    objAccess.DoCmd.RunMacro "Excel Output Macro", 1
    objAccess.CloseCurrentDatabase
    Screen.MousePointer = vbDefault
End Sub

Don't forget to add a reference to the Microsoft Access Object Library.

Hope this helps!
-kf

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now