?
Solved

Runtime error '3061': Too few parameters. Expected 4.

Posted on 2015-01-21
15
Medium Priority
?
651 Views
Last Modified: 2016-02-10
Hello - I'm migrating some VBA code from using docmd.openquery to db.execute. I got this code from an example on the web:

    Dim db As DAO.Database
    Set db = CurrentDb()
    db.Execute "COM_CREATE_COMMISSIONS", dbFailOnError
    db.Execute "COM_CREATE_COMMISSIONS_GM", dbFailOnError
    db.Execute "COM_CREATE_COMMISSIONS_REV", dbFailOnError

The three queries (in quotes) are append queries, and when running I get the error above "Runtime error 3061': Too few parameters. Expected 4. I have tried quite a few permutations but this error seems to be the closest to actually running the code, so this is the example I'm posting hear. The three queries run fine (and ran fine using the docmd.openquery code), and they are in an Access 2013 database. The code did have some problems before I referenced the "Microsoft Office 15.0 Access database engine Object Library" and "Microsoft Data Access Components Installed Version", which seemed to fix that problem.

In summary, I'm just looking for the complete syntax for using the db.execute statement to run three already defined Access 2013 queries.  

Note - this is my workaround for suppressing Action Query messages, since it's the only published method to do so when using SharePoint Lists as the data source (which is the case for the queries in this case). The Action Query confirmations remained even though they were turned off in Client Options, the autoexec macro, and even in the code. So a solution to completely turn off these confirmations (when using SharePoint Lists) during an action query would also be equally effective for me, as I can simply use the docmd.openquery syntax.

Thanks!
0
Comment
Question by:maknit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
15 Comments
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40563701
You probably reference some form controls in the queries. These are parameters that have to be specified when calling by Execute:

Dim db As DAO.Database
Dim qd As DAO.QueryDef
     Set db = CurrentDb()
     Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS")
     qd.Parameters(0) = Forms!frmYourForm!txtSomeTextbox.Value
     qd.Parameters(1) = Forms!frmYourForm!txtSomeTextbox1.Value
     qd.Parameters(2) = Forms!frmYourForm!txtSomeTextbox2.Value
     qd.Parameters(3) = Forms!frmYourForm!txtSomeTextbox3.Value
     qd.Execute([Options])

Or by name:

     qd.Parameters("Forms!frmYourForm!txtSomeTextbox") = Forms!frmYourForm!txtSomeTextbox.Value

/gustav
0
 
LVL 58
ID: 40563935
If all your parameters are control references, you can also do a loop on the parameters collection and use Eval() to resolve the reference:

Dim prm as Parameter

For Each prm in qdf.Parameters
  prm.Value = Eval(prm.Name)
Next prm

Open in new window


Jim.
0
 

Author Comment

by:maknit
ID: 40565586
Hello - and thanks to both posts - very helpful. I have the exact code pasted below. I started seeing my app error out all over the place, so I had to uncheck the references - Microsoft Office 15.0 Access database engine Object Library and Microsoft Data Access Components Installed Version. That got rid of all the problems, but now the code below causes the error - "Compile error: User-defined type not defined". Any help would be great - and I've tried to enable the reference Microsoft DAO 3.6 Object Library but get a "Error in Loading DLL". I'm thinking that it doesn't apply to Access 2013, as I've validated the DLL and registered it multiple times to no avail.

Code:

 Dim db As DAO.Database
    Dim qd As DAO.QueryDef
      Set db = CurrentDb()
      Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS")
      qd.Parameters(0) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_ID].Value
      qd.Parameters(1) = [Forms]![frm_Main]![cmb_Current_Year].Value
      qd.Execute , dbFailOnError
      Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS_GM")
      qd.Parameters(0) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_ID].Value
      qd.Execute , dbFailOnError
      Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS_REV")
      qd.Parameters(0) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_ID].Value
      qd.Execute , dbFailOnError
    MsgBox "Commission transactions created"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40565850
You need this reference:

 Microsoft Office 15.0 Access database engine Object Library

/gustav
0
 

Author Comment

by:maknit
ID: 40567484
Thank you - got past the references error - now I think it's a syntax issue, but I can't seem to get past it. The following statement errors "Run-time error '3061': Too few parameters. Expected 4" :

qd.Execute (dbFailOnError)

 For some reason there aren't too many clear depictions of what the syntax should be. If I can get a syntax example, hopefully this should do it - thanks again.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40567947
