?
Solved

Output MS Access Table to Excel using VB5

Posted on 1999-01-28
4
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:kswinney
ID: 1499591
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
ID: 1499592
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
ID: 1499593
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 300 total points
ID: 1499594
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

801 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