Filter between two dates MS Access - vba

I have a form where i created two text boxes to choose the start and end date and a button to filter the table in the form by the dates chosen.
My vba code does not work. Can someone help?
This is the code:
Private Sub Command150_Click()
If IsNull(Me.Text153) And IsNull(Me.Text155) Then
Me.FilterOn = False
Else
Me.Filter = "[Datum] BETWEEN #" & Me.Text153 & "# AND #" & Me.Text155 & "#"
Me.FilterOn = True
End If
End Sub
Note: Text153 is start date, Text 155 is end date, Datum is the field in the table which is filtered.
The dates filtered should be >= Start date and <= End date.

Datum sounds German, so you may have to force a slash as the date separator:
Private Sub Command150_Click()
Dim Filter As String
If IsNull(Me.Text153) Or IsNull(Me.Text155) Then
Me.FilterOn = False
Else
Filter = "[Datum] BETWEEN #" & Format(Me.Text153, "yyyy\/mm\/dd") & "# AND #" & Format(Me.Text155, "yyyy\/mm\/dd") & "#"
' Print the filter string to the Immediate window (press Ctrl+G).
Debug.Print Filter
Me.Filter = Filter
Me.FilterOn = True
End If
End Sub

Related

Filter form records by date range using two unbound text boxes

Good afternoon.
I have a continuous form which I want to allow the user to filter by using unbound boxes in the form header. See screenshot of form below:
Form Design View
I can filter error type (Unbound: cboErrorType) and responsibility (Unbound: cboResponsibility) without any issues, but I cannot get the date range to work (Records outside of the selected date range are being returned). Please see screenshot:
Form - filtered
Please see my code below:
Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#dd\/mm\/yyyy\#"
If Not IsNull(Me.cboErrorType) Then
strWhere = strWhere & "([ErrorType] = """ & Me.cboErrorType & """) AND "
End If
If Not IsNull(Me.cboResponsibility) Then
strWhere = strWhere & "([Decision] = """ & Me.cboResponsibility & """) AND "
End If
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([Date of Error] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
End If
If Not IsNull(Me.txtEndDate) Then
strWhere = strWhere & "([Date of Error] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
'Debug.Print strWhere
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
I am removing the filters with the following code:
Private Sub cmdResetFilter_Click()
Dim ctl As Control
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
ctl.value = Null
End Select
Next
Me.FilterOn = False
End Sub
Please can someone suggest a solution?
If further information is required, let me know and I will oblige.
Many thanks.
The date format must be either the "reverse" US format (mm/dd/yyyy) or the ISO format (which also works for ADO). So try:
Const conJetDate = "\#yyyy\/mm\/dd\#"

Searching for data in a combo list

I have a database with a table ALL_INCOME which contains all my data with reference to income.
I managed to create a search button which is able to search the data in a date range successfully.
My table has these fields:
Date,Type of income, Amount.
I will like to search in a date range so it can pick a specific type of income records to be displayed.
For instance, if I have investment, savings as the type of income in my table and also in my combo list, I will like to be able to search in a date range using a specific type of income.
These are my codes which is able to search data in a date range that displays all data.
Private Sub Command20_Click()
' Search button
Call Search
End Sub
Sub Search()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.OrderDateFrom) Or IsNull(Me.OrderDateTo) Then
MsgBox "Please enter the date range", vbInformation, "Date Range Required"
Me.OrderDateFrom.SetFocus
Else
strCriteria = "([DATE] >= #" & Me.OrderDateFrom & "# And [DATE] <= #" & Me.OrderDateTo & "#)"
task = "select * from ALL_INCOME where (" & strCriteria & ") order by [DATE]"
DoCmd.ApplyFilter task
End If
End Sub
Any help with this will be greatly appreciated.
Thank you
Try below codes. It will search specific IncomeType between two date range. If you do not select any IncomeType then it will filter all IncomeType in specified date range.
Option Compare Database
Option Explicit
Private Sub cmdApplyFilter_Click()
Call Search
End Sub
Sub Search()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.OrderDateFrom) Or IsNull(Me.OrderDateTo) Then
MsgBox "Please enter the date range", vbInformation, "Date Range Required"
Me.OrderDateFrom.SetFocus
Else
strCriteria = "[MyDate] >= " & Format(Me.OrderDateFrom, "\#mm\/dd\/yyyy\#") & _
" And [MyDate] <= " & Format(Me.OrderDateTo, "\#mm\/dd\/yyyy\#") & _
" AND [IncomeType] LIKE '" & IIf(IsNull(Me.cboIncomeType), "*", Me.cboIncomeType) & "'"
'task = "select * from ALL_INCOME where (" & strCriteria & ") order by [DATE]"
DoCmd.ApplyFilter , strCriteria
End If
End Sub

ApplyFilter works until I hit the clear button which clears the filters

I have created a form which should filter by dates that users can type in. After typing the needed dates the user clicks the "limit"-button so that data can filter these range. It works really fine until the user hits the clear button.
The filtering STILL works BUT the validation weather the users has typed the dates ist NOT working. It throws the error "runtime error 3000. reserved error (-3201)
The validation is checking weather the date is typed in, if not, a message box should appears.
--This is the "limit button" that shows a range of dates--
Private Sub eingrenzen()
Dim strCriteria, task As String
'MsgBox if no entry
' "Me.vonDatumFeld" is german and means "fromDate"
' "Me.bisDatumFeld" means "untilDate"
Me.Refresh
If IsNull(Me.vonDatumFeld) Or IsNull(Me.bisDatumFeld)
Then
MsgBox "Grenzen sie bitte den Zeitraum 'von' und 'bis' ein", vbInformation, "Zeitraum eingrenzen"
Me.vonDatumFeld.SetFocus
Else
strCriteria = "([Datum] >= #" & Format(Me.vonDatumFeld, "yyyy-mm-dd") & "# And [Datum] <= #" & Format(Me.bisDatumFeld, "yyyy-mm-dd") & "#)"
task = "select * from TblTeile where (" & strCriteria & ") order by [Datum]"
DoCmd.ApplyFilter task 'Wende ZeitraumFILTER an
End If
End Sub
--And this is the "clear button"--
Private Sub CmdLösch_Click()
Me.KIDText = vbNullString
Me.FirmaTxt = vbNullString
Me.DatumTxt = vbNullString
Me.ProdArttxt = vbNullString
Me.BeschrText = vbNullString
Me.MaterialText = vbNullString
Me.vonDatumFeld = vbNullString
Me.bisDatumFeld = vbNullString
Me.Filter = vbNullString
Me.FilterOn = False
Me.Requery
End Sub
Use the Filter method:
strCriteria = "[Datum] >= #" & Format(Me.vonDatumFeld, "yyyy-mm-dd") & "# And [Datum] <= #" & Format(Me.bisDatumFeld, "yyyy-mm-dd") & "#"
' task = "select * from TblTeile where (" & strCriteria & ") order by [Datum]"
Me.Filter = strCriteria
Me.FilterOn = True

VBA at Access - Filter with several criteria, where one criteria can be Null

I got some problems with my vba code.
I got a form at access with a filter.
At the moment the one filter is for start/end date and a Person.
And because i am a noob, there is an other Filter for a cost centre and the start/end date.
I want to combine both, but with the case, cost centre can be Null or not.
My "main" filter looks like this:
Private Sub Befehl51_Click()
If Nz(Me.txtvon, "") = "" Then //StartDate
MsgBox "Bitte Datumsbereich wählen!"
Exit Sub
End If
If Nz(Me.txtbis, "") = "" Then //EndDate
MsgBox "Bitte Datumsbereich wählen!"
Exit Sub
End If
Me.Filter = "[TaetigkeitsDatum] between " & Format(Nz(Me!txtvon, Date),"\#yyyy-mm-dd\#") & "
and " & Format(Nz(Me!txtbis, Date), "\#yyyy-mm-dd\#") & " And " & "[PersonalID] = " & Me.Liste0 & ""
Me.FilterOn = True
End Sub
The syntax for the cost criteria is like:
[TaetigkeitsKostenstellenIDRef] = "Kombinationsfeld145"
Thanks for help!
You're nearly there, there are just some minor SQL syntax errors.
I've written it to separate lines to increase readability and make understanding easier
Me.Filter = "[TaetigkeitsDatum] BETWEEN " & Format(Nz(Me!txtvon, Date),"\#yyyy-mm-dd\#") & " AND " & Format(Nz(Me!txtbis, Date), "\#yyyy-mm-dd\#") & _
" AND [PersonalID] = " & Me.Liste0
If IsNumeric(Me!Kombinationsfeld145) Then
Me.Filter = Me.Filter & " AND [TaetigkeitsKostenstellenIDRef] =" & Me!Kombinationsfeld145
End If
Some of the changes I've made:
Spacing was off. Before every And and variable name, there should be a space.
The " & " between And and a variable name was unnecessary, removed that.
I've only included the comparison on Me!Kombinationsfeld145 if it's numeric. That way, if it's Null or a zero-length string, it's not included in the comparison.

Perform a search for a user input date that falls between two dates

I have been trying to create a search that pulls up all of the records with a user specified date that falls between the start and end dates of that record. I also want to user to be able to pull it up by "Property" unless they leave it blank.
I am very new to VBA as in started yesterday and this is the latest version that I could come up with:
Private Sub Command4_Click()
Dim strFilter As String
strFilter = [Start_Date] <= Format(Me.RateDate, "Short Date") _
And [End_Date] >= Format(Me.RateDate, "Short Date")
If Not IsNull(Me.Property) Then
strFilter = strFilter & " AND [Num_Code] = '" & Me.Property & "'"
End If
DoCmd.OpenReport "rpt_RatesAll", acViewPreview, , strFilter
End Sub
The most recent error message returns: Run-time error '2465': Microsoft Access can't find '|1' referred to in your expression.
Any help would be very appreciated!
strFilter = "[Start_Date] <= #" Me.RateDate & "# And [End_Date] >= #" & Me.RateDate & "#"
Should do it.
What you had is being taken as a literal string and not using the values you desired.