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

Having trouble updating a recordset

3 2Bits
The code will not accept the top line inside the Do While loop. I am open to suggestions. I would like the user to type in a collection of 1 or more numbers that will need a common index applied. (forming part of a one-many relationship).
Expand|Select|Wrap|Line Numbers
  1. Public Sub ValveLink_Click()
  2.  
  3.     Dim db As Database
  4.     Dim rs As Recordset
  5.  
  6.     Dim IndexFocus  As Long
  7.     Dim Valve_No As Long
  8.     Dim i As Integer
  9.     Dim Answer As String
  10.  
  11.     Set db = CurrentDb
  12.     Set rs = db.OpenRecordset("NameplateIndexUpdate") 'Fields: ValveNo and Index + others
  13.  
  14.     i = 0
  15.     IndexFocus = Me.[Valve Index]             'transferred from a form, proven to be ok.
  16.     Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve")
  17.  
  18.     Do While (Not rs.EOF) And (i < 15)
  19.         If rs!ValveNo = Valve_No Then                               '***** only works when I change it to =i *****
  20.             MsgBox ("This works, valve No is " & Valve_No)          'works ok when tested
  21.             rs.Edit
  22.             rs!Index = IndexFocus                                   'works ok when tested
  23.             MsgBox ("This works as well, Index No is " & rs![Index])  'works ok when tested
  24.             rs.Update
  25.             Debug.Print rs!Index                                    'works ok when tested
  26.         End If
  27.         Answer = MsgBox("Do you have another valve to link?", vbQuestion + vbYesNo + vbDefaultButton2, "Another Valve to Link") 'Works OK
  28.         If Answer = vbNo Then                                       'works ok
  29.             Exit Do                                                 'works ok when tested, drops out of loop
  30.         Else
  31.             Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve") 'works ok
  32.         End If
  33.         rs.MoveNext
  34.         i = i + 1
  35.         Loop
  36.     rs.Close
  37.     Set rs = Nothing
  38.     db.Close
  39.  
  40. End Sub
Mar 15 '22 #1
7 5479
NeoPa
32,556 Expert Mod 16PB
Hi Wazza.

Please try to pay more attention when posting. A VBA question is not appropriate in the Lounge and code must always be posted within the [ CODE ] tags provided.

The reason your code won't compile is because the If statement comes in two versions - single-line and multi-line. It's clear you want the multi-line version but if you add a comment after the Then part it causes it to be treated as a single-line instead. It sort of sucks in a way - I get that - but that's the rules we have to live with I'm afraid. Put the comment on the previous line and that problem will disappear. Good luck.
Mar 15 '22 #2
Wazza0161
3 2Bits
Thanks NeoPa,

Tips are welcome for both website and coding. I thought of trying to have an open-ended loop (say do while something obvious where the loop specifies the link, not an If Statement) and then let the user dictate when to exit (exiting the loop has been proven to work) and thus not letting the code get confused.
Mar 15 '22 #3
NeoPa
32,556 Expert Mod 16PB
Hi Wazza.

Don't misunderstand what I'm saying. It's perfectly possible to use a multi-line If statement. You simply can't add a comment on the same line if that's what you want. That would turn it into a single-line If statement - which would be no use to you or anyone. You simply have to realise that and move the comment from that line to another one so it's treated as you mean it - as a multi-line statement.

Does that make it clearer?
Mar 16 '22 #4
NeoPa
32,556 Expert Mod 16PB
Oh, just as another little pointer for another part of your code - not really relevant to the question but worth pointing out anyway - line #23, where you use MsgBox(), should not be found within the Edit / Update section of your code. The user may not respond immediately to the prompt and then you get left with a lock on the table that is never released. Even a delay releasing it can cause serious problems so I suggest you move that after the rs.Update.
Mar 16 '22 #5
NeoPa
32,556 Expert Mod 16PB
Hi Wazza.

Please disregard my earlier post about the multi-line If statements. It turns out my memory was flawed and that isn't a thing at all after all :-(

So, I guess we'd better look at what actually is going wrong then. Perhaps you could share some details of what you see as a problem with line #19 of your posted code and we could go from there?
Mar 16 '22 #6
Wazza0161
3 2Bits
I have solved the problem by using a nested loop. Works brilliantly. Thanks for all your advice but I was able nut it out myself. I have tidied the code up a bit, used a query instead of a table, removed all the MsgBoxes relating to step confirmations, removed all comments. I am buzzing with this now. here is the sample from Immediate window, where I linked 3 valves with the same model. Hopefully I have added the coding correctly this time.
Valve No = 315, Index No = 576
Valve No = 317, Index No = 576
Valve No = 324, Index No = 576

