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).
Code:
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
Comment