473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Update or Select Statement. "Error Too Few Parameters!"

129 100+
I am trying to code a SAVE button which will automatically change numerous rows of data within a table. I have created the appropriate query and checked it works when the form is open. However the coding doesn't work. I am not very good using the whole rst! syntax. I have attempted to get the code right and checked online for similar solutions however i have come up blank. The is as follows;

Expand|Select|Wrap|Line Numbers
  1.     Dim db As Database
  2.     Dim rst As Recordset
  3.  
  4.     Set db = CurrentDb()
  5.     Set rst = db.OpenRecordset("SELECT MaterialID, RangeID, ColourID, CarcussColourID, FasciaID, FasciaMaterialID, FasciaFinishID, SuppNo, PONo FROM ordlin WHERE (((ordlin.OrderNo)=[Forms]![frmSOLineDetails]![OrderNo]) AND ((ordlin.SuppNo)=[forms]![frmSOLineDetails]![SuppNo]) AND ((ordlin.ItemNo)=[forms]![frmSOLineDetails]![ItemNo])) OR (((ordlin.OrderNo)=[Forms]![frmSOLineDetails]![OrderNo]) AND ((ordlin.SuppNo)=[forms]![frmSOLineDetails]![SuppNo]) AND ((ordlin.ItemNoSub)=[forms]![frmSOLineDetails]![ItemNo])) OR (((ordlin.OrderNo)=[Forms]![frmSOLineDetails]![OrderNo]) AND ((ordlin.SuppNo)=[forms]![frmSOLineDetails]![SuppNo]) AND ((ordlin.ItemNoCmp)=[forms]![frmSOLineDetails]![ItemNo]))")
  6.  
  7.     DoCmd.SetWarnings False
  8.  
  9.     With rst
  10.         .Fields!MaterialID = cboMaterialDesc
  11.         .Fields!RangeID = cboRangeDesc
  12.         .Fields!ColourID = cboColourDesc
  13.         .Fields!CarcussColourID = cboCarcussColourDesc
  14.         .Fields!FasciaID = cboFasciaDesc
  15.         .Fields!FasciaMaterialID = cboFasciaMatDesc
  16.         .Fields!FasciaFinishID = cboFasciaFinDesc
  17.         .Fields!SuppNo = SuppNo
  18.         .Fields!PONo = PONo
  19.         .Update
  20.     End With
  21.     rst.Close
  22.     Set rst = Nothing
  23.  
  24.     DoCmd.SetWarnings True
Expand|Select|Wrap|Line Numbers
  1. Set rst = db.OpenRecordset("SELECT MaterialID, RangeID, ColourID, CarcussColourID, FasciaID, FasciaMaterialID, FasciaFinishID, SuppNo, PONo FROM ordlin WHERE (((ordlin.OrderNo)=[Forms]![frmSOLineDetails]![OrderNo]) AND ((ordlin.SuppNo)=[forms]![frmSOLineDetails]![SuppNo]) AND ((ordlin.ItemNo)=[forms]![frmSOLineDetails]![ItemNo])) OR (((ordlin.OrderNo)=[Forms]![frmSOLineDetails]![OrderNo]) AND ((ordlin.SuppNo)=[forms]![frmSOLineDetails]![SuppNo]) AND ((ordlin.ItemNoSub)=[forms]![frmSOLineDetails]![ItemNo])) OR (((ordlin.OrderNo)=[Forms]![frmSOLineDetails]![OrderNo]) AND ((ordlin.SuppNo)=[forms]![frmSOLineDetails]![SuppNo]) AND ((ordlin.ItemNoCmp)=[forms]![frmSOLineDetails]![ItemNo]))")
This code has been taken from the made up query which filters all the relevant information i require. Basically i would like the button to update all the fields within this query. The form itself is a single form. So the data changed would need changing to all rows of data within the query or rst.

I receive an error stating "Too few parameters Expected 3" however i have made sure all the details that could possibly be changed are on the form. I have still never managed to have a successful attempt at this sort of code so any advice would be much appreciated! Thanks
Oct 21 '08 #1
4 2316
Stewart Ross
2,545 Expert Mod 2GB
Hi Constantine Al. Although the database engine can interpret form control references when run from the query editor such direct form control references are not seen as valid fields when run from SQL in code for OpenRecordset and similar functions, hence the parameter error message.

To resolve it, the norm is to build the SQL string and refer to each control's current value within the string, rather than to the control itself:

