PDA

View Full Version : Adding fields...is there a better way?



Gingertrees
08-27-2008, 08:22 AM
I have a form, part of which assesses a client's financial situation. Since this is nifty information that's not widely used in many reports, all fields are unbound. Anyway, I have it set up so the user can enter client's sources of income, then hit a command button to add it (Using the ValueSum function)

-->There's got to be a better way to do this than the command button, but how? Before Update? After Update?
-->Valuesum is a long involved process, which I don't want to duplicate at least twice more for similar situations - is there a better way??
(Thanks to anyone who takes the time to look at my code.)

Private Sub Command92_Click()
'define the variables of the function
Dim value1 As Currency
Dim value2 As Currency
Dim value3 As Currency
Dim value4 As Currency
Dim value5 As Currency
Dim value6 As Currency
Dim value7 As Currency
Dim value8 As Currency
Dim value9 As Currency
Dim value10 As Currency
Dim value11 As Currency
'assign what the variables of the function stand
'for in real life for example, value1 in the
'function represents the curemployment text box
value1 = Me.curemployment.Value
'Me.WHATEVER.value = names of textboxes that
'contain values (sources of income)
value2 = Me.curspouseemployment.Value
value3 = Me.curSSI.Value
value4 = Me.curspouseSSI.Value
value5 = Me.curunemploy.Value
value6 = Me.curAFDC.Value
value7 = Me.curfoodstamps.Value
value8 = Me.curchildsupport.Value
value9 = Me.curother1.Value
value10 = Me.curother2.Value
'value11 / curtotalincome = sum of above
value11 = valuesum(value1, value2, value3,_
value4, value5, value6, value7)
Me.curtotalincome.Value = value11

End Sub
'name of the function
Function valuesum(value1 As Currency, _
value2 As Currency, value3 As Currency, _
value4 As Currency, value5 As Currency, _
value6 As Currency, value7 As Currency, _
value8 As Currency, value9 As Currency, _
value10 As Currency)
'procedure of the function
valuesum = Nz(value1, 0) + Nz(value2, 0) + _
Nz(value3, 0) + Nz(value4, 0) + Nz(value5, 0) + _
Nz(value6, 0) + Nz(value7, 0) + Nz(value8, 0) + _
Nz(value9, 0) + Nz(value10, 0)
End Function

CreganTur
08-27-2008, 08:47 AM
If you want to have the calculation roll up on its own then you could use After Update. Instead of C&Ping the same code for every single After Update event, you should make a User Defined sub, and just call it.

I would suggest setting a default value of 0 for these textboxes.


Valuesum is a long involved process, which I don't want to duplicate at least twice more for similar situations - is there a better way??

If you need this function to be available for multiple forms, then just move it to a Module and make it Public- then you can call it from any of your forms when it's needed.