Loop through many fields in a table to create a new field (1 Viewer)

melfaram

New member
Local time
Yesterday, 22:35
Joined
Jun 12, 2013
Messages
4
I would like to thank you for your help in advance. I have a table called sheet1. It has a field for ID (PID), and many fields for scores assigned by evaluators (text not number) The fields have the same first part (Rev) and a numbers appended to them : Rev1, Rev2, etc. I want to loop through all the fields that have the scores and change them from string to number and then use them to create a new field called total that has the total numbers of values< 4 across the fields. The ID field should not be in the loop.. I have never used loops before. Therefore, I will be very grateful if you let me know 1) how to exclude the ID field from the loop and include the fields I want only and 2) what should I do to end up with the results I want.
Thanks again

What I have
Table: Sheet1
Fields: PID, Rev1, Rev2, Rev3, Rev4 ---------------- Rev30

Current table:
PIDRev1Rev2Rev3Rev4Rev30
11235----
24455----
34565---
What I want
PIDRev1Rev2Rev3Rev4Rev30Total (values < 4)
11235---0
24455---2
34445----3
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:35
Joined
May 7, 2009
Messages
19,247
what you are doing is not Normal to database.

you should have a "simple" table:

PID (number)
Rev (number)

everytime there is revision, you add Record with same PID but increasing Rev number.
 

Melfaramawi

New member
Local time
Today, 00:35
Joined
Jun 1, 2022
Messages
6
I am sorry if my question was not clear. I do not create tables or add new records to them. I get excel sheets and I import them in access. I am looking for a Vba code to loop through the records across the fields.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 06:35
Joined
Jul 9, 2003
Messages
16,285
I am sorry if my question was not clear.

Your question is very clear! The difficulty you are facing is because of the arrangement of the data. You need to transpose the data into two columns as suggested by ArnelGP.

The why and the how are explained in my blog here:-


I also have a tool to simplify this process which is available for free to Access World Forum members.

If you would like a copy of the tool then send me a private message within access world forums messaging system and I will send you a coupon code to release a free copy.
 

June7

AWF VIP
Local time
Yesterday, 21:35
Joined
Mar 9, 2014
Messages
5,488
A VBA procedure could do this but as already advised, a normalized structure would likely not even need VBA.

Your desired output is not clear to me. Why does PID 1 have 0 Total when there are 3 values <4 and neither of the other records has values <4 so why do they show 2 and 3?
 

Melfaramawi

New member
Local time
Today, 00:35
Joined
Jun 1, 2022
Messages
6
A VBA procedure could do this but as already advised, a normalized structure would likely not even need VBA.

Your desired output is not clear to me. Why does PID 1 have 0 Total when there are 3 values <4 and neither of the other records has values <4 so why do they show 2 and 3?
Thank you for your response. The numbers in total are arbitrary. I just put any numbers. Because I have many fields I did not put all of them. But you are right the total for first row should have at least 3. Ok, I know how to transpose (union all) but is there a way to transpose using loops. The idea is the number of fields will keep changing and not fixed.
 

June7

AWF VIP
Local time
Yesterday, 21:35
Joined
Mar 9, 2014
Messages
5,488
Here is a very simple function:
Code:
Function GetTotal(intPID As Integer) As Integer
Dim rs As DAO.Recordset, intF As Integer, x As Integer, intC As Integer
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Sheet1 WHERE PID=" & intPID)
intF = rs.Fields.count - 1
For x = 1 To intF
    If rs(x) < 4 Then intC = intC + 1
Next
GetTotal = intC
End Function
It assumes: 1) PID field is the first field in table design and 2) all other fields are of the RevX naming and 3) every field has a number value

Call the function from query:
SELECT PID, GetTotal([PID]) AS Cnt FROM Sheet1;

Or call from a textbox on form or report.
 
Last edited:

melfaram

New member
Local time
Yesterday, 22:35
Joined
Jun 12, 2013
Messages
4
Here is a very simple function:
Code:
Function GetTotal(intPID As Integer) As Integer
Dim rs As DAO.Recordset, intF As Integer, x As Integer, intC As Integer
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Sheet1 WHERE PID=" & intPID)
intF = rs.Fields.count - 1
For x = 1 To intF
    If rs(x) < 4 Then intC = intC + 1
Next
GetTotal = intC
End Function
It assumes: 1) PID field is the first field in table design and 2) all other fields are of the RevX naming and 3) every field has a number value

Call the function from query:
SELECT PID, GetTotal([PID]) AS Cnt FROM Sheet1;

Or call from a textbox on form or report.
Thank you very much June7. It worked well. I really appreciate it.
 

June7

AWF VIP
Local time
Yesterday, 21:35
Joined
Mar 9, 2014
Messages
5,488
Might still consider procedure to transpose data when received. If your dataset gets very large, my VBA solution might perform slowly. SQL aggregation is usually most efficient approach.
 

melfaram

New member
Local time
Yesterday, 22:35
Joined
Jun 12, 2013
Messages
4
Might still consider procedure to transpose data when received. If your dataset gets very large, my VBA solution might perform slowly. SQL aggregation is usually most efficient approach.
Therefore, it will be great if you can post a loop that can be used with union all to transpose datasets such as the one you wrote the previous loop for, please.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:35
Joined
Feb 19, 2002
Messages
43,331
What we are suggesting is that INSTEAD of importing the data as is, you normalize it on import. That solves your analysis problem, now and later. June's solution is specific to your exact question. If you want to do an average, you need to create a different function. Converting the data to a proper design allows you to create simple queries rather than having to write code for each new request.

@Uncle Gizmo is offering a free tool to do this for you. Perhaps you should look at it.
 

melfaram

New member
Local time
Yesterday, 22:35
Joined
Jun 12, 2013
Messages
4
What we are suggesting is that INSTEAD of importing the data as is, you normalize it on import. That solves your analysis problem, now and later. June's solution is specific to your exact question. If you want to do an average, you need to create a different function. Converting the data to a proper design allows you to create simple queries rather than having to write code for each new request.

@Uncle Gizmo is offering a free tool to do this for you. Perhaps you should look at it.
Ok, got it. Thanks
 

Users who are viewing this thread

Top Bottom