473,385 Members | 1,409 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,385 software developers and data experts.

rst.Edit and Update Problem

129 100+
Hi I am trying to insert certain information into a table based on a unique id. In terms of the Data Environment, it is within Stock and BOM Details. I have enforced a Button to do this command however it only works on 1 row at a time instead of moving onto the next, which i have coded it too. Can anyone tell me why or how to fix this issue. I can't keep clicking on the different rows followed by the button.

I am using Microsoft Access 2007:

Expand|Select|Wrap|Line Numbers
  1.     Dim Reply As String
  2.     Dim strSQL As String
  3.     Dim strSQL2 As String
  4.     Dim db As Database
  5.     Dim rst As Recordset
  6.  
  7.     strSQL = "INSERT INTO stkmas ([StkShortDesc],[Width],[Depth],[Height]) SELECT [StkShortDesc],[Width],[Depth],[Height] FROM preordlin WHERE IsNull(StkID)"
  8.     strSQL2 = "INSERT INTO preordlin ([OrderNo],[StkID],[StkShortDesc],[Width],[Depth],[Height],[Qty]) SELECT forms!frmCustomerOrderForm!sfrmSOHeader!OrderNo AS OrderNo,[SubStkID],[stkmas.StkShortDesc],preordlin.Width,preordlin.Depth,preordlin.Height,[Qty] FROM (stkbommas INNER JOIN stkmas ON stkbommas.substkID = stkmas.StkID) WHERE stkbommas.StkID = forms!frmPreSOLine!StkID"
  9.  
  10.     StkID.value = DLookup("[StkID]", "stkmas", "[StkShortDesc] = Forms![frmPreSOLine]![StkShortDesc]")
  11.     Price.value = DLookup("[SalePrice1]", "primas", "[StkID] = Forms![frmPreSOLine]![StkID]")
  12.  
  13.     If IsNull(StkID) Then
  14.         Reply = MsgBox("There are Stock Items present that do not belong to our existing Stock files. Would you like to ADD them?", vbYesNo, "None Existent Stock Details!")
  15.         If Reply = vbYes Then
  16.             DoCmd.RunSQL strSQL
  17.         Else
  18.         End If
  19.     Else
  20.     End If
  21.  
  22.     DoCmd.SetWarnings False
  23.  
  24.     Set db = CurrentDb()
  25.     Set rst = db.OpenRecordset("SELECT * FROM preordlin")
  26.  
  27.     Do Until rst.EOF
  28.         rst.Edit
  29.         DoCmd.RunSQL strSQL2
  30.         rst.Update
  31.         rst.MoveNext
  32.     Loop Until rst.EOF
  33.     rst.Close
  34.     Me.Requery
  35.     Set rst = Nothing
  36.  
  37.     DoCmd.SetWarnings True
With this coding i receive no error however it removes the StkShortDesc and Price from the actual fields and still only displays data for the first row. I have checked books and the internet forums, to my point of view this code should work however it doesn't. Any help would be much appreciated!

Thanks

Chris
Aug 21 '08 #1
7 3838
ADezii
8,834 Expert 8TB
  1. The syntax that you are using is not correct, one option would be:
    Expand|Select|Wrap|Line Numbers
    1. Do While Not rst.EOF
    2.   rst.Edit
    3.     DoCmd.RunSQL strSQL2
    4.   rst.Update
    5.   rst.MoveNext
    6. Loop
  2. I have never seen this type of code syntax before where you are executing multiple Action Queries (INSERT), inserting Records into a Table, while at the same time looping through a Recordset based on the very same Table that Records are being inserted into. I honestly can't believe that the code works at all, and if it should work, I believe that it will eventually lead to major conflicts down the line.
  3. I feel as though your code needs to be drastically restructured but don't take my word on it, see what some of the other Admins/Moderators/Experts have to say on the Topic.
Aug 21 '08 #2
FishVal
2,653 Expert 2GB
I would agree with ADezii. This code will never work as you are expecting
Expand|Select|Wrap|Line Numbers
  1. While Not You = DavidBlaine
  2. Wend
  3.  
Kind regards,
Fish
Aug 21 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi. ADezii and FishVal have said much of what needs to be said here - your code is in need of considerable revision!

If you process a recordset within a loop the .Edit and .Update methods apply to what you do using the recordset's fields directly - not to what you do using separate DoCmd's to run SQL updates. The .Update is intended for field-level updates of the current record, and only the current record, within that recordset instance. Bear in mind that SQL updates can apply to many tables simultaneously, and many records within them, and you will start to see the erroneous linking of updates with recordset processing that is going on.

