![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi all, Using A2007. *I'm using an append query to load data into a temporary table. The query works fine when I run it from the query view. *But when I use Execute and reference it, or pass the SQL as a string, it brings up 'Run-time error '3061' - Too few parameters. Expected 2. I create the table using: ================================================== ========= Dim db As Database * * * * * * Dim temp_table As TableDef * * * * * * Set db = DBEngine(0)(0) * * * * * * Set temp_table = db.CreateTableDef("TEMPtblReport1") * * * * * * * * With temp_table * * * * * * * * * * .Fields.Append .CreateField("GroupID", dbInteger) * * * * * * * * * * .Fields.Append .CreateField("GroupName", dbText) * * * * * * * * * * .Fields.Append .CreateField("CompanyID", dbDouble) * * * * * * * * * * .Fields.Append .CreateField("CompanyName", dbText) * * * * * * * * * * .Fields.Append .CreateField("ProductID", dbText) * * * * * * * * * * .Fields.Append .CreateField("ProductName", dbText) * * * * * * * * * * .Fields.Append .CreateField("ValueOfSale", dbCurrency) * * * * * * * * * * .Fields.Append .CreateField("Quantity", dbInteger) * * * * * * * * * * .Fields.Append .CreateField("Discount", dbCurrency) * * * * * * * * * * .Fields.Append .CreateField("ListPrice", dbCurrency) * * * * * * * * * * .Fields.Append .CreateField("DateOfSale", dbDate) * * * * * * * * End With * * * * * * db.TableDefs.Append temp_table ================================================== ========= then try to append data to it using ================================================== ========== INSERT INTO TEMPtblReport1 (GroupID, GroupName, CompanyID, CompanyName, ProductID, ProductName, Quantity, ValueOfSale, Discount,[List Price], DateOfSale ) SELECT qryRetroGroupCompanyProductMonth.GroupID, qryRetroGroupCompanyProductMonth.GroupName, qryRetroGroupCompanyProductMonth.CompanyID, qryRetroGroupCompanyProductMonth.CompanyName, qryRetroGroupCompanyProductMonth.ProductID, qryRetroGroupCompanyProductMonth.ProductName, qryRetroGroupCompanyProductMonth.Quantity, qryRetroGroupCompanyProductMonth.ValueOfSale, qryRetroGroupCompanyProductMonth.Discount, qryRetroGroupCompanyProductMonth.[List Price], qryRetroGroupCompanyProductMonth.DateOfSale FROM qryRetroGroupCompanyProductMonth; ================================================ This works when running the query directly in query view, but gives the error when using db.Execute with either a query name or the query's SQL passed as a string. I'm going round and round in circles. *Can anyone help? Thanks, Franc. |
#3
| |||
| |||
|
|
FROM qryRetroGroupCompanyProductMonth; |
#4
| |||
| |||
|
|
On Tue, 7 Dec 2010 13:24:40 -0800 (PST), franc sutherland franc.sutherl... (AT) googlemail (DOT) com> wrote: FROM qryRetroGroupCompanyProductMonth; Please post the SQL of the above query? *Are there, for example, any WHERE clauses referencing a form control? Tony -- Tony Toews, Microsoft Access MVP Tony's Main MS Access pages -http://www.granite.ab.ca/accsmstr.htm Tony's Microsoft Access Blog -http://msmvps.com/blogs/access/ For a convenient utility to keep your users FEs and other files * updated seehttp://www.autofeupdater.com/ |
#5
| |||
| |||
|
|
When I change the form text box references to actual dates, the error does not occur. What would be the best way to incorporate the user's inputted dates into the query so that the append query will run? |
#6
| |||
| |||
|
|
franc sutherland<franc.sutherland (AT) googlemail (DOT) com> wrote in news:dd83adfc-fa0a-417a-aa11-b3e7306962e0 (AT) 29g2000prb (DOT) googlegroups.com : When I change the form text box references to actual dates, the error does not occur. What would be the best way to incorporate the user's inputted dates into the query so that the append query will run? You should define the control references as parameters, for one. |
#7
| |||
| |||
|
|
Instead of using the execute method on the Append query try using code toopen the query Docmd.OpenQuery "NameOfQuery" Or if you want to you can build the query string and input the values of the parameters. *It is difficult to say what to do consider you are nesting queries inside of nested queries. qryAvgPrice, qryCompanyDiscount, qryCompanyDiscountDateOfSale, qrySalesCompaniesProducts The simple thing to do might be to have a saved query that you can modifyby changing its SQL. Dim strSQL as String strSQL = "SELECT qryAvgPrice.DateOfSale, qryCompanyDiscount.GroupID, qryCompanyDiscount.GroupName, qryCompanyDiscount.GroupJoinCompanyID, qryCompanyDiscount.GroupJoinStartDate, qryCompanyDiscount.GroupJoinEndDate, qryCompanyDiscount.GroupDiscountProductID, qryCompanyDiscount.GroupDiscountStartDate, qryCompanyDiscount.GroupDiscountEndDate, IIf([GroupJoinStartDate]>[DateOfSale], 0,IIf([GroupJoinEndDate]<[DateOfSale], 0,IIf([GroupDiscountStartDate]>[DateOfSale], 0,IIf([GroupDiscountEndDate]<[DateOfSale],0, [GroupDiscountPercentageDiscount])))) AS DiscountToApply, qryAvgPrice.PricePrice AS[List Price] FROM qryAvgPrice INNER JOIN qryCompanyDiscount ON (qryAvgPrice.ProductID = qryCompanyDiscount.GroupDiscountProductID) AND (qryAvgPrice.CompanyID = qryCompanyDiscount.GroupJoinCompanyID)" Dim strWHere as String strWHERE = "WHERE qryAvgPrice.DateOfSale Between " & Format([Forms]![frmNewReports]![txtFrom],"\#yyyy-mm-dd\#") & " And " & Format([Forms]![frmNewReports]![txtTo],"\#yyyy-mm-dd\#") CurrentDb().QueryDefs("NameOfSavedquery").SQL = strSQL & strWhere The use that saved query in place of qryCompanyDiscountDateOfSale or you could chose to modify qryCompanyDiscountDateOfSale John Spencer Access MVP 2002-2005, 2007-2010 The Hilltop Institute University of Maryland Baltimore County On 12/8/2010 3:22 PM, David-W-Fenton wrote: franc sutherland<franc.sutherl... (AT) googlemail (DOT) com> *wrote in news:dd83adfc-fa0a-417a-aa11-b3e7306962e0 (AT) 29g2000prb (DOT) googlegroups.com : When I change the form text box references to actual dates, the error does not occur. *What would be the best way to incorporate the user's inputted dates into the query so that the append query will run? You should define the control references as parameters, for one. |
#8
| |||
| |||
|
|
I changed that to variables (David is that what you meant about defining the control references as parameters? |
#9
| |||
| |||
|
|
franc sutherland <franc.sutherl... (AT) googlemail (DOT) com> wrote innews:b392734f-4407-4b69-9bcc-788315253629 (AT) c17g2000prm (DOT) googlegroups.co m: I changed that to variables (David is that what you meant about defining the control references as parameters? No. This is what I mean by parameters: PARAMETERS [Forms]![frmInventory]![InventoryID] Long; SELECT tblInventory.* FROM tblInventory WHERE tblInventory.InventoryID=[Forms]![frmInventory]![InventoryID]; This makes the query optimizable, because the data type of the form control is defined in the parameter. It also causes certain things like Nulls in the referenced control to be handled in a more orderly fashion than when the control is not defined as a parameter. From A2002 on, it is essential that any control reference used in a WHERE clause be defined as a parameter. Before that, it worked OK without it, but something changed between A2000 and A2002 in the way control references were handled that broke the old system and from then on required the parameters in order to produce reliable results. -- David W. Fenton * * * * * * * * *http://www.dfenton.com/ contact via website only * *http://www.dfenton.com/DFA/ |
#10
| |||
| |||
|
|
On Dec 10, 3:39*am, "David-W-Fenton" <dfas... (AT) dfenton (DOT) com> wrote: franc sutherland <franc.sutherl... (AT) googlemail (DOT) com> wrote innews:b392734f- 4407-4b69-9bcc-788315253629 (AT) c17...ooglegroups.co m: I changed that to variables (David is that what you meant about defining the control references as parameters? No. This is what I mean by parameters: PARAMETERS [Forms]![frmInventory]![InventoryID] Long; SELECT tblInventory.* FROM tblInventory WHERE tblInventory.InventoryID=[Forms]![frmInventory]![InventoryID]; This makes the query optimizable, because the data type of the form control is defined in the parameter. It also causes certain things like Nulls in the referenced control to be handled in a more orderly fashion than when the control is not defined as a parameter. From A2002 on, it is essential that any control reference used in a WHERE clause be defined as a parameter. Before that, it worked OK without it, but something changed between A2000 and A2002 in the way control references were handled that broke the old system and from then on required the parameters in order to produce reliable results. Thanks for that. That looks like a useful thing. Can I ask what you meant by 'makes the query optimizable'? |
![]() |
| Thread Tools | |
| Display Modes | |
| |