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; - Dim db As Database
-
Dim rst As Recordset
-
-
Set db = CurrentDb()
-
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]))")
-
-
DoCmd.SetWarnings False
-
-
With rst
-
.Fields!MaterialID = cboMaterialDesc
-
.Fields!RangeID = cboRangeDesc
-
.Fields!ColourID = cboColourDesc
-
.Fields!CarcussColourID = cboCarcussColourDesc
-
.Fields!FasciaID = cboFasciaDesc
-
.Fields!FasciaMaterialID = cboFasciaMatDesc
-
.Fields!FasciaFinishID = cboFasciaFinDesc
-
.Fields!SuppNo = SuppNo
-
.Fields!PONo = PONo
-
.Update
-
End With
-
rst.Close
-
Set rst = Nothing
-
-
DoCmd.SetWarnings True
- 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
4 2316
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: - Set rst = db.OpenRecordset("SELECT MaterialID,
-
...
-
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: - Dim lngOrderNo as Long, lngSuppNo as Long ...
-
lngOrderNo = Forms![frmSOLineDetails]![OrderNo]
-
lngSuppNo = Forms![frmSOLineDetails]![SuppNo]
-
...
-
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: - "... WHERE [somefield] like '" & somestring & "' AND ..."
-Stewart
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; - Dim db As Database
-
Dim rst As Recordset
-
Dim lngOrderNo As Long
-
Dim strSuppNo As String
-
Dim lngItemNo As Long
-
-
DoCmd.SetWarnings False
-
-
lngOrderNo = Forms![frmSOLineDetails]![OrderNo]
-
strSuppNo = Forms![frmSOLineDetails]![SuppNo]
-
lngItemNo = Forms![frmSOLineDetails]![ItemNo]
-
-
Set db = CurrentDb()
-
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 & "))")
-
-
Do
-
rst.Edit
-
rst.Fields!MaterialID = cboMaterialDesc
-
rst.Fields!RangeID = cboRangeDesc
-
rst.Fields!ColourID = cboColourDesc
-
rst.Fields!CarcussColourID = cboCarcussColourDesc
-
rst.Fields!FasciaID = cboFasciaDesc
-
rst.Fields!FasciaMaterialID = cboFasciaMatDesc
-
rst.Fields!FasciaFinishID = cboFasciaFinDesc
-
rst.Fields!SuppNo = SuppNo
-
rst.Fields!PONo = PONo
-
rst.Update
-
rst.MoveNext
-
Loop Until rst.EOF
-
rst.Close
-
Set rst = Nothing
-
DoCmd.Save
-
Me.Requery
-
-
DoCmd.SetWarnings True
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): - 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
Yeah thankyou very much. It worked a treat!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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....
|
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)...
|
by: truptidas |
last post by:
Hi.
I want to print invoice of select parameters, how do I go about it?
|
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;
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |