从/名称 - 运输日期搜索

时间:2015-04-24 10:54:43

标签: ms-access access-vba

按名称搜索有效,但当我尝试按日期过滤时,会出现此错误:

运行时错误'3075':

Syntax error(missing operator) in query expression 'DriverFirst Like'*paul*'TransportDate Like '*4/5/2015*".

下面是代码:

Private Sub Command33_Click()

    Dim strSQL As String
    Dim boolBefore As Boolean

    strSQL = ""
    boolBefore = False

    If Len(Me.txtSrch) > 1 Then
        If (boolBefore) Then
            strSQL = strSQL & " AND "
        End If

        strSQL = strSQL & "DriverFirst Like '*" & Me.txtSrch & "*'"
    End If

    If Len(Me.fromDate) > 1 Then
        If (boolBefore) Then
            strSQL = strSQL & " AND "
        End If

        strSQL = strSQL & "TransportDate Like '*" & Me.fromDate & "*'"
        boolBefore = True
    End If

    Me.Form.Filter = strSQL
    Me.Form.FilterOn = True

End Sub

2 个答案:

答案 0 :(得分:1)

您在日期中连接了星号通配符。我认为Access查询应该使用hashmarks(例如#)来包装日期和直接比较而不是模式匹配。

strSQL = strSQL & "TransportDate = #" & Me.fromDate & "#"

答案 1 :(得分:0)

TransportDate喜欢fromDate

没有多大意义。此外,您必须使用格式正确的字符串表达式作为日期值,因此:

strSQL = strSQL & " TransportDate = #" & Format(Me!fromDate, "yyyy\/mm\/dd") & "#"