Expand|Select|Wrap|Line Numbers
  1. Public Sub ValveLink_Click()
  2.  
  3.     Dim db As Database
  4.     Dim rs As Recordset
  5.  
  6.     Dim IndexFocus  As Long
  7.     Dim Valve_No As Long
  8.     Dim Answer As String
  9.  
  10.     Set db = CurrentDb
  11.     Set rs = db.OpenRecordset("21A-ValveIndexUpdate") 'Fields: ValveNo and Index + others
  12.  
  13.     IndexFocus = Me.[Valve Index]
  14.     Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve")
  15.  
  16.     rs.MoveFirst
  17.     Do While (Not rs.EOF)
  18.  
  19.         Do While (Not rs.EOF)
  20.             If rs!ValveNo = Valve_No Then
  21.                 rs.Edit
  22.                 rs!Index = IndexFocus
  23.                 rs.Update
  24.                 Debug.Print "Valve No = " & rs!ValveNo & ", Index No = " & rs!Index
  25.             End If
  26.             rs.MoveNext
  27.         Loop
  28.  
  29.         Answer = MsgBox("Do you have another valve to link?", vbQuestion + vbYesNo + vbDefaultButton2, "Another Valve to Link")
  30.         If Answer = vbNo Then
  31.            Exit Do
  32.         Else
  33.             Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve")
  34.         End If
  35.         rs.MoveFirst
  36.     Loop
  37.     rs.Close
  38.     Set rs = Nothing
  39.     Set db = Nothing
  40.  
  41. End Sub
Mar 17 '22 #7
NeoPa
32,556 Expert Mod 16PB
Hi Wazza.

Nicely done. Including the [/code] on the last line instead of the next is something very few spot ;-)

Also, your code has a very logical look to it, and that's always a good sign.

As there's little else I can do for you I'll just post a version that's hopefully slightly improved and leave you to spot the differences & decide if there's anything there you want to take for yourself.
Expand|Select|Wrap|Line Numbers
  1. Public Sub ValveLink_Click()
  2.  
  3.     Dim db As DAO.Database
  4.     Dim rs As DAO.Recordset
  5.  
  6.     Dim IndexFocus As Long, Valve_No As Long, Answer As Long
  7.     Dim strMsg As String
  8.  
  9.     Set db = CurrentDb()
  10.     Set rs = db.OpenRecordset("21A-ValveIndexUpdate") 'Fields: ValveNo and Index + others
  11.  
  12.     IndexFocus = Me.[Valve Index]
  13.  
  14.     Do Until rs.EOF
  15.         Call rs.MoveFirst
  16.         strMsg = "Please enter the valve number you want to link to this model"
  17.         Valve_No = InputBox(strMsg, "Valve Link", "Enter Valve")
  18.  
  19.         Do Until rs.EOF
  20.             If rs!ValveNo = Valve_No Then
  21.                 Call rs.Edit
  22.                 rs!Index = IndexFocus
  23.                 Call rs.Update
  24.                 strMsg = Replace("Valve No = %VN, Index No = %IN." _
  25.                                , "%VN", rs!ValveNo)
  26.                 strMsg = Replace(strMsg, "%IN", rs!Index)
  27.                 Debug.Print strMsg
  28.             End If
  29.             Call rs.MoveNext
  30.         Loop
  31.  
  32.         Answer = MsgBox(Prompt:="Do you have another valve to link?" _
  33.                       , Buttons:=vbQuestion Or vbYesNo Or vbDefaultButton2 _
  34.                       , Title:="Another Valve to Link")
  35.         If Answer = vbNo Then Exit Do
  36.     Loop
  37.     Call rs.Close
  38.     Set rs = Nothing
  39.     Set db = Nothing
  40.  
  41. End Sub
At some other time, and in another thread, we could maybe talk about applying the updates more simply using SQL code. Much less to worry about that way. As & when you're up for it though.
Mar 17 '22 #8

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

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
2
by: ed | last post by:
i'm having trouble with a form. I want to be able to type in the address of the form with the data for the form items in the URL (ie: http://somesite.com/formpage.html?field1=data1&field2=data2)....
0
by: cwbp17 | last post by:
I'm having trouble updating individual datagrid cells. Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables...
0
by: Jozef | last post by:
Hello, I'm having trouble with the download links on my web server. The error I'm getting is; CGI Timeout The specified CGI application exceeded the allowed time for processing. The server...
1
by: MLH | last post by:
Am having trouble with the filter property setting below. Would like to filter the listing to car makes beginning with "D". I'm blowing it on the filter spec somehow??? Sub OpenRecordsetX() ...
6
by: cek172 | last post by:
I am having trouble updating a table based on information from another table. I have two tables that aren't 1 to 1. The first table ("A") has approximately 100 records and three fields from this...
2
by: Stu | last post by:
Hi guys, I've been having trouble getting the clock function to work portably, please could I get some thoughts? <Possibly OT comments> It works fine on my laptop (under WinXP) and on my...
1
by: omar.norton | last post by:
I am trying to create a from with a series of combo boxes that each query a different field (called Specific01, Specific02 etc., except the first field which is called Condition). Each combo box...
1
by: George | last post by:
I have just loaded Access 2007 and am having trouble creating a new database where I update my table from a query. I have done this numerous times in years past but am totally frustrated tryng to...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.