Results 1 to 8 of 8
  1. #1
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86

    Runtime error 3061

    I get this error when I run the code below. Runtime error 3061 Too few parameters. Expected 1. Anyone know why this is happening?

    Update: I figured out it is because my query has a parameter. How do I pass that parameter on to the code? [Forms]![frmreports]![ID]



    Sub exportQueryADODB()


    Dim dbs As DAO.Database
    Dim rsTable As DAO.Recordset
    Dim rsQuery As DAO.Recordset


    Set dbs = CurrentDb


    Set rsQuery = dbs.OpenRecordset("qryAll")


    Set excelApp = CreateObject("Excel.application", "")
    excelApp.Visible = True
    Set targetWorkbook = excelApp.workbooks.Open("C:\Users\igonzalez\Docume nts\access.xlsx")
    targetWorkbook.Worksheets("Sheet2").Range("A5").Co pyFromRecordset rsQuery


    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes, it's the parameter. Simplest solution is to wrap the form reference in the Eval() function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86
    Quote Originally Posted by pbaldy View Post
    Yes, it's the parameter. Simplest solution is to wrap the form reference in the Eval() function.
    I ended up doing the following code and it worked.

    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter




    Set db = CurrentDb
    Set qdf = db.QueryDefs!qryControl
    For Each prm In qdf.Parameters
    prm.Value = Eval(prm.Name)
    Next prm






    Set rst = qdf.OpenRecordset(dbOpenDynaset)




    Set excelApp = CreateObject("Excel.application", "")
    excelApp.Visible = True
    Set targetWorkbook = excelApp.Workbooks.Open("S:\Technical Services\access.xlsx")
    targetWorkbook.Worksheets("Sheet2").Range("B3").Co pyFromRecordset rst

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes, that's the more involved method. Glad you got it working.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86
    With the code below how would I then save and close the excel workbook?


    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter




    Set db = CurrentDb
    Set qdf = db.QueryDefs!qryControl
    For Each prm In qdf.Parameters
    prm.Value = Eval(prm.Name)
    Next prm






    Set rst = qdf.OpenRecordset(dbOpenDynaset)




    Set excelApp = CreateObject("Excel.application", "")
    excelApp.Visible = True
    Set targetWorkbook = excelApp.Workbooks.Open("S:\Technical Services\access.xlsx")
    targetWorkbook.Worksheets("Sheet2").Range("B3").Co pyFromRecordset rst

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Untested, but try

    excelApp.Save
    excelApp.Quit
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86
    Thank you, below is the code that worked.

    targetWorkbook.Save
    excelApp.Quit

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help.
    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. Replies: 3
    Last Post: 02-26-2016, 12:34 PM
  2. Runtime Error 3061. Too few parameters, expected 2
    By Gina Maylone in forum Programming
    Replies: 35
    Last Post: 01-13-2014, 02:37 PM
  3. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  4. Runtime Error 3061 Expected 3
    By kumail123 in forum Programming
    Replies: 1
    Last Post: 03-28-2012, 09:44 AM
  5. Error 3061
    By Shanks in forum Queries
    Replies: 4
    Last Post: 09-16-2009, 07:13 AM

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