Each time you use OpenRecordset you open a copy of the current query or table concerned. Using external updates that bypass the recordset within its processing loop will, at the very least, generate update anomalies between what is in the loop and what you have updated using the RunSQL statements. Once a recordset has been opened the data is effectively static until you close and re-open it, unless you change field values using the recordset's own methods to do so (not by running external SQL updates).

You can use the .Addnew method to insert a new record in a recordset - but then you have to set the field values individually. It is not done using an SQL statement. Loop processing of recordsets is record-by-record; SQL inserts and updates are set-oriented. The two are very different.

I hesitate to think what will go on if values are changed the way you are doing - I have no real idea what will happen to the values read by the recordset for the remaining records in the loop, because the mixed syntax is distinctly non-standard.

-Stewart
Aug 21 '08 #4
nico5038
3,080 Expert 2GB
I would expect the strSQL2 to be depend on value(s) from the extracted row.

Something like:


Expand|Select|Wrap|Line Numbers
  1.     Dim Reply As String
  2.     Dim strSQL As String
  3.     Dim strSQL2 As String
  4.     Dim db As Database
  5.     Dim rst As Recordset
  6.  
  7.     strSQL = "INSERT INTO stkmas ([StkShortDesc],[Width],[Depth],[Height]) SELECT [StkShortDesc],[Width],[Depth],[Height] FROM preordlin WHERE IsNull(StkID)"
  8.  
  9.     StkID.value = DLookup("[StkID]", "stkmas", "[StkShortDesc] = Forms![frmPreSOLine]![StkShortDesc]")
  10.     Price.value = DLookup("[SalePrice1]", "primas", "[StkID] = Forms![frmPreSOLine]![StkID]")
  11.  
  12.     If IsNull(StkID) Then
  13.         Reply = MsgBox("There are Stock Items present that do not belong to our existing Stock files. Would you like to ADD them?", vbYesNo, "None Existent Stock Details!")
  14.         If Reply = vbYes Then
  15.             DoCmd.RunSQL strSQL
  16.         Else
  17.         End If
  18.     Else
  19.     End If
  20.  
  21.     DoCmd.SetWarnings False
  22.  
  23.     Set db = CurrentDb()
  24.     Set rst = db.OpenRecordset("SELECT * FROM preordlin")
  25.  
  26.     Do Until rst.EOF
  27.         rst.Edit
  28.     strSQL2 = "INSERT INTO preordlin ([OrderNo],[StkID],[StkShortDesc],[Width],[Depth],[Height],[Qty]) SELECT forms!frmCustomerOrderForm!sfrmSOHeader!OrderNo AS OrderNo,[SubStkID],[stkmas.StkShortDesc],preordlin.Width,preordlin.Depth,preordlin.Height,[Qty] FROM (stkbommas INNER JOIN stkmas ON stkbommas.substkID = stkmas.StkID) WHERE stkbommas.StkID =" & rst.stkID
  29.         DoCmd.RunSQL strSQL2
  30.         rst.Update
  31.         rst.MoveNext
  32.     Loop Until rst.EOF
  33.     rst.Close
  34.     Me.Requery
  35.     Set rst = Nothing
  36.  
  37.     DoCmd.SetWarnings True
Probably you should also move the warning message into the rst loop, or better create a checking loop before the INSERT loop.

Nic;o)
Aug 21 '08 #5
Constantine AI
129 100+
Hi all, thanks for all your views. I know my coding is bad (sucks) i didn't know of any other way to process the information. The reason why i am doing this is because i have imported a csv file which contains the top level details of a particular stock item. My client wishes to process that information and distinguish the Sub-Stock and Component Details that go with the Stock item in question and insert these details into the orderline. For example:

CSV File

StockShortDesc, Qty, Width, depth and Height

300 DL Unit,1,300,250,600

Pre-order Line

Stock ID, StockShortDesc, Qty, Width, depth and Height

201, 300 DL Unit, 1, 300, 250, 600

With the click of the button it finds all the related Sub-Stock Details and Inserts it into the Pre-Order Line, so for example:

201, 300 DL Unit (Contains)

140, BaseCarcDrawer
389, Feet
161, DrawerFrontFascia
401, DoorFasciaDrawer

