I’m really a newbie with VBA. I’ve got a fairly elaborate function for Access VBA that loops through a table, does a bunch of calculations and conditional statements, and returns a value. When I run the function and use “Debug.Print” to get the function values it spits out exactly what it should in the Immediate Window. However, when I try to call the function to an Access query on the same table, the query returns only the last value in the loop.
I built a much simpler looping function to try to figure out what I was doing wrong (see below), and the same thing happens – correct values in the immediate window, only the last value returned in the expression in the query. Can someone tell me what obvious mistake I’m making? If I can fix it here, I ought to be able to fix it in the real project. Greatly appreciate any assistance!
Function TestID()
Dim d As Database
Dim r As Recordset
Dim id As Field
Set d = CurrentDb()
Set r = d.OpenRecordset (“tblAllocation”)
r.MoveFirst
r.MoveLast
r.MoveFirst
Do Until r.EOF
Set id = r.Fields(“IDAllocation”)
TestID = id
Debug.Print TestID ‘This lists all the correct values in the Immediate Window'
r.MoveNext
If r.EOF Then Exit Do
Loop
r.Close
Set r = Nothing
End Function