Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997

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 numeric database fields to zeroes:

 
Dim nVar As Integer
nVar = 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 numeric variable, but to a grid cell or a text box, you do not get what you 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 need it!) the leading space produced by Str$() function.

Garold Minkin
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date