按日期显示帐户操作

时间:2016-07-28 12:21:26

标签: vba ms-access access-vba

我需要通过要求用户输入帐号和日期范围来验证帐户在特定时间段内完成的操作,但每次运行时我都会收到错误“type mismatch”

以下是代码:

Private Sub cmdSearch_Click()
    Call search
End Sub

Sub search()
    Dim strCriteria, strCount, task As String

    Me.Refresh
    If IsNull(Me.compte_hist) Or IsNull(Me.date_deb) Or IsNull(Me.date_fin) Then
        MsgBox "s'il vous plaît assurez-vous que tous les champs sont remplis", vbInformation, "Date Range Required"
        Me.compte_hist.SetFocus
    Else
        strCriteria = "([Date_operation]>= #" & Me.date_deb & "# And [Date_operation] <= #" & Me.date_fin & "#)"
        strCount = "[Compte]=#" & Me.compte_hist & "#"
        task = "select * from Operations where Operations  (" & strCriteria & ")" And " (" & strCount & ") order by [Date_operation]" 
        DoCmd.ApplyFilter task
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

试试这个:

strCriteria = "([Date_operation]>= #" & Format(Me.date_deb, "mm\/dd\/yyyy") & "# And [Date_operation] <= #" & Format(Me.date_fin, "mm\/dd\/yyyy") & "#)"
strCount = "[Compte]=" & Me.compte_hist
task = "select * from Operations where (" & strCriteria & ") And  (" & strCount & ") order by [Date_operation]"
Me.RecordSource = task

此外,您只能应用过滤器:

strCriteria = "([Date_operation]>= #" & Format(Me.date_deb, "mm\/dd\/yyyy") & "# And [Date_operation] <= #" & Format(Me.date_fin, "mm\/dd\/yyyy") & "#)"
strCount = "[Compte]=" & Me.compte_hist
task = "(" & strCriteria & ") And  (" & strCount & ")"
Me.Filter = task
Me.FilterOn = True

如果帐号不是数字,请使用引号:

strCount = "[Compte]='" & Me.compte_hist & "'"