If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Help: Macro to Run query with param and export to excel

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-09, 10:55
Raysoc Raysoc is offline
Registered User
 
Join Date: Feb 2009
Posts: 11
Question Help: Macro to Run query with param and export to excel

Im new to this so please excuse my inexperience.

I need this macro to open up a query, the query has paramaters that i want to pass in the macro, then export it to an excel file, then run again with the second set of paramaters and append the query to the first excel file as a new sheet.

I will be doing this frequently and running entering and exporting manually is tiresome and time consuming.

Thanks!

My attempt thus far:

Sub test()

Dim ParamArr As Integer
ParamArr = Array(0, 50, 100, 125, 150, 175, 200, 300, 400, 500)


DoCmd.SetWarnings False

For i = 1 To ParamArr.Count
'Want to pass i and i+1 each time, untill the end it will be 500 and the second param will be empty as i want 500+
'Also instead of overwriting the xls file each time append new query as a new worksheet
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "sample", "C:\Documents and Settings\me\test.xls", True
Next
DoCmd.SetWarnings True

End Sub
Reply With Quote
  #2 (permalink)  
Old 02-13-09, 18:12
nckdryr nckdryr is offline
Computer Monkey
 
Join Date: May 2005
Posts: 1,191
Welcome to the forums!

Poots has a post in the code bank of Excel Automation that might be of some use to you.

Based on that, I've done some work like:
Code:
    On Error resume next
    
    'instantiate variables
    Dim db As DAO.Database, _
        rs As DAO.Recordset
    Dim exlApp As Excel.Application, _
        exlBook As Excel.Workbook, _
        exlSheet As Excel.Worksheet
    Dim strFilePath As String, _
        strExlSheetName As String, _
        strFormCurrency As String
    Dim intColCnt As Long, _
        intRowCnt As Long, _
        i As Long, _
        j As Long
    
    'Prepare recordset
    Set db = Application.CurrentDb
    Set rs = db.OpenRecordset(strSQL)  'define this on looping
    
    'Write data to file
    'Populate recordset to get accurate record count
    If Not rs.EOF Then rs.MoveLast
    rs.MoveFirst
    'Note: Excel arrays and cells start at 1, not 0, don't deduct 1
    intColCnt = rs.Fields.Count
    intRowCnt = rs.RecordCount
    i = 1
    'Write column headings
    For j = 0 To (intColCnt - 1)
        exlSheet.Cells(1, j + 1).Value = Nz(rs.Fields(j).Name, "")
    Next j
    'Write recordset data
    i = 2
    rs.MoveFirst
    While Not rs.EOF
        For j = 0 To (intColCnt - 1)
            exlSheet.Cells(i, j + 1).Value = Nz(rs.Fields(j).Value, "")
        Next j
        
        i = i + 1
        rs.MoveNext
    Wend
I'm sure this could be taken and adapted to your particular needs. You'll just have to put this in your loop and change the SQL every time.

FYI, when I copied this code over I omitted a bunch of other stuff, so I'm not guaranteeing this code will work "as-is", but it should be enough to give you a good start.
__________________
Me.Geek = True
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On