TheRedOne
TheRedOne

Reputation: 165

manipulate query result to create another table

OK so I have tried a few differrent ways and searched the forums. I've found similar problems to the one I'm having, but not quite what I'm looking for. I have a query on my database that returns 2 columns. In the first column is a code number, and the revision for the code number in the second

e.g.

Code_1       0
Code_1       1
Code_2       0
Code_2       1
Code_2       2  

Now what I need to do is loop through this query and pick out the code and the maximum revision number associated with each code number and put these into another table so I would end up with

Code_1     1
Code_2     2

in my new table.

I've can't even seem to loop through the query and all I get each time is the first result. I've used Debug.Print below to show myself exactly what my loop is doing.I've done some Java and the vba loops are obv different. One way I've tried is:

Dim db  As DAO.Database
Dim rs  As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Query_Name")

Dim i,j as Integer
Dim tempvar1, tempvar2 As Variant
For i = 0 To rs.EOF
tempvar1 = rs!Code_Number
For j = 0 to rs.EOF
tempvar2 = rs!Code_Rev
If Code_Rev > tempvar2 Then
tempvar2 = rs!Code_Rev
End If
Debug.Print(tempvar1 + tempvar2)
Next j
Next i

All I get is the first code number and revision on the list. If I can't even loop through the list properly I've no hope of creating a new table from the results I need :-/ I've also tried doing something similar with a Do While Loop but same type of result. Please show me the error of my ways in iterating through the query and please help me create a new table from the results.

Upvotes: 1

Views: 204

Answers (1)

Fionnuala
Fionnuala

Reputation: 91376

Why not a straight query?

SELECT tx.Code, Max(tx.[NewCode]) AS MaxOfC INTO New
FROM tx
GROUP BY tx.Code;

Where tx is the name of your table.

Upvotes: 1

Related Questions