Access ComboBox

时间:2017-04-18 17:14:21

标签: vba combobox access-vba

我正在尝试使用ComboBox通过我构建的表单追加查询。 Combobox应该是可选的,但我似乎无法解决无效使用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

2 个答案:

答案 0 :(得分:2)

尝试:

if isnull(me.combo8) then

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

if me.combo8.value = "" then

答案 1 :(得分:1)

IF IsNull(Trim(me.combo8)) Then
    'Do Stuff
End if