如何正确查询是否在Access中从Excel中选中了复选框

时间:2015-08-04 13:50:16

标签: excel vba excel-vba ms-access access-vba

Excel中用户窗体上的Checkbox6确定查询的选择条件。我需要使用checkbox6 True / False的属性来查询位于Access中的表中的[Archived]列。例如,如果在userform上选中了Checkbox6,则会向[Archived]列查询“True”值。 [Archived]列是数据类型是/否。我已经尝试了“True”和“False”以及-1和0但它似乎不起作用。请查看下面的代码并建议如何使其工作。

由于

Dim sar4 As String
sar4 = "False"

SQLwhere = "WHERE "


If Len(sar2 & vbNullString) <> 0 Then

If CheckBox7 = True Then
        SQLwhere = SQLwhere & "[MDL_Comments].[AA_change] = '" & sar2 & "' AND "
    Else

SQLwhere = SQLwhere & "[MDL_Comments].[AA_change] LIKE '" & "%" & sar2 & "%" & "' AND "
End If

End If

If CheckBox6 = True Then
SQLwhere = SQLwhere & "[MDL_Table1].[Archived] = '" & sar4 & "' AND "
End If

StrSql = "SELECT * FROM [MDL_Comments] "
 'Remove the last AND applicable
If SQLwhere = "WHERE " Then
    SQLwhere = ""
Else
    SQLwhere = Left(SQLwhere, Len(SQLwhere) - 5)
End If
StrSql = StrSql & SQLwhere

1 个答案:

答案 0 :(得分:1)

我认为问题是你在变量周围使用单引号 - ('“&amp; sar4&amp;”')。这将把变量设置为一个字符串,而不管你赋给它的值。

相关问题