Hi Duncan,
Here's a VBA function that I created to use as an example of looping through rows of data in a Table and then doing something with particular values from each row.
Let me know if you have any questions.
Code:
'Get values from a table using a query in VBA.
Function Get_DB_Values()
'Assumes that you have a Table1 and that Field1 is Text and Field2 is a Number.
' . . . just for testing purposes.
' . . . You should use your actual Table & field names & set your variables to match the data types of your table fields.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strField1 As String
Dim intField2 as Integer
On Error GoTo Error_Handle
Set db = CurrentDb
strSQL = "Select * From [Table1]"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
'This Do While loop goes through all the records in strSQL.
Do While Not rs.EOF
strField1 = rs![Field1]
intField2 = rs![Field2]
'I put this little If Then here with a MsgBox . . . to look at the values
'In your case - you will need to put in logic here to update the current record.
If intField2 = 5 Then
'MsgBox strField1 & ", " & intField2
End If
.MoveNext 'Move to next record in recordset.
Loop 'Back to 'Do While' to check if we are at the end of the file.
Exit_Get_DB_Values:
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set db = Nothing
Exit Function
Error_Handle:
Resume Exit_Get_DB_Values
End With
End Function
Hope this helps!
All the best.