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). - Public Sub ValveLink_Click()
-
-
Dim db As Database
-
Dim rs As Recordset
-
-
Dim IndexFocus As Long
-
Dim Valve_No As Long
-
Dim i As Integer
-
Dim Answer As String
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("NameplateIndexUpdate") 'Fields: ValveNo and Index + others
-
-
i = 0
-
IndexFocus = Me.[Valve Index] 'transferred from a form, proven to be ok.
-
Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve")
-
-
Do While (Not rs.EOF) And (i < 15)
-
If rs!ValveNo = Valve_No Then '***** only works when I change it to =i *****
-
MsgBox ("This works, valve No is " & Valve_No) 'works ok when tested
-
rs.Edit
-
rs!Index = IndexFocus 'works ok when tested
-
MsgBox ("This works as well, Index No is " & rs![Index]) 'works ok when tested
-
rs.Update
-
Debug.Print rs!Index 'works ok when tested
-
End If
-
Answer = MsgBox("Do you have another valve to link?", vbQuestion + vbYesNo + vbDefaultButton2, "Another Valve to Link") 'Works OK
-
If Answer = vbNo Then 'works ok
-
Exit Do 'works ok when tested, drops out of loop
-
Else
-
Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve") 'works ok
-
End If
-
rs.MoveNext
-
i = i + 1
-
Loop
-
rs.Close
-
Set rs = Nothing
-
db.Close
-
-
End Sub
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.
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.
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?
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 .
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?
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 - Public Sub ValveLink_Click()
-
-
Dim db As Database
-
Dim rs As Recordset
-
-
Dim IndexFocus As Long
-
Dim Valve_No As Long
-
Dim Answer As String
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("21A-ValveIndexUpdate") 'Fields: ValveNo and Index + others
-
-
IndexFocus = Me.[Valve Index]
-
Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve")
-
-
rs.MoveFirst
-
Do While (Not rs.EOF)
-
-
Do While (Not rs.EOF)
-
If rs!ValveNo = Valve_No Then
-
rs.Edit
-
rs!Index = IndexFocus
-
rs.Update
-
Debug.Print "Valve No = " & rs!ValveNo & ", Index No = " & rs!Index
-
End If
-
rs.MoveNext
-
Loop
-
-
Answer = MsgBox("Do you have another valve to link?", vbQuestion + vbYesNo + vbDefaultButton2, "Another Valve to Link")
-
If Answer = vbNo Then
-
Exit Do
-
Else
-
Valve_No = InputBox("Please enter the valve number you want to link to this model", "Valve Link", "Enter Valve")
-
End If
-
rs.MoveFirst
-
Loop
-
rs.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
End Sub
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. - Public Sub ValveLink_Click()
-
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
-
Dim IndexFocus As Long, Valve_No As Long, Answer As Long
-
Dim strMsg As String
-
-
Set db = CurrentDb()
-
Set rs = db.OpenRecordset("21A-ValveIndexUpdate") 'Fields: ValveNo and Index + others
-
-
IndexFocus = Me.[Valve Index]
-
-
Do Until rs.EOF
-
Call rs.MoveFirst
-
strMsg = "Please enter the valve number you want to link to this model"
-
Valve_No = InputBox(strMsg, "Valve Link", "Enter Valve")
-
-
Do Until rs.EOF
-
If rs!ValveNo = Valve_No Then
-
Call rs.Edit
-
rs!Index = IndexFocus
-
Call rs.Update
-
strMsg = Replace("Valve No = %VN, Index No = %IN." _
-
, "%VN", rs!ValveNo)
-
strMsg = Replace(strMsg, "%IN", rs!Index)
-
Debug.Print strMsg
-
End If
-
Call rs.MoveNext
-
Loop
-
-
Answer = MsgBox(Prompt:="Do you have another valve to link?" _
-
, Buttons:=vbQuestion Or vbYesNo Or vbDefaultButton2 _
-
, Title:="Another Valve to Link")
-
If Answer = vbNo Then Exit Do
-
Loop
-
Call rs.Close
-
Set rs = Nothing
-
Set db = Nothing
-
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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)....
|
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...
|
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...
|
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()
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |