自定义函数“无效的使用 null”的克服方法
常用的Access自定义变量有String,Integer,Long,Single,Double,Variant等,但众多自定义变量中唯有Variant类型系统在使用前会判别是否为空值,即判别变量是否为Null值。
以下有两个例子,例子一的myStr变量定义为String类型,当Command1单击时就会出现Msgbox报错"无效的使用 null"。但例子二就不会出现该类报错,而是正规的反映Msgbox 123。
因为例子二的myStr变量定义为Variant类型,而在函数工作前先检查myStr是否会返回Null值。
例子一:
'假设Text1文本框为空值
Private Sub Command1_Click()
Call TestFun (Text1)
End Sub
Function TestFun(myStr As String)
If IsNull(myStr) Then
MsgBox 123
End If
End Function
例子二:
Private Sub Command1_Click()
Call TestFun (Text1)
End Sub
Function TestFun(myStr As Variant)
If IsNull(myStr) Then
MsgBox 123
End If
End Function