It isn't a syntax question. It means that either one or more fields are misspelled or that some parameters haven't been assigned values.

It's easy to debug:

       Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS")
       qd.Parameters(0) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_ID].Value
       qd.Parameters(1) = [Forms]![frm_Main]![cmb_Current_Year].Value
       Debug.Print "SQL:", qd.SQL
       qd.Execute , dbFailOnError

Now study the SQL string and correct until success.

/gustav
0
 

Author Comment

by:maknit
ID: 40572153
Thanks Gustav - I missed the dependent queries. I need to look at it deeper, but I think the parameters in the dependent queries make up the 4 in the error. It sounds like using the Execute method requires all query parameters, in all levels of the queries, to be explicitly called out. I'll give that a try.

Thanks,

Mike
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40572162
Yes, that's a trap. The parameters must be defined on the lowest level as they are "carried forward" to overlying query.

/gustav
0
 

Author Comment

by:maknit
ID: 40597717
Hello - sorry for the long response time - I've been debugging and testing, and thank you again for the help. I've been able to get 2 out of the 3 queries working. The query that still doesn't seem to work is the first one - COM_CREATE_COMMISSIONS. I've tried all kinds of permutations of parameters, query defs, etc., but it's not appending a record that should be added (It's an append query).  Also, when I debug and check the SQL, it's running as expected, and when I copy that SQL and run separately, it works. It only doesn't work when in this DAO format. I'm thinking it might be the fact that the parameter [Forms]![frm_Commissions_Workbench]![txt_INV_COM_YEAR] occurs two times (in two of the underlying queries), and its only in the procedure once. I tried adding another parameter so that there are two [Forms]![frm_Commissions_Workbench]![txt_INV_COM_YEAR] parameters , but it fails. It doesn't fail with just one, but it doesn't add the record. So I'm thinking it's relating to the parameters and possibly that one of them occurs more than once, but is specified only once. And again, the other two queries (with one parameter) work fine.  Any ideas? The code follows:

Dim db As DAO.Database
    Dim qd As DAO.QueryDef
    Set db = CurrentDb()
      Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS")
      qd.Parameters(0) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_ID].Value
      qd.Parameters(1) = [Forms]![frm_Commissions_Workbench]![txt_INV_COM_YEAR].Value
      qd.Parameters(2) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_DATE].Value
      Debug.Print "SQL:", qd.SQL
      qd.Execute (dbFailOnError)
      Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS_GM")
      qd.Parameters(0) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_ID].Value
      Debug.Print "SQL:", qd.SQL
      qd.Execute (dbFailOnError)
      Set qd = db.QueryDefs("COM_CREATE_COMMISSIONS_REV")
      qd.Parameters(0) = [Forms]![frm_Commissions_Workbench]![txt_INV_APP_ID].Value
      Debug.Print "SQL:", qd.SQL
      qd.Execute (dbFailOnError)
    Rem DoCmd.OpenQuery "COM_CREATE_COMMISSIONS"
    Rem DoCmd.OpenQuery "COM_CREATE_COMMISSIONS_GM"
    Rem DoCmd.OpenQuery "COM_CREATE_COMMISSIONS_REV"
    MsgBox "Commission transactions created"

Thanks!
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40597789
What does Debug.Print "SQL:", qd.SQL return?

Try forcing a date value here:

   qd.Parameters(2) = DateValue([Forms]![frm_Commissions_Workbench]![txt_INV_APP_DATE].Value)

> I'm thinking it might be the fact that the parameter
> [Forms]![frm_Commissions_Workbench]![txt_INV_COM_YEAR] occurs two times

Did you specify it as a parameter in both queries?

/gustav
0
 

Author Comment

by:maknit
ID: 40598581
Hi Gustav,

I'll give the DateValue a try.

All these form controls are doing is functioning as query criteria. I haven't defined them as parameters. The other two that work don't have them defined as parameters, just criteria. I'll get back to you on the DateValue asap.
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 40598593
The trick is that defining them as parameters stops Access from trouble guessing what data type they could hold.

/gustav
0
 

Author Comment

by:maknit
ID: 40598601
Ok - thanks - I'll give that a try as well.
0
 

Author Comment

by:maknit
ID: 40598630
Thanks, Gustav - entering them as parameters resolved it. All queries are working as intended. Thanks again for all of your help!
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40598634
You are welcome!

/gustav
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

719 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