And so on the 140 Sub-Stock Item contains Components as well, so by the time i have finished i have a long list of components within the orderline. My clients wants it this way as changes maybe required during the assembly line.

Table wise i have self-joins:

Stockmas table - StkBOMmas table - Stockmas table - StkCmpBOMmas table
StkID - StkID, SubStkID - StkID - SubStkId, CmpID
(One) - (Many) - (One) - (Many)

I know this seems messy however the coding for the button works, it assigns the details according and correctly, however if you click on the same StkID twice it duplicates the values, but i can stop that. It assigns the SubStock details as well correctly but again duplicates when clicked upon twice.

Is there no way i can loop this procedure to all rows of data?
Aug 21 '08 #6
Constantine AI
129 100+
I would expect the strSQL2 to be depend on value(s) from the extracted row.

Something like:


Expand|Select|Wrap|Line Numbers
  1.     Dim Reply As String
  2.     Dim strSQL As String
  3.     Dim strSQL2 As String
  4.     Dim db As Database
  5.     Dim rst As Recordset
  6.  
  7.     strSQL = "INSERT INTO stkmas ([StkShortDesc],[Width],[Depth],[Height]) SELECT [StkShortDesc],[Width],[Depth],[Height] FROM preordlin WHERE IsNull(StkID)"
  8.  
  9.     StkID.value = DLookup("[StkID]", "stkmas", "[StkShortDesc] = Forms![frmPreSOLine]![StkShortDesc]")
  10.     Price.value = DLookup("[SalePrice1]", "primas", "[StkID] = Forms![frmPreSOLine]![StkID]")
  11.  
  12.     If IsNull(StkID) Then
  13.         Reply = MsgBox("There are Stock Items present that do not belong to our existing Stock files. Would you like to ADD them?", vbYesNo, "None Existent Stock Details!")
  14.         If Reply = vbYes Then
  15.             DoCmd.RunSQL strSQL
  16.         Else
  17.         End If
  18.     Else
  19.     End If
  20.  
  21.     DoCmd.SetWarnings False
  22.  
  23.     Set db = CurrentDb()
  24.     Set rst = db.OpenRecordset("SELECT * FROM preordlin")
  25.  
  26.     Do Until rst.EOF
  27.         rst.Edit
  28.     strSQL2 = "INSERT INTO preordlin ([OrderNo],[StkID],[StkShortDesc],[Width],[Depth],[Height],[Qty]) SELECT forms!frmCustomerOrderForm!sfrmSOHeader!OrderNo AS OrderNo,[SubStkID],[stkmas.StkShortDesc],preordlin.Width,preordlin.Depth,preordlin.Height,[Qty] FROM (stkbommas INNER JOIN stkmas ON stkbommas.substkID = stkmas.StkID) WHERE stkbommas.StkID =" & rst.stkID
  29.         DoCmd.RunSQL strSQL2
  30.         rst.Update
  31.         rst.MoveNext
  32.     Loop Until rst.EOF
  33.     rst.Close
  34.     Me.Requery
  35.     Set rst = Nothing
  36.  
  37.     DoCmd.SetWarnings True
Probably you should also move the warning message into the rst loop, or better create a checking loop before the INSERT loop.

Nic;o)
Thanks for that coding nico5038 it nearly works and it is the closest i have gotten. When i implemented this coding it does it perfectly however it changes the Unit Stock id to the last record stock id, yet it inserts all the necessary items. I wish i could show you what i mean!
Aug 21 '08 #7
Constantine AI
129 100+
Thanks nico5038 i have it sorted now and the code works well, cheers.
Aug 21 '08 #8

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

Similar topics

10
by: Jim | last post by:
Hi, Let me explain how I'd generally do things. For a page where the user edits data, I'd generally call it something like editfred.php (for example), the page which updates the data would be...
25
by: dixie | last post by:
I have some code that adds new records into a table for each ID in a list box when a button on a form is clicked. This works fine. My problem now is that I wish to be able to edit all the records...
2
by: Sky | last post by:
Hello: Another question about trying to wring functionality from a DataGrid... Have a DB table of "Contacts" -- 14 or more fields per record Show in datagrid -- but only 5 columns (First,Last,...
3
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
1
by: chromis | last post by:
Hi, I'm having trouble fully implementing the edit section of a contact admin system, so far I have written the following: - Bean (Contact.cfc) - Data Access object (ContactDAO.cfc) - Gateway...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.