0

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
2
  • I just realized I wasn't entirely clear. I want the function to return a value for each record in tblAllocation, and it does this in the Immediate window, but not when I call the function in a query. Commented Oct 29, 2018 at 20:00
  • 1
    Please be a bit more explicit in what you want. Try adding sample data and expected output. A function can only return a single value or object by design, but that can be a string containing multiple concatenated values, for example. Commented Oct 29, 2018 at 20:51

2 Answers 2

1

To return a different value per input row, your function needs to use one or more parameters from the input table/query.

E.g.

Function TestID(ID As Long, someField As Double) As Double
    ' do calculations on parameters here
    TestID = CalcResult
End Function

and pass column values as parameters in the query, e.g.

SELECT foo, bar, TestID([ID], [someField]) AS Calculation
FROM myTable
Sign up to request clarification or add additional context in comments.

1 Comment

Andre, this also helps. I think I know what I have to do with the larger project moving forward. Thanks much.
0

The function will only ever return one value.

You are printing the value assigned to the TestID symbol for each iteration of the Do loop, but this value is merely overwritten on the next iteration of the loop, it will not be returned by the function.

If you want multiple values to be returned, consider either building & returning an array of values, or append the values to another table - though, this seems rather redundant given that you are sourcing the values from an existing table/query.

1 Comment

Thanks much, this helps me understand what I was missing. I'll have to work on an array, I guess. Appreciate the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.