MS Access:Null vs'' - 让我发疯

时间:2014-02-11 00:20:31

标签: vba ms-access access-vba

我有一个包含2个嵌入子表单的表单。

subform_1上的on-click事件从所选的subform_1记录(所有TEXT字段)中设置引用5 attrs的subform_2的记录源。

strSQL = "SELECT Table_1.* FROM Table_1 " & _
    "WHERE (((Table_1.Attr_A)= '" & Forms![mainform]![subform_1]![attr1] & "') " & _
    "AND ((Table_1.Attr_B) = '" & Forms![mainform]![subform_1]![attr2] & "') " & _
    "AND ((Table_1.Attr_C) = '" & Forms![mainform]![subform_1]![attr3] & "') " & _
    "AND ((Table_1.Attr_D) = '" & Forms![mainform]![subform_1]![attr4] & "') " & _
    "AND ((Table_1.Attr_E) = '" & Forms![mainform]![subform_1]![attr5] & "'));"
Forms![mainform]![subform_2].Form.RecordSource = strSQL

我的问题是,有些记录在5个必需的attrs中可能有一个NULL值,这是一个有效的条件。周围的''当subform_1值为NULL时,会在集合中生成(0)记录。

有效处理subform_1中的NULL条件的任何建议吗?

2 个答案:

答案 0 :(得分:2)

如果你只考虑Table_1.Attr_A,我认为你说你想要这个......

"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE (t1.Attr_A & '') = '" & Forms![mainform]![subform_1]![attr1] & "'"

如果这是正确的,请根据AND为下一个条件添加Attr_B

"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE " & _
"(t1.Attr_A & '') = '" & Forms![mainform]![subform_1]![attr1] & "'" & _
" AND (t1.Attr_B & '') = '" & Forms![mainform]![subform_1]![attr2] & "'"

然后通过添加剩余条件继续。

答案 1 :(得分:0)

我相信Nz函数也可以使用:

"SELECT t1.* FROM Table_1 AS t1 " & _
"WHERE Nz(t1.Attr_A) = '" & Forms![mainform]![subform_1]![attr1] & "'"