运行时错误13 :-(

时间:2016-11-24 02:36:42

标签: vba ms-access

我创建了一个搜索表单。我的搜索表单包含一个子表单,我一直试图让搜索按钮工作。我的搜索按钮将帮助我运行基于日期和产品的搜索表单。

我一直收到错误"运行时错误13"

错误位于***

的行

如果有人在此搜索表单上协助我,我将非常感激。谢谢! :-)

这些是我的代码

Private Sub Search_Click()

    Dim strProduct As String
    Dim strDatePicker As Date
    Dim sql As String

    sql = "select * from 2_WIP where "
    strDatePicker = format(Me.DatePicker, "dd/mm/yyyy")

    If IsDate(Me.DatePicker) Then
       ***strDatePicker = " [Date] Like '*'"***
    Else
       strDatePicker = " [Date] = '" & Me.DatePicker & "'"
    End If

    If IsNull(Me.Product) Then
       strProduct = " [Product] like '*'"
    Else
       strProduct = " [Product] like '" & Me.Product & "'"
    End If

    sql = strDatePicker & "and" & strProduct

    Me.subfrmWIP.Form.RecordSource = sql
    Me.subfrmWIP.Form.Requery

End Sub

1 个答案:

答案 0 :(得分:2)

您正在尝试将非日期字符串值分配给日期变量,因此您收到错误。

Dim strDatePicker As Date
strDatePicker = " [Date] Like '*'"

这项任务是倒退的。

If IsDate(Me.DatePicker) Then
   ***strDatePicker = " [Date] Like '*'"***
Else
   strDatePicker = " [Date] = '" & Me.DatePicker & "'"
End If
Private Sub Search_Click()

    Dim ProductSearch As String, DateSearch As String
    Dim sql As String

    sql = "SELECT * FROM 2_WIP WHERE "

    If IsDate(Me.DatePicker) Then
        DateSearch = "(([2_WIP].[Date])>=#" & DateValue(Me.DatePicker) & _
        "# And ([2_WIP].[Date])<#" & DateValue(Me.DatePicker) + 1 & "#) "
    Else
        DateSearch = " [Date] Like '*'"
    End If

    If IsNull(Me.Product) Then
        ProductSearch = " [Product] like '*'"
    Else
        ProductSearch = " [Product] like '" & Me.Product & "'"
    End If

    sql = sql & DateSearch & " AND " & ProductSearch

    Me.subfrmWIP.Form.RecordSource = sql
    Me.subfrmWIP.Form.Requery

End Sub

样本输出

  

SELECT * FROM 2_WIP WHERE [Date] =#11/23/2016#AND [Product] like&#39; 16oz Coke&#39;

如何使用Query Builder帮助在VBA中构建SQL

enter image description here