Search Query Works First Time但后续运行在查询表达式中获得运行时错误3075额外)

时间:2012-08-14 14:47:51

标签: vba ms-access search

我已经完成了之前提出的所有问题,甚至尝试了解决方案,但无济于事,所以这里是我的困境。我使用了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

1 个答案:

答案 0 :(得分:1)

ctl.Value = ""中查看此cmdReset_Click()?我猜想当你清除控件时,其中一个是结束一个零长度的字符串,但你在构建过滤器时的测试是null,而不是零长度的字符串,所以某些东西,很可能是数字之一,是结束somefield=<blank>。尝试:

 ctl.Value = Null

顺便说一下,使用等于狂野卡片是没有意义的:

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

应该是:

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

或者

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