Solved

Output MS Access Table to Excel using VB5

Posted on 1999-01-28
4
207 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 100 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

726 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