Solved

Output MS Access Table to Excel using VB5

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 39
MsgBox 2 54
vb6 connector to SQL Server 2 37
Excel VBA - Run Time error '1004' Application-defined or object-defined error 4 81
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 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