在 Access ComboBox 中无效使用 Null

Posted

技术标签:

【中文标题】在 Access ComboBox 中无效使用 Null【英文标题】:Invalid Use of Null in Access ComboBox 【发布时间】:2017-04-18 17:14:20 【问题描述】:

我正在尝试使用 ComboBox 通过我构建的表单附加查询。 Combobox 应该是可选的,但我似乎无法绕过 Invalid use of null 错误。这是我目前的代码

Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim Box1 As String
Dim strBox1 As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()

'General SQL Code
strSQL = "SELECT * FROM Test1"

'Build the IN string by looping through the listbox
For i = 0 To List6.ListCount - 1
    If List6.Selected(i) Then
        If List6.Column(0, i) = "_All" Then
            flgSelectAll = True
        End If
        strIN = strIN & "'" & List6.Column(0, i) & "',"
    End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [Test1.Brand_Name] in " & _
           "(" & Left(strIN, Len(strIN) - 1) & ")"
'Create the AND string
Box1 = Me.Combo8.Value
 If IsNull(Me.Combo8.Value) Then
 strBox1 = Nz(Me.Combo8.Column(0), "")
 Else: strBox1 = " AND [Test1.Population] = '" & Box1 & "'"
 End If
If Not flgSelectAll Then
    strSQL = strSQL & strWhere & strBox1
End If
 MyDB.QueryDefs.Delete "cpwg"
Set qdef = MyDB.CreateQueryDef("cpwg", strSQL)

'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "cpwg", acViewNormal

我也试过

If IsNull(Box1) Or Box1 = "Null" Then
 strBox1 = Nz(Me.Combo8.Column(0), "")
Else: strBox1 = " AND [Test1.Population] = '" & Box1 & "'"
 End If

【问题讨论】:

【参考方案1】:

试试:

if isnull(me.combo8) then

另外,我不知道您的组合框是如何填充的,但是 Nullno data 不同。也许试试

if me.combo8.value = "" then

【讨论】:

这行得通,.value 是破坏作品的部分。谢谢 我想的差不多了;我提到的 nullno data 点与 .value 方面有关。我不认为你真的会用 Null 填充组合框【参考方案2】:
IF IsNull(Trim(me.combo8)) Then
    'Do Stuff
End if

【讨论】:

以上是关于在 Access ComboBox 中无效使用 Null的主要内容,如果未能解决你的问题,请参考以下文章

在 MS Access 中使用 VBA 查看 Combobox 中的唯一值

表格/查询中不存在MS Access Combobox的值

MS Access ComboBox.Column - 使用名称而不是索引?

使用 MS Access 中的 Linq 填充 ComboBox

WPF ComboBox 默认选中无效

MS Access Form:使用第一个 Combobox 的值来过滤第二个 Combobox 的选项?