views:

51

answers:

2

I would like to write some custom VBA functions to perform calculations on particular fields in a set of queries. This is what I currently have:

Public Function TVM(LEN_SEC As Double, ADT_CUR As Integer, TRK_PCT As Integer)
    TVM = LEN_SEC * ADT_CUR * TRK_PCT / 100#
End Function

This works fine, but when I create the query I have to do something like this:

SELECT TVM([LEN_SEC], [ADT_CUR], [TRK_PCT]) AS TRK_VMT
FROM System;

I would prefer to do this instead:

SELECT TVM()
FROM System;

Is there a way to access the current row being output by the query from within the VBA function? Something like this:

Public Function TVM(LEN_SEC As Double, ADT_CUR As Integer, TRK_PCT As Integer)
    TVM = CurrentRow.Fields("LEN_SEC") * CurrentRow.Fields("ADT_CUR") * CurrentRow.Fields("TRK_PCT") / 100#
End Function

These calculations are frequently used in time-sensitive custom generated reports, by several users of varying skill level and attention spans - the simpler I can make the interface for them, the better it is for our QA team.

Thanks!

+2  A: 

There is no way to do what your example demonstrates.

Probably the most performance approach would be to avoid the function altogether since it only uses simple operators.

For example:

SELECT [LEN_SEC] * [ADT_CUR] * [TRK_PCT] / 100 as TVM
FROM System
JohnFx
I suspected there might be no way to do this, but thought I'd ask. This is not the most complicated calculation that is performed - the example was kept simple to avoid clouding the question I wanted answered. When setting up calculations where multiple, sometimes obtusely named, fields are needed, it would help to have a simple function that could be called rather than having the analysts stop to look up the formulae each time.
Kevin Hobbs
In that case your approach that calls the function from the stored proc with parameters passed in is really the only practical option.
JohnFx
A: 

Users with limited skills would be better off creating their reports from queries than tables. Put your function with all the parameters in the query.

Select TVM from qrySystem;

Or teach them how to use the Expression Builder in the Query Designer and make sure they replace the <> with actual field names.

Jeff O