Results 1 to 14 of 14
  1. #1
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33

    Exporting query to Excel. Almost there

    I have a query based on a combobox that works fine, and a function that I 'borrowed' from a message board that exports a query to Excel. That also works fine except when I use it with a query that is based on a combobox.



    I get an error that says "Error 3061 too Few Parameters. Expected 1."

    Here is my query:

    Code:

    SELECT tblItem.Pallet, tblItem.PO, tblItem.Dept, tblItem.Class, tblItem.ItemNumber, tblItem.VesselVoyage, tblItem.Container, tblItem.Cartons, tblItem.UnitsPerCarton, tblItem.EA, tblItem.ItemDescription, tblItem.DamageDescriptionFROM tblItemWHERE (((tblItem.SalvagePickupName)=[Forms]![frmMain]![cboSalvagePickup]));
    And here is my code:

    Code:

    Private Sub btnSalvageReport_Click() Call SalvageToExcel("qrySalvageReport", "Sheet1", "J:\MyFolder\qrySalvageReport.xlsx")End Sub
    Which calls this function:

    Code:
    Code:
    Public Function SalvageToExcel(strTQName As String, strSheetName As String, strFilePath As String)
    ' strTQName is the name of the table or query you want to send to Excel
    ' strSheetName is the name of the sheet you want to send it to
    ' strFilePath is the name and path of the file you want to send this data into.
        Dim rst As DAO.Recordset
        Dim ApXL As Object
        Dim xlWBk As Object
        Dim xlWSh As Object
        Dim fld As DAO.Field
        Dim strPath As String
        Const xlCenter As Long = -4108
        Const xlBottom As Long = -4107
        On Error GoTo err_handler
        strPath = strFilePath
        Set rst = CurrentDb.OpenRecordset(strTQName)
        Set ApXL = CreateObject("Excel.Application")
        Set xlWBk = ApXL.Workbooks.Open(strPath)
        ApXL.Visible = True
        Set xlWSh = xlWBk.Worksheets(strSheetName)
        xlWSh.Range("A1").Select
        For Each fld In rst.Fields
            ApXL.ActiveCell = fld.Name
            ApXL.ActiveCell.Offset(0, 1).Select
        Next
        rst.MoveFirst
        xlWSh.Range("A2").CopyFromRecordset rst
        xlWSh.Range("A1:L1").Select
        With ApXL.Selection
            .Font.Name = "Arial"
            .Font.Size = 12
            .Borders.LineStyle = xlContinuous
            .Borders.ColorIndex = xlAutomatic
        End With
        With ApXL.Selection
            .Interior.Color = RGB(153, 204, 51)
            .HorizontalAlignment = xlCenter
            .RowHeight = 30
            .VerticalAlignment = xlCenter
        End With
        ApXL.ActiveSheet.Cells.Select
        ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
        ApXL.ActiveSheet.Cells.HorizontalAlignment = xlCenter
        xlWSh.Range("A1").Select
        rst.Close
        Set rst = Nothing
    Exit_SendSalvageToExcel:
        Exit Function
    err_handler:
        DoCmd.SetWarnings True
        MsgBox Err.Description, vbExclamation, Err.Number
        Resume Exit_SendSalvageToExcel
    End Function
    Any ideas?

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Your code is unreadable like that, but I suspect you're running into this:

    http://support.microsoft.com/default...b;en-us;209203

    Another solution is to wrap the form reference in the query in the Eval() function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33
    Sorry, fixed my code. I will check the link.

  4. #4
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33
    thanks for your reply, this might help. I didn't think that I needed to feed it the parameter in code since it is written right into the query.

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Sadly, yes. DAO can't resolve the form reference, hence the error.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33
    Sadly, it appears that I can't resolve the form reference either.

  7. #7
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    What's your code now, if you're using the method from the link?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33
    Quote Originally Posted by pbaldy View Post
    What's your code now, if you're using the method from the link?
    I started first by trying to define the parameter before calling the function, but couldn't get that, so I decided to butcher the function instead. I got this far yesterday and gave up. I think that I can maybe get one thing working, but combining these two things (the export to excel with this new parameter part) has me over my head.

    Right now it stops on the definition of "strTQName![Forms!frmMain!cboSalvagePickup]" and says "qualifier must be a collection"

    Code:
    Public Function SalvageToExcel(strTQName As String, strSheetName As String, strFilePath As String)
    ' strTQName is the name of the table or query you want to send to Excel
    ' strSheetName is the name of the sheet you want to send it to
    ' strFilePath is the name and path of the file you want to send this data into.
        Dim rst As DAO.Recordset
        Dim ApXL As Object
        Dim xlWBk As Object
        Dim xlWSh As Object
        Dim fld As DAO.Field
        Dim strPath As String
        Dim strSearchName As String
        Const xlCenter As Long = -4108
        Const xlBottom As Long = -4107
        On Error GoTo err_handler
        strPath = strFilePath
        
        
        strSearchName = Forms![frmMain]![cboSalvagePickup]
        strTQName![Forms!frmMain!cboSalvagePickup] = strSearchName    <-------------------Qualifier must be a collection
        Set rst = CurrentDb.OpenRecordset(strTQName)
        Set ApXL = CreateObject("Excel.Application")
        Set xlWBk = ApXL.Workbooks.Open(strPath)
        ApXL.Visible = True
        Set xlWSh = xlWBk.Worksheets(strSheetName)
        xlWSh.Range("A1").Select
        For Each fld In rst.Fields
            ApXL.ActiveCell = fld.Name
            ApXL.ActiveCell.Offset(0, 1).Select
        Next
        rst.MoveFirst
        xlWSh.Range("A2").CopyFromRecordset rst
        xlWSh.Range("A1:L1").Select
        With ApXL.Selection
            .Font.Name = "Arial"
            .Font.Size = 12
            .Borders.LineStyle = xlContinuous
            .Borders.ColorIndex = xlAutomatic
        End With
        With ApXL.Selection
            .Interior.Color = RGB(153, 204, 51)
            .HorizontalAlignment = xlCenter
            .RowHeight = 30
            .VerticalAlignment = xlCenter
        End With
        ApXL.ActiveSheet.Cells.Select
        ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
        ApXL.ActiveSheet.Cells.HorizontalAlignment = xlCenter
        xlWSh.Range("A1").Select
        rst.Close
        Set rst = Nothing
    Exit_SendSalvageToExcel:
        Exit Function
    err_handler:
        DoCmd.SetWarnings True
        MsgBox Err.Description, vbExclamation, Err.Number
        Resume Exit_SendSalvageToExcel
    End Function

  9. #9
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    That link can be confusing. Here are the relevant lines:


    Dim qdfMyQuery As DAO.QueryDef
    Dim dbSample As DAO.Database
    Dim rstCountOrders As DAO.Recordset
    Dim strSearchName As String

    Set dbSample = CurrentDb()
    Set qdfMyQuery = dbSample.QueryDefs("qryCustomerOrdersParameter")
    strSearchName = Forms![frmSearch]![txtCustomerToFind]
    qdfMyQuery![Forms!frmSearch!txtCustomerToFind] = strSearchName

    Set rstCountOrders = qdfMyQuery.OpenRecordset()

    Note the important part in the last line that a lot of people miss; the recordset is opened on the QueryDef.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33
    Yeah, I JUST got a positive result and it was that last line that I noticed was different.

    I decided to scrap the function, since it was not really re-useable with my other reports because the formatting is different anwyay, so I stripped everything out and put it behind the button to see if I could get the basics working and it seems like it does.

    Set rst = CurrentDb.OpenRecordset(qdf)

    became

    Set rst = qdf.OpenRecordset()

    and now it appears to work (only one test so far). I can't say that I really understand it.

  11. #11
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33
    Ignore this post.. solved my own issue.

    recordset doesn't work unless you're on the last record.

    rst.moveLast
    rowcount = rst.recordcount + 1

    solved it.


    Thanks again for your help, I think this thread is finished.

  12. #12
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    Wombat is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    33
    pbaldy, I just started working on another report where i have to read two date parameters and query for records that fall between. In trying to figure it out, I found this great bit of code...


    Code:
    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
    Dim rst As DAO.Recordset
        Set dbs = CurrentDb
        Set qdf = dbs.QueryDefs("qryWeeklyDamageReport")
        
        For Each prm In qdf.Parameters
            prm = Eval(prm.Name)
        Next prm
        
        Set rst = qdf.OpenRecordset
    The eval(prn.Name) appears to make the VBA read the parameters as they're written in the query. It seems a simpler way of assigning them, assuming you are referencing controls on a form.

  14. #14
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    I agree, that's how I'd do it for more than one parameter.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exporting a Query to Excel
    By tcheck in forum Access
    Replies: 3
    Last Post: 07-22-2011, 05:00 PM
  2. Query Exporting to Excel
    By Coffee in forum Queries
    Replies: 2
    Last Post: 07-18-2011, 07:42 AM
  3. Exporting query to Excel file with password?
    By jvera524 in forum Access
    Replies: 0
    Last Post: 12-06-2010, 11:24 AM
  4. Exporting Query to Excel
    By jvera524 in forum Import/Export Data
    Replies: 0
    Last Post: 12-06-2010, 09:16 AM
  5. Exporting from Query to existing Excel wksht
    By kfschaefer in forum Import/Export Data
    Replies: 0
    Last Post: 04-07-2006, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums