Solved

Export a DAO recordset to excel?

Posted on 2000-04-25
4
990 Views
Last Modified: 2011-10-03
How to easily export a DAO recordset to EXCEL within a few line of code?
0
Comment
Question by:mr_anusorn
4 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2750500
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2750975
Use the CopyFromRecordset method.

gobjExcel.Sheets("Budget").Range("E10").CopyFromRecordset rst
0
 
LVL 4

Accepted Solution

by:
CraigLazar earned 100 total points
ID: 2752156
Hi try this this is a sample from one of my projects


be sure to click the referenece ExcelCtl type library in your project refrences of your project

Private Sub cmdSendExcelex_Click()
On Error GoTo eh:
 
  Dim tmpStartDate As Date
  Dim tmpEndDate As Date
 
 
  Dim xl As New Excel.Application
  Dim wb As Excel.Workbook
  Dim ws As Excel.Worksheet
  Dim tmpPercentCheck As String
 
  tmpStartDate = GetStartDate()
  tmpEndDate = GetEndDate()
 
  Dim db As Database
  Dim rsRead As Recordset
  Dim SQLRead As String
  Dim tmpCounter As Integer
 
  'Variables for the cells
    Dim tmpACell As String
    Dim tmpBCell As String
    Dim tmpCCell As String
    Dim tmpDCell As String
    Dim tmpECell As String
    Dim tmpFCell As String
    Dim tmpGCell As String
    Dim tmpHCell As String
 
    SQLRead = "Select * from Expenses where Date >= #" & tmpStartDate & "# and Date<=#" & tmpEndDate & "#;"
    tmpCounter = 0
    Set wb = xl.Workbooks.Open("c:\jimauto\Invoices.xls")
    Set ws = wb.Sheets("List Expenses")
     
    Set db = OpenDatabase("c:\Jimauto\Vat.mdb")
    Set rsRead = db.OpenRecordset(SQLRead, dbOpenSnapshot)
    If rsRead.RecordCount <> 0 Then
        tmpCounter = tmpCounter + 4
        rsRead.MoveFirst
        ws.Range("D1").Value = tmpStartDate
        ws.Range("F1").Value = tmpEndDate
        Do Until rsRead.EOF
            tmpCounter = tmpCounter + 1
            tmpACell = "A" & tmpCounter
            tmpBCell = "B" & tmpCounter
            tmpCCell = "C" & tmpCounter
            tmpDCell = "D" & tmpCounter
            tmpECell = "E" & tmpCounter
            tmpFCell = "F" & tmpCounter
           
            ws.Range(tmpACell).Value = rsRead("Date")
            ws.Range(tmpBCell).Value = rsRead("Company")
            ws.Range(tmpCCell).Value = rsRead("Memo")
            ws.Range(tmpDCell).Value = rsRead("Cost")
            ws.Range(tmpECell).Value = rsRead("Vat")
            ws.Range(tmpFCell).Value = rsRead("Total")
            rsRead.MoveNext
        Loop
            wb.Close SaveChanges:=True
            xl.Quit
            Set xl = Nothing
   
    End If
   
    MsgBox "finnished"
       
eh:
    If Err.Number = 1004 Then
        MsgBox "Excel File is open in excel on the server"
        Exit Sub
    End If

End Sub

hope this helps

:)
Craig
0
 
LVL 1

Expert Comment

by:mathies
ID: 2752966
Transfert you recordset into a array.
After you can directly transfer you array into Excell
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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

13 Experts available now in Live!

Get 1:1 Help Now