devxlogo

More on Null Conversion

More on Null Conversion

This tip has been published more than once on pages of VBPJ(See “99 of the Hottest Tech Tips For VB Developers,”Supplement to the February 1996 issue of VBPJ, page 17).

This code is recommended to convert Null values in the numericdatabase fields to zeroes:

 Dim nVar As IntegernVar = 0 & rs!nField' assumed that nField ' is a numeric field in ' the recordset

The expression 0 & rs!nField actually returns a string,not a number. If, say, rs!nField contains 1, 0 &rs!nField results in “01.”

The code above works due to the automatic conversion of types.If, however, you need to assign the numeric value, not to a numericvariable, but to a grid cell or a text box, you do not get whatyou want, and additional data formatting is required.

You might consider three other ways to get rid of Nulls:

 ' This will work, and in VB4 you do ' not have to include MSAFINX.DLL ' with your project' (as you did in VB3). However, the ' expression might look a bit too long...nVar = IIf(IsNull(rs!nField), 0, _        rs!nField)

This will work, both in VB3 and VB4:

 nVar = Val("" & rs!nField)

Or:

 nVar = Val(0 & rs!nField)

I always use:

 "" & n  

instead of Str$(n) when I do not need (and I hardly ever needit!) the leading space produced by Str$() function.

devx-admin

Share the Post: