MS-Access 2010中的VBA应用程序定义或对象定义错误

时间:2018-05-18 08:36:43

标签: vba access-vba

我正在尝试开发一个MS Access项目,该项目应根据多个搜索条件搜索记录,例如开始和结束日期,事件发生地,事件名称等。但我不太明白为什么我这样做错误:

    Private Sub CommandSearch_Click()
 Dim strReport As String 'Name of report to open.
    Dim strField As String 'Name of your date field.
    Dim strMessage As String 'Message on no data for selection.
    Dim varWhere As String 'Where condition for OpenReport.

    ' Const conDateFormat = "\#mm\/dd\/yyyy\#"
    Const conDateFormat = "\#dd\/mm\/yyyy\#"

    ' strReport = "RB_Events"
    strField = "[Event_StartDate]"
    varWhere = ""

    ' ================================================================================
    ' CHECK FOR DATE RANGE
    ' ================================================================================
    If IsNull(Me.txtEvent_StartDate) Then
        If Not IsNull(Me.txtEvent_EndDate) Then  'End date, but no start.
            varWhere = "(" & strField & " <= " & Format(Me.txtEvent_EndDate, conDateFormat) & ") "
        Else
            'neither start nor end dates, do nothing to varWhere
        End If
    Else
        If IsNull(Me.txtEvent_EndDate) Then  'Start date, but no End.
            varWhere = "(" & strField & " >= " & Format(Me.txtEvent_StartDate, conDateFormat) & ") "
        Else 'Both start and end dates present.
            varWhere = "(" & strField & " Between " & Format(Me.txtEvent_StartDate, conDateFormat) _
            & " AND " & Format(Me.txtEvent_EndDate, conDateFormat) & ")"
        End If
    End If

    ' =============================================================
    ' CHECK FOR data in fields
    ' ================================================================================

      Dim strSQL As String

        ' Check for anything like event name
    If Me.txtEvent_Name <> "" Then
        varWhere = varWhere & "[Event_Name] Like '*" & Me.txtEvent_Name & "*' AND "
    End If

  'Drop the last " AND " from the string
    If Right(strSQL, 5) = " AND " Then
        strSQL = Left(strSQL, Len(strSQL) - 5)
    End If

    'next add the where to the front of the string if the user has entered something in at least one field
    If strSQL <> "False" Then
        If varWhere = "" Then
            varWhere = strSQL
        Else
            varWhere = varWhere & " AND " & strSQL
        End If
    End If

    '================================================================================

    Debug.Print varWhere

    If varWhere <> "" Then
        'Update the list box row source

Forms!F_EVENTS.RowSource = "SELECT * FROM Q_EVENTS WHERE " & varWhere

    Else
        'Update the list box row source
       Forms!F_EVENTS.RowSource = "SELECT * FROM Q_EVENTS"

    End If

    ' Requery the list box
    Forms!F_EVENTS.Requery

Exit_cmdSearch_Click:
   Exit Sub

Err_cmdSearch_Click:
    MsgBox "You did not fill in any data", vbCritical, "No data"
    Resume Exit_cmdSearch_Click
End Sub

1 个答案:

答案 0 :(得分:0)

RowSource用于组合/列表框。所以:

Forms!F_EVENTS.RecordSource = "SELECT * FROM Q_EVENTS WHERE " & varWhere –