Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 将Null显式分配给variant类型的变量时,Null的使用无效_Ms Access_Vba - Fatal编程技术网

Ms access 将Null显式分配给variant类型的变量时,Null的使用无效

Ms access 将Null显式分配给variant类型的变量时,Null的使用无效,ms-access,vba,Ms Access,Vba,我目前正在尝试将一个旧的ADP项目从Access 2010 x64升级到Access 2019 x64。我已设法将其转换为.accdb文件,但现在我的VBA代码出现了错误 请考虑以下功能: Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean Dim cnTemp As ADODB.Connection, rsTemp As ADODB.Recordset Dim sSQL As St

我目前正在尝试将一个旧的ADP项目从Access 2010 x64升级到Access 2019 x64。我已设法将其转换为.accdb文件,但现在我的VBA代码出现了错误

请考虑以下功能:

Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
  Dim cnTemp As ADODB.Connection, rsTemp As ADODB.Recordset
  Dim sSQL As String
  On Error GoTo LAB_Error
  sSQL = "SELECT T_Value FROM INT_SystemSettings WHERE (T_Key = '" & sKey & "')"
  Set cnTemp = New ADODB.Connection
  Set rsTemp = New ADODB.Recordset
  cnTemp.CursorLocation = adUseServer
  cnTemp.Open CurrentProject.BaseConnectionString
  rsTemp.Open sSQL, cnTemp, adOpenForwardOnly, adLockReadOnly
  If (rsTemp.EOF) Then GoTo LAB_Error
  vValue = Nz(rsTemp![T_Value])
  rsTemp.Close
  cnTemp.Close
  On Error GoTo 0
  GetSystemSetting = True
  Exit Function
LAB_Error:
  vValue = Null
  If (rsTemp.State <> adStateClosed) Then rsTemp.Close
  If (cnTemp.State <> adStateClosed) Then cnTemp.Close
  GetSystemSetting = False
End Function
执行此行时,将引发运行时错误:

Invalid use of Null
我在不同的网站上读过几十篇关于该错误消息的文章,包括这篇,但它总是归结为OP没有将目标变量设置为
变量
。但在我的例子中,目标变量vValue,
variant
类型的
。此外,该代码在Access 2010 x64中运行了8年,没有任何问题


该错误的原因是什么?我如何防止它?

请记住,对于这样的函数:

Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
    vValue = Null
除非指定
ByVal
,否则参数将通过
ByRef
传递,因此您实际上是在写入调用函数时用作参数的变量

如果该变量不是变量,则会触发错误

Dim str As String
If GetSystemSetting("non-existing", str) Then    ' KA-BOOM!

使用
DLookup
的替代方案如下所示。它的行为应该完全相同,除非您的有效系统设置为NULL

Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
  ' DLookup returns NULL if no record is found
  vValue = DLookup("T_Value", "INT_SystemSettings", "T_Key = '" & sKey & "'")
  GetSystemSetting = Not IsNull(vValue)
End Function

DLookup
是一个只读操作,因此在锁定方面应该是相同的。

如何调用此函数
vValue
不是局部变量,它是调用函数的变量(ByRef),不一定是变量。另外,您可以用一个简单的
DLookup
调用替换整个函数。@Andre非常感谢!这就是问题所在。。。不确定我怎么会错过它(我的最后一个VBA编码是几年前的事了…)。关于
DLookup
,我不确定它是否完全按照函数的原始作者的意图进行锁定;一般来说,我希望在这个beast中尽可能少地进行更改(其中包含大量代码…)。你介意回答一下你的意见吗?
Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
  ' DLookup returns NULL if no record is found
  vValue = DLookup("T_Value", "INT_SystemSettings", "T_Key = '" & sKey & "'")
  GetSystemSetting = Not IsNull(vValue)
End Function