We help IT Professionals succeed at work.

Loop through records and update value in VBA

dgravitt
dgravitt asked
on
338 Views
Last Modified: 2017-03-28
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

CERTIFIED EXPERT
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 TsioumprisIT Supervisor
CERTIFIED EXPERT
Distinguished Expert 2019

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

CERTIFIED EXPERT
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 TsioumprisIT Supervisor
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
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.
CERTIFIED EXPERT
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.
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Thanks for the feedback.

/gustav

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions