How to use Decimal Data Type in VBA code
Posted on 2004-04-26
I need to record Purchase Order line item quantities to 2 decimal places. In the past I've always used a double data type and forced rounding to 2 decimal places in my forms. I've been toying with the idea of changing the table column from a double to a decimal datatype with a scale of 2 so that I don't have to do any rounding in my forms and have the jet engine take care of it for me at the table level. Since VBA doesn't support a decimal data type, how do I handle these values in code and avoid rounding problems?
For example if I have some code that nees to add decimal data type values, do I read each value into a variant as follows:
dim var1 as variant
dim var2 as variant
dim varSum as variant
var1 = 1.15
var2 = 1.85
varSum = cdec(var1) + cdec(var2)
How does VBA handle rounding in the above style code?
Or do I convert each value to a double and do the rounding myself:
dim dbl1 as double
dim dbl2 as double
dim dblSum as double