2017-04-18 44 views
1

我想通過我建立的窗體來使用ComboBox來附加查詢。 Combobox應該是可選的,但我似乎無法解決空錯誤的無效使用。這是我目前訪問組合框中無效的空使用

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

嘗試代碼:

if isnull(me.combo8) then 

而且,我不知道你的組合框的填充方式,但是不同於沒有數據。也許嘗試

if me.combo8.value = "" then 
+0

這個工作的。價值是被脫膠了作品的作品。謝謝 – SQLnRprobs

+0

我想盡可能多;我提到的_null_ <>到_no data_ point屬於.value方面。我不認爲你真的會永遠填充組合框 – scott

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

這也適用,謝謝 – SQLnRprobs