我继承了一个VBA项目,并且正在尝试调试我收到的错误:Invalid use of Null
。一段时间后,我找到了发生错误的位置,但尚未找到具体的罪魁祸首和/或问题的解决方案。请参阅以下snippit(抛出异常的行注释注释):
Dim db As Database, wsp As Workspace
Dim retVal As Variant
Dim tableType As Long
'Check to see if the table name exists in the zLinked_Tables table; if it does, that means its a SQL table
If DCount("*", "[zLinked_Tables]", "[LocalName] = '" & TableName & "' AND [ObjectType] = 'Table'") > 0 Then
'SQL Table
retVal = ExecuteSPT("TRUNCATE TABLE [" & TableName & "]", 0) 'truncate the table via passthrough query on server
If KeyColumn <> "" Then
retVal = ExecuteSPT("DBCC CHECKIDENT([" & TableName & "],RESEED,1)", 0) 'reset the identity column value via passthrough query on server
End If
Else
'MS Access Table
tableType = DLookup("[Type]", "[MSysObjects]", "[Name] = '" & TableName & "'")
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM [" & TableName & "]" 'delete all records from table
If KeyColumn <> "" Then
DoCmd.SetWarnings True
If tableType = 1 Then 'Resident/Local
Set db = CurrentDb
db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)"
Else 'Linked Table
Set wsp = DBEngine.Workspaces(0)
Set db = wsp.OpenDatabase(DLookup("[Database]", "[MSysObjects]", "[Name] = '" & TableName & "'")) 'ERROR THROWN ON THIS LINE
db.Execute "ALTER TABLE [" & TableName & "] ALTER COLUMN [" & KeyColumn & "] COUNTER(1,1)" 'reset the autonumber column value
End If
DoCmd.SetWarnings False
Set db = Nothing
End If
End If
Exit Function
请注意,我检查了TableName
变量,它不是null,而且是一个有效的表名
答案 0 :(得分:2)
这条线上发生了几件事。除了检查TableName
是否为null之外,您还应该检查wsp
是否为空? Set wsp = DBEngine.Workspaces(0)
因任何原因失败了吗?
dlookup失败了吗? DLookup("[Database]", "[MSysObjects]", "[Name] = '" & TableName & "'")
。如果该值为null,则表示您将null
传递给wsp.OpenDatabase()
,这会出现错误:
答案 1 :(得分:2)
您的DLOOKUP在MSYSObjects的“数据库”字段中返回一个没有值(NULL)的行。在即时窗口和executs中键入以下内容。您需要添加代码以允许这种可能性。
我认为你正试图获得完整的路径?
?(DLookup("[Database]", "[MSysObjects]", "[Name] = '" & "Table1" & "'"))