Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do While Loop not producing expected results

Status
Not open for further replies.

Rufusjeep

Technical User
Nov 6, 2001
10
0
0
US
Want to query table to get a list of all employees who have had a particular training then take their employee ID's and find them in the Worker table and change the field "Selected" to true. Was able to get it to mark the first record in the Worker table for the recordset but not the rest. Any ideas on what I need to change in my code to get it to modify all of the matching records in the Worker table and the recordset.

Dim CurDB As Database, Rs As Recordset, SQLStmt As String
Dim Rstbl As Recordset

Set CurDB = CurrentDb()
SQLStmt = "SELECT [EmpID], [EmpID] FROM [tblTRAINING] Where Criteria1 And Criteria2"
Set Rs = CurDB.OpenRecordset(SQLStmt, DB_OPEN_DYNASET)
Set Rstbl = CurDB.OpenRecordset("tblWORKER", DB_OPEN_DYNASET)
Do While Not Rs.EOF
Do While Not Rstbl.EOF
If Rs("EmpID") = Rstbl("EmpID") Then
Rstbl.Edit
Rstbl("SELECTED") = True
Rstbl.Update
End If
Rstbl.MoveNext
Loop
Rs.MoveNext
Loop
Rs.Close
Rstbl.Close
End Sub

Thanks for the help in advance,
Mike
 
Assuming that it is a Yes/No field

Rstbl("SELECTED") = -1

Craig
 
Hi Mike!

Try this little addition:

Do While Not Rs.EOF
Rstbl.MoveFirst
Do While Not Rstbl.EOF
If Rs("EmpID") = Rstbl("EmpID") Then
Rstbl.Edit
Rstbl("SELECTED") = True
Rstbl.Update
End If
Rstbl.MoveNext
Loop
Rs.MoveNext
Loop

hth
Jeff Bridgham
bridgham@purdue.edu
 
You should be able to use an UPDATE SQL Statement to get the same results.

Dim SQLStmt as String

SQLStmt = "UPDATE [tblWorker] SET [tblWorker].Selected = True WHERE [tblWorker].EmpID = [tblTraining].EmpID AND Criteria1 And Criteria2"

CurrentDB.Execute SQLStmt
 
Better to follow the old MD grad A.K.A. TerpFan2001). He, at least, is not folllowing you off into clog-the-cpu swamp.


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top