Loop through records and update value in VBA

dgravitt
dgravitt used Ask the Experts™
on
I have a table with a xfrm_ID, and an amps value. There will be many records where the xfrm_id is the same. I have a column called Amp_seq that for each Xfrm_id I would like to number 1 to ..... from lowest amps to highest. below is an example of what I would like to have when completed.

Xfrm_id         Amps       Amp_seq
2345               1000              1
2345               1200              2
2345               1200              2
2345               1500              3

Any help is appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
You need to add a grouping section to the report and group by xfrm_id and add a sort by Amps.  Then you can add the Amp_Seq control and give it a ControlSource value of:

=1

Then set it's Group property to OverGroup.

You can hide the group header and footer so that the report doesn't change.  Otherwise the Xfrm_id would be in the group header and not show on each detail record.

Author

Commented:
Sorry if I was confusing, but I need a loop that will populate the Amp_Seq in the table. The example was what I would like the data in table look like.
John TsioumprisSoftware & Systems Engineer

Commented:
You will need 2 recordsets like this
Dim rst1,rst2
set rst1 = currentdb.Openrecordset("SELECT Xfrm_id  From YouTable GROUP BY  Xfrm_id ")
while not rst1.EOF
counter =1
set rst2 = currentdb.Openrecordset("SELECT * From YouTable WHERE  Xfrm_id=  " & rst1.Fields("Xfrm_id"))
while not rst2.EOF
rst2.edit
rst2.Fields("Amps") = 1000*counter
rst2.Fields("Amp_seq") = counter
rst2.Update
counter=counter+1
rst2.MoveNext
Wend
rst1.MoveNext
Wend

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Distinguished Expert 2017

Commented:
Are you sure you want to permanently store this value?  What happens if an item gets deleted? or something gets added in the middle?  Exactly what is the purpose of the sequence number?

PS John's code doesn't sort the recordset so since queries and tables are unordered sets, there are times when the query engine might deliver the rows "out of order".  The only way to control order is to specifically ORDER BY.  Also, rather than using two recordsets and runing potentially thousands of queries, just sort by the two fields and add break logic that resets the counter when the ID field changes.
John TsioumprisSoftware & Systems Engineer

Commented:
I didn't have any info about ORDER BY thats why i didn't use it...maybe if have sample data i would/could apply sorting
Distinguished Expert 2017

Commented:
I would like to number 1 to ..... from lowest amps to highest. below is an example of what I would like to have when completed.
 That's how I knew.

Author

Commented:
The table source is a query with two unions. I tried using the query with the unions as my record source for my report, but it is too slow. So, I make a table with the query then use it as my record source. Maybe this will help, I use the Amps_seq field to display the records with the 4 lowest values. It could be more than 4 records if some of the values are duplicated. The answer to your first question, this will be rebuilt every time before running the report. The table populated and then the amp_seq set. I thought it would be easier to use the amp_seq in case the number of values to return changes. In my query to feed the report, I just set amp_seq < 5, order amps lowest to highest. Hope this helps. I'm open to a better way of handling this.
Distinguished Expert 2017

Commented:
If this is a report, why would you return duplicate rows?  Why not let the union eliminate the dups?  Are you sure you need a union?  How can you tell where each row is coming from?

Author

Commented:
the amps could be duplicated, but there are other fields that make it unique. I need all records for the lowest 4 amps. the data comes from 3 different sources, and is combined for one datasource. I need to loop through each xfrm_id, then assign the values to amp_seq for lowest to highest amps. I have manually assigned the amp_seq to some records,  and this gives me what I need. I just don't know how to write the code for loop.
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
Just loop and update a recordset:

Dim rs As DAO.Recordset
Dim Counter As Long
Dim LastID As Long

Set rs = CurrentDb.OpenRecordset("Select Xfrm_id, Amps, Amp_seq From YourTable Order By 1, 2")
While Not rs.EOF
    If LastID <> rs!Xfrm_id.Value Then
        Counter = 0
        LastID = rs!Xfrm_id.Value
    End If
    Counter = Counter + 1
    rs.Edit
        rs!Amp_seq.Value = Counter
    rs.Update
    rs.MoveNext
Wend
rs.Close

Set rs = Nothing

Open in new window

/gustav

Author

Commented:
This is so close. But, if an Amp value is the same for a Xfrm_id, the Amp_Seq needs to be the same as the first example shows. Thanks for the suggestion.

Author

Commented:
I was able to make this work. Below is my final code. Thanks again.
Dim db As Database
Dim rs As DAO.Recordset
Dim Counter As Long
Dim LastID As Long
Dim LastAmp As Long
Set db = CurrentDb
LastAmp = 0
Set rs = db.OpenRecordset("Select XFBNK_ID, Amps, Amp_seq From AllEle where amps >=1 order by 1, 2")
While Not rs.EOF
    If LastID <> rs!XFBNK_ID.Value Then
        Counter = 0
        LastID = rs!XFBNK_ID.Value
    End If
    If LastAmp <> rs!Amps Then
    Counter = Counter + 1
    End If
    LastAmp = rs!Amps.Value
    rs.Edit
        rs!Amp_seq.Value = Counter
    rs.Update
    rs.MoveNext
   
Wend
rs.Close

Set rs = Nothing
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Thanks for the feedback.

/gustav

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial