2016-07-28 58 views
0

我需要通過要求用戶輸入帳號和日期範圍來驗證帳戶在特定時間段內完成的操作,但每次運行時都會出現「類型不匹配「按日期顯示帳戶操作

下面是代碼:

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

友情提示,'strCriteria'和'strCount'的變體。請參閱[doc](http://stackoverflow.com/documentation/vba/877/declaring-variables/2957/dim#t=201607281239547850873) – litelite

回答

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 & "'"