Output MS Access Table to Excel using VB5

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 !!!!!

bradyjAsked:
Who is Participating?
 
kfrickConnect With a Mentor Commented:
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
 
kswinneyCommented:
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
 
bradyjAuthor Commented:
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
 
kswinneyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.