2

我有一个 MS Access 表单,用户可以在其中选择最多四个条件来过滤报告返回的记录。在 VBA 中,我试图根据用户选择过滤的字段(如果有)动态构建过滤器字符串。下面的代码给了我一个“Block If without End If”错误。我在这里做错了什么?

If (IsNull(frm!employee) = False) Then
    strFilter = "Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    If IsNull(frm!sop) = False Then
        strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
    End If
    If IsNull(frm!revision) = False Then
        strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
    End If
    If IsNull(frm!dept) = False Then
        strFilter = strFilter + " AND department = '" & frm!dept & "'"
    End If
Else
If (IsNull(frm!sop) = False) Then
    strFilter = "sop_number = '" & frm!sop & "'"
    If (IsNull(frm!employee) = False) Then
        strFilter = "Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    End If
    If IsNull(frm!revision) = False Then
        strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
    End If
    If IsNull(frm!dept) = False Then
        strFilter = strFilter + " AND department = '" & frm!dept & "'"
    End If
Else
If (IsNull(frm!revision) = False) Then
    strFilter = "revision_number = '" & frm!revision & "'"
    If (IsNull(frm!employee) = False) Then
        strFilter = " AND Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    End If
    If IsNull(frm!sop) = False Then
        strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
    End If
    If IsNull(frm!dept) = False Then
        strFilter = strFilter + " AND department = '" & frm!dept & "'"
    End If
Else
If IsNull(frm!dept) = False Then
    strFilter = "department = '" & frm!dept & "'"
    If (IsNull(frm!employee) = False) Then
        strFilter = " AND Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
    End If
    If IsNull(frm!sop) = False Then
        strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
    End If
    If IsNull(frm!revision) = False Then
        strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
    End If
End If

您可以给我任何改进此代码的建议将不胜感激。

4

2 回答 2

4

该代码避免在strFilter. 然而,逻辑很难遵循,它在相同的基本主题上使用了多种变体。

使用更简单的方法。您对 4 个表单控件中包含的值感兴趣:

  • frm!employee
  • frm!sop
  • frm!Revision
  • frm!dept

依次检查它们中的每一个,对于任何包含值的,添加一个以“AND”开头的段strFilter。之后,如果strFilter包含任何文本,您知道它以“AND”开头,因此您可以简单地丢弃前 5 个字符。

strFilter = vbNullString ' <- make it explicit
If Len(Trim(frm!employee) & vbNullString) > 0 Then
    strFilter = strFilter & _
        " AND Trainee_First_Name & ' ' &  Trainee_Last_Name = '" & _
        frm!employee & "'"
End If
If Len(Trim(frm!sop) & vbNullString) > 0 Then
    strFilter = strFilter & " AND sop_number = '" & frm!sop & "'"
End If
If Len(Trim(frm!Revision) & vbNullString) > 0 Then
    strFilter = strFilter & " AND revision_number = '" & _
        frm!Revision & "'"
End If
If Len(Trim(frm!dept) & vbNullString) > 0 Then
    strFilter = strFilter & " AND department = '" & frm!dept & "'"
End If
If Len(strFilter) > 0 Then
    ' discard " AND " from beginning of string
    strFilter = Mid(strFilter, 6)
End If
MsgBox "strFilter ->" & strFilter & "<-"
于 2013-01-18T22:38:23.353 回答
2

问题是您的else... if子句没有以end if. 您需要使用ElseIf

Else
  If ...
  End If
Else...

我在下面修改了您的代码以使用后一种语法。注意下面的End If行。

注意:您不需要**,这只是在您的代码块中引起注意!

If (IsNull(frm!employee) = False) Then
        strFilter = "Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
        If IsNull(frm!sop) = False Then
            strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
        End If
        If IsNull(frm!revision) = False Then
            strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
        End If
        If IsNull(frm!dept) = False Then
            strFilter = strFilter + " AND department = '" & frm!dept & "'"
        End If
    Else
    If (IsNull(frm!sop) = False) Then
        strFilter = "sop_number = '" & frm!sop & "'"
        If (IsNull(frm!employee) = False) Then
            strFilter = "Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
        End If
        If IsNull(frm!revision) = False Then
            strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
        End If
        If IsNull(frm!dept) = False Then
            strFilter = strFilter + " AND department = '" & frm!dept & "'"
        End If


**End If**
    Else
    If (IsNull(frm!revision) = False) Then
        strFilter = "revision_number = '" & frm!revision & "'"
        If (IsNull(frm!employee) = False) Then
            strFilter = " AND Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
        End If
        If IsNull(frm!sop) = False Then
            strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
        End If
        If IsNull(frm!dept) = False Then
            strFilter = strFilter + " AND department = '" & frm!dept & "'"
        End If
    **End If**
    Else
    If IsNull(frm!dept) = False Then
        strFilter = "department = '" & frm!dept & "'"
        If (IsNull(frm!employee) = False) Then
            strFilter = " AND Trainee_First_Name + ' ' +  Trainee_Last_Name = '" & frm!employee & "'"
        End If
        If IsNull(frm!sop) = False Then
            strFilter = strFilter + " AND sop_number = '" & frm!sop & "'"""
        End If
        If IsNull(frm!revision) = False Then
            strFilter = strFilter + " AND revision_number = '" & frm!revision & "'"
        End If
    **End If**
    End If
于 2013-01-18T21:19:11.553 回答