Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write forumlas in code

Status
Not open for further replies.

gwilym40

Programmer
May 2, 2002
31
GB
Hi I have a continious form for which I'd like to write a formula in code

i.e.
write the select statement and then
say

Dim result as string

result = theforumla

I have 12 fields (Month1 to Month12)

I want to, for the sum of ALL 12 fields (not just Month1)

=Sum(IIf(IsNumeric([Month1]),Val([Month1]),0))

Any ideas?
Thanks





 
Hi if your columns are called Month1, Month2 etc try somthing like:

Dim curTotal As Currency
Dim i
Dim Rs as DAO.Recordset

Set Rs = Db.OpenREcordset("SELECT ...etc)
or the equivalent ADO contruct if you are using ADO

curTotal = 0
For i = 1 to 12
curTotal = curTotal + Nz(Rs("Month" & i),0)
Next i

Hope this helps

Ken Reay
 
Call the following procedure with the fields you want to sum as the arg list. Per your example:

Result = basSum([Month1], [Month2], ... [Month12])

Although I'm at a loss as to WHY you want to convert the calculation to a string data type ...



Code:
Public Function basSum(ParamArray varMyVals()) As Variant

    'Michael Red 3/7/2002
    'To return the SUM of a series of values

    'Sample Usage:
    '? bassum(1, 2.0067, 3)
    '6.0067

    Dim Idx As Integer
    Dim MySum As Variant

    For Idx = 0 To UBound(varMyVals())
        If (IsMissing(varMyVals(Idx))) Then
            GoTo NextVal
        End If
        If (IsNumeric(varMyVals(Idx))) Then
            MySum = MySum + varMyVals(Idx)
        End If
NextVal:
    Next Idx

    basSum = MySum

End Function
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top