试图在VBA中解决"无效使用Null"

Hor*_*Kol 5 vba access-vba

首先是快速代码段:

Dim GUID As String
Dim givenNames, familyName, preferredName, gender, comments, carer, medicareNumber, patientNumber As String
Dim dob As Variant
Dim deceased, resolved, consultNotes As Boolean
Dim age As Variant

givenNames = Null
familyName = Null
preferredName = Null
gender = Null
dob = Null
comments = Null
deceased = False
resolved = False
carer = Null
age = Null
consultNotes = False
patientNumber = Null ' This is where I get the error
Run Code Online (Sandbox Code Playgroud)

知道为什么这个最后一个变量会绊倒吗?我已经将Null分配给了许多其他字符串而没有任何错误.

Tho*_*mas 13

在VBA/VB6中,字符串不能设置为Null; 只有Variants可以设置为null.另外,当您在问题中声明内联逗号分隔的变量时,只会将最后一个键入为字符串; 所有其他人都被打字为变种.要将它们作为类型声明在一行上,您必须包含该类型

Dim a As String, Dim b As String ...
Run Code Online (Sandbox Code Playgroud)

这就是为什么只在一条线上声明它们是有道理的.

(顺便说一句,应该注意的deceased, resolved是,由于同样的原因,它们也被输入为变体.)


Ste*_*sen 7

它成功的原因givenNames是你无意中将它们定义为Variant类型.它失败了patientNumber,因为你成功地将其定义为a String,并且字符串不接受Null值.

在一个Dim语句中,该As <type>子句适用于列表中的每个单独变量,因此只将它放在列表的末尾,您只将显式类型应用于最后列出的变量.隐式类型Variant应用于其他类型.