Recently, I found that a piece of code is not very good at processing null values. I wrote two sections of code, one for me and the other for my brother. Which one is better?
'<Summary> convert an empty field to an empty string </Summary>
'<Param name = "field"> Field instance </param>
'<Return> Field content </return>
Public Function convertnull2string (field as ADODB. Field) as Variant
Require me. isinstance (field), me, "convertnull2string", "field must be instantiated"
If field. type = advarwchar or field. type = adchar then
Convertnull2string = field. Value &""
Else
Convertnull2string = field. Value
End if
End Function
'Get the value of the specified field
Public Function getfieldvalue (field as field, optional isnumeric as Boolean = false) as Variant
On Error goto E
If not field is nothing then
If isnull (field. Value) then
If isnumeric then
Getfieldvalue = 0
Else
Getfieldvalue = ""
End if
Else
Getfieldvalue = field. Value
End if
Else
Getfieldvalue = ""
End if
Exit Function
E:
Getfieldvalue = ""
End Function
Of course, it would be even better if it is processed in the SQL statement, as shown below:
Select a, isnull (B, '0') as B from tnulls
Related Articles:
Http://www.access-cn.com/Article/Class5/Class28/Class30/200508/1744.html
Http://www.zdnet.com.cn/developer/database/story/0,3800066906,39412365,00.htm
Http://unruledboy.cnblogs.com/archive/2004/06/27/18988.aspx