Expand|Select|Wrap|Line Numbers
  1. Set rst = db.OpenRecordset("SELECT MaterialID, 
  2. ...
  3. WHERE (((ordlin.OrderNo)= " & [Forms]![frmSOLineDetails]![OrderNo] & ") AND ((ordlin.SuppNo)= " & [forms]![frmSOLineDetails]![SuppNo] & ") AND ... 
and so on.

In this case, because there are so many of these references to form controls it would probably be better to assign their values to variables and use these as the literals withiin your code:

Expand|Select|Wrap|Line Numbers
  1. Dim lngOrderNo as Long, lngSuppNo as Long ...
  2. lngOrderNo = Forms![frmSOLineDetails]![OrderNo]
  3. lngSuppNo = Forms![frmSOLineDetails]![SuppNo] 
  4. ...
  5. WHERE (((ordlin.OrderNo)= " & lngOrderNo & ") AND ((ordlin.SuppNo)= " & lngSuppNo & ") AND ... 
If the values concerned are strings rather than numbers you will need to add single quotes as delimiters before and after the literal value to make all this work, like this:

Expand|Select|Wrap|Line Numbers
  1.  "... WHERE [somefield] like '" & somestring & "' AND ..."
-Stewart
Oct 21 '08 #2
Constantine AI
129 100+
Hi thanks for the help. I have changed my code to what you told me to, now the only problem i have now is that i receive a write conflict error. I realise the Form is based on the same source as my Set rst so i know why i receive it. Is there a way i can stop this write conflict from happening? Here is my coding;

Expand|Select|Wrap|Line Numbers
  1.     Dim db As Database
  2.     Dim rst As Recordset
  3.     Dim lngOrderNo As Long
  4.     Dim strSuppNo As String
  5.     Dim lngItemNo As Long
  6.  
  7.     DoCmd.SetWarnings False
  8.  
  9.     lngOrderNo = Forms![frmSOLineDetails]![OrderNo]
  10.     strSuppNo = Forms![frmSOLineDetails]![SuppNo]
  11.     lngItemNo = Forms![frmSOLineDetails]![ItemNo]
  12.  
  13.     Set db = CurrentDb()
  14.     Set rst = db.OpenRecordset("SELECT MaterialID, RangeID, ColourID, CarcussColourID, FasciaID, FasciaMaterialID, FasciaFinishID, SuppNo, PONo FROM ordlin WHERE (((ordlin.OrderNo)= " & lngOrderNo & ") AND ((ordlin.SuppNo)= '" & strSuppNo & "') AND ((ordlin.ItemNo)= " & lngItemNo & ")) OR ((ordlin.OrderNo)= " & lngOrderNo & ") AND (((ordlin.SuppNo)= '" & strSuppNo & "') AND ((ordlin.ItemNoSub)= " & lngItemNo & ")) OR (((ordlin.OrderNo)= " & lngOrderNo & ") AND ((ordlin.SuppNo)= '" & strSuppNo & "') AND ((ordlin.ItemNoCmp)= " & lngItemNo & "))")
  15.  
  16.     Do
  17.         rst.Edit
  18.         rst.Fields!MaterialID = cboMaterialDesc
  19.         rst.Fields!RangeID = cboRangeDesc
  20.         rst.Fields!ColourID = cboColourDesc
  21.         rst.Fields!CarcussColourID = cboCarcussColourDesc
  22.         rst.Fields!FasciaID = cboFasciaDesc
  23.         rst.Fields!FasciaMaterialID = cboFasciaMatDesc
  24.         rst.Fields!FasciaFinishID = cboFasciaFinDesc
  25.         rst.Fields!SuppNo = SuppNo
  26.         rst.Fields!PONo = PONo
  27.         rst.Update
  28.         rst.MoveNext
  29.     Loop Until rst.EOF
  30.     rst.Close
  31.     Set rst = Nothing
  32.     DoCmd.Save
  33.     Me.Requery
  34.  
  35.     DoCmd.SetWarnings True
Oct 22 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi. I can only think that the write conflict arises because of a record lock being applied. Record locks usually reflect underlying editing operations, which perhaps may be outstanding at the time.

You can make sure that any outstanding changes to the current record are dealt with by using the form's Dirty property (which is set True if the underlying record has been changed but not saved):

Expand|Select|Wrap|Line Numbers
  1. IF me.dirty then me.dirty = false
This resetting of the Dirty property actually saves any changes to the current record. If you use this before running your recordset you can be sure that there are no unsaved edits causing record locks.

If you still receive write-conflict errors it would suggest that either the recordset is not updatable, or again a lock is applied, and it would be worth setting a breakpoint and stepping through your code line by line to see if it is all records that are affected or just one.

-Stewart
Oct 22 '08 #4
Constantine AI
129 100+
Yeah thankyou very much. It worked a treat!
Oct 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: RS | last post by:
I have below code and i get the following error when i run it... Procedure 'StoredProcedure1' expects parameter '@ID', which was not supplied. Am i setting parameters wrong? Thank you all...
7
by: sea# | last post by:
I'm writing an app to keep results of survey in DB. Using MS Access. This method is writing answers to DB. Each survey has one question. Each time an answer arrive to method, I check to which...
1
by: GG | last post by:
I have a very simple form that I am using to take in the information and save it to the MS Access Database using dataset. My DataAdapter is called dadConstituent and dataset is called...
3
by: PAUL | last post by:
Hello, I have 2 datasets I am trying to update. The parent table seems to update fine but when I go update the chiled table I get an error message that says I need a related record in the parent...
0
by: Henry Lee | last post by:
HI, all I am new to ASP 2.0 and I am trying hard to learn all objects of ..net. I keep got a error when I try to update a record in dataDetailView Object which using objectdatasource and DataSet....
6
by: BaWork | last post by:
I have the following to insert a new record: <% .. Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "DSN=qqqqq;" SQLStmt = "INSERT INTO tbl_qqqqqq (main_cat, cat_fee, fee_amount)...
2
by: truptidas | last post by:
Hi. I want to print invoice of select parameters, how do I go about it?
4
by: EmilyA | last post by:
Hi! I'm not that good at programming, so I was wondering if anyone could tell me why I'm getting an unreachable statement error for lines 18, 35, 68, and 86. import java.util.ArrayList; ...
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
8
by: Benniit | last post by:
Am using Vb.NET 2008 and SQL 2008. I have a problem, when the execution reaches daAdapter.Update(dtTable) then I receive this error ""String or binary data would be truncated. The statement has been...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.