1

我已经解决了所有以前提出的问题,甚至尝试了解决方案,但无济于事,所以这是我的困境。我使用了来自 Allenbrowne 的搜索查询,它最初很棒。但是,我想添加到我已经完成的搜索条件,并且在第一次搜索时我得到了我正在寻找的结果。我现在使用不同的代码来删除所有搜索条件并取消过滤表单(拆分表单)。再次一切正常。现在我想使用相同的不同参数再次搜索,这次我得到运行时错误 3075,说明我在查询表达式中有一个额外的 )。我什至查看了查询以查看这个额外的 ) 在哪里,但无济于事。代码如下,在 Me.Filter = strWhere 上出错。在清除搜索屏幕并想要开始新搜索时,我还提供了重置过滤器。任何帮助将不胜感激。

Option Compare Database
Option Explicit

Private Sub cmdFilter_Click()


    Dim strWhere As String                  'The criteria string.
    Dim lngLen As Long                      'Length of the criteria string to append to.

    '***********************************************************************
    'Look at each search box, and build up the criteria string from the non-blank ones.
    '***********************************************************************

    If Not IsNull(Me.txtCustID) Then
        strWhere = strWhere & "([Customer_ID] = " & Me.txtCustID & ") AND "
    End If

     If Not IsNull(Me.txtJobID) Then
        strWhere = strWhere & "([Job_ID] = " & Me.txtJobID & ") AND "
    End If

     If Not IsNull(Me.txtName) Then
        strWhere = strWhere & "([Name] Like ""*" & Me.txtName & "*"") AND "
    End If

    If Not IsNull(Me.TxtPostcode) Then
        strWhere = strWhere & "([Postcode] = ""*" & Me.TxtPostcode & "*"") AND "
    End If

    If Not IsNull(Me.txtCompany) Then
        strWhere = strWhere & "([CompanyName] Like ""*" & Me.txtCompany & "*"") AND "
    End If

    If Not IsNull(Me.txtLocation) Then
        strWhere = strWhere & "([Location] Like ""*" & Me.txtLocation & "*"") AND "
    End If

    If Not IsNull(Me.CboStatus) Then
        strWhere = strWhere & "([Status] = " & Me.CboStatus & ") AND "
    End If

    If Not IsNull(Me.CboSource) Then
        strWhere = strWhere & "([EnquirySource] = " & Me.CboSource & ") AND "
    End If

        'See if the string has more than 4 characters (a trailng " AND ") to remove.
    lngLen = Len(strWhere) - 4
    If lngLen <= 0 Then     'Nah: there was nothing in the string.
        MsgBox "No criteria", vbInformation, "Nothing to do."
    Else                    'Yep: there is something there, so remove the " AND " at the end.
        strWhere = Left$(strWhere, lngLen)
        'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
    Debug.Print strWhere

        'Finally, apply the string as the form's Filter.
        Me.Filter = strWhere
        Me.FilterOn = True
    End If
End Sub

Private Sub cmdReset_Click()
    'Purpose:   Clear all the search boxes in the Form Header, and show all records again.
    Dim ctl As Control

    'Clear all the controls in the Form Header section.
    For Each ctl In Me.Section(acHeader).Controls
        Select Case ctl.ControlType
        Case acTextBox, acComboBox
            ctl.Value = ""
        Case acCheckBox
            ctl.Value = False
        End Select
    Next

    'Remove the form's filter.
    Me.FilterOn = False

End Sub
4

1 回答 1

1

看到这个ctl.Value = ""cmdReset_Click()?我猜想,当您清除控件时,其中一个以零长度字符串结尾,但是您在构建过滤器时的测试是针对空字符串,而不是零长度字符串,所以很可能是其中一个数字是结束了somefield=<blank>。尝试:

 ctl.Value = Null

顺便说一句,将equals与通配符一起使用是没有意义的:

strWhere = strWhere & "([Postcode] = ""*" & Me.TxtPostcode & "*"") AND "

应该:

strWhere = strWhere & "([Postcode] Like ""*" & Me.TxtPostcode & "*"") AND "

或者

strWhere = strWhere & "([Postcode] = """ & Me.TxtPostcode & """) AND "
于 2012-08-14T16:19:06.723 回答