Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-21
15
Medium Priority
?
722 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
  • 7
  • 7
15 Comments
 
LVL 52

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 52

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 52

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 52

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 52

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 52

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 52

Expert Comment

by:Gustav Brock
ID: 40598634
You are welcome!

/gustav
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

824 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