PC Review


Reply
Thread Tools Rate Thread

Access 2010 Loop through a recordset

 
 
New Member
Join Date: Apr 2012
Posts: 2
 
      14th Apr 2012
Hello,

I am looking for some help with looping through a recordset and performing an action that involves each record. The problem involves two tables; tblShiftID and tblNames.
tblShiftID has two fields; IdStart and IdEnd (both are number fields). TblNames has several fields but my only concern is a field named ShiftID. The ShiftID in tblNames needs to be changed to generate new schedules.

tblShiftID's two fields give the start (IdStart) and end (IdEnd) of an employees rotation through a schedule - i.e. 1-7. If the employee is on ShiftID 2 this week I need him on 3 next week. Another employee may be on rotation 11-17 with his ShiftID as 17. I would need his ShiftID to be 11 next week.

There are 43 sets of rotations like this so a loop would be great. The following works to change the 1-7 rotation:

DoCmd.RunSQL "UPDATE tblNames SET tblNames.ShiftID = IIf([ShiftID]=7,1,[ShiftID]+1)" & _
"WHERE (((tblNames.ShiftID) Between 1 And 7));"

Then for the 11-17 rotation:

DoCmd.RunSQL "UPDATE tblNames SET tblNames.ShiftID = IIf([ShiftID]=17,11,[ShiftID]+1)" & _
"WHERE (((tblNames.ShiftID) Between 11 And 17));"

The new code would loop through and update each employees ShiftID getting the 1 from tblShiftID.IdStart and the 7 from tblShiftID.IdEnd then move to the next record to the 11 and 17 and so on.

Thank you in advance for any help.

Rick
 
Reply With Quote
 
 
 
 
New Member
Join Date: Apr 2012
Posts: 2
 
      15th Apr 2012
The problem has been solved. Here is the final code in case it might help someone out:


Private Sub cmdRotate_Click()
If MsgBox("You are about to change the ShiftID numbers in order to create a new schedule!!! Are you sure?", vbYesNo, "Continue?") = vbYes Then
Dim db As Database
Dim rs As Recordset
Dim iCount As Integer
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT * FROM tblShiftID;") 'open the recordset for use

With rs

If Not (rs.BOF And rs.EOF) Then 'Ensure that there are actually records to work with
rs.MoveLast 'This is required otherwise you may not get the right count
iCount = rs.RecordCount 'Determine the number of returned records
rs.MoveFirst 'not necessary but starting from the first record to troubleshoot problems

Do While Not .EOF

DoCmd.RunSQL "UPDATE tblNames SET tblNames.ShiftID = IIf([ShiftID]=" & rs!IdEnd & ", " & rs!IdStart & ",[ShiftID]+1) " & _
"WHERE (((tblNames.ShiftID) Between " & rs!IdStart & " AND " & rs!IdEnd & "));"

.MoveNext

Loop
End If
End With
Else
End If
End Sub
 
Reply With Quote
 
 
 
 
Senior Member
bootneck02's Avatar
Join Date: Jul 2010
Location: Maidstone, Kent, UK
Posts: 2,615
 
      15th Apr 2012
Thanks for the heads up rpes, an answer to a question is always of help someone else. Oh hello & welcome to PCR nice to have you along.

 

Last edited by bootneck02; 15th Apr 2012 at 04:32 PM..
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
loop through field names, then use to loop through records topopulate grid pmacdiddie@gmail.com Microsoft Access Form Coding 1 4th Jan 2008 02:13 PM
How to loop through a recordset and update the field of a recordset or delete current record Karen Middleton Microsoft Access Macros 1 4th Jan 2005 11:30 AM
How to loop through a recordset and update the field of a recordset or delete current record Karen Middleton Microsoft Access Queries 1 4th Jan 2005 11:30 AM
Loop through recordset Razor Microsoft Access Form Coding 2 20th Feb 2004 12:20 AM
Infinite Loop Infinite Loop Infinite Loop Infinite Loop... Donna Windows XP Setup 0 8th Dec 2003 11:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:56 AM.