access vba docmd.applyfilter for dates - vba

I am running a code based on a week number to filter for work order dates that match the week column and are greater than the WODate Column. My code gives me a "Syntax" Error for the second applyfilter command. Is there a way to have both filters apply at once?
my code:
Private Sub Weeks_AfterUpdate()
DoCmd.ApplyFilter , "[Week] = '" & Me.Weeks & "'"
DoCmd.ApplyFilter , "[WODate] >= #" & Format(wodate, "mm/dd/yyy") & "# and [FYDate] <= #" & Format(todate, "mm/dd/yyyy") & "#"
Me.FilterOn = True
End Sub

You miss a y, and the date separators should be escaped:
DoCmd.ApplyFilter , "[WODate] >= #" & Format(wodate, "mm\/dd\/yyyy") & "# and [FYDate] <= #" & Format(todate, "mm\/dd\/yyyy") & "#"

Related

MS Access – Filtering #Multiple Criteria while Ignoring Null Values

Essentially, I’m trying to filter a form on three conditions.. without one filter overwriting the previous..
Is the non conformance open or closed – Source = option buttons
Between a date range – Source = text input field
Which operations are involved Source = Combobox
I decided to use If statements to cover the options buttons. If option xx true then
The filters work fine until there is a blank value in either in the combobox or the date input text field..
Then an error is returned saying Extra) in query expression
If Me.Option54 = True Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([OpDescription]=" & ([Combo58]) & ")And ([NCR Clsd?]=False)"
End If
If Me.Option56 = True Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([OpDescription]=" & ([Combo58]) & ")And ([NCR Clsd?]=True)"
End If
If Me.Option54 = False And Me.Option56 = False Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([OpDescription]=" & ([Combo58]) & ")"
End If
Me.FilterOn = True
Is there a way of ignoring the filter if the source field is blank?
I can’t seem to get the IsNull command to actually do anything
Either have more conditional statements to not use filter if criteria is not provided or handle null to substitute with another value.
Nz([Date], #1/1/1900#)
Format(Nz([TxtDtStrt], #1/1/1900#))
Format(Nz([TxtDtEnd], #12/31/2099#))
The end solution I found was to use IsNull and Not IsNull as Boolean combined with IF And Covering all the possible combinations in a public sub
Then call up the public sub on click of each of the filters.. Its working, just wondering if there's a more elegant way of doing it...
Public Sub NCRDtFltr()
If IsNull(Me.Combo58.Value) And Me.Option54 = True Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([NCR Clsd?]=False)"
End If
If IsNull(Me.Combo58.Value) And Me.Option56 = True Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([NCR Clsd?]=True)"
End If
If IsNull(Me.Combo58.Value) And Me.Option54 = False And Me.Option56 = False Then
Filter = "[Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#"
End If
If Not IsNull(Me.Combo58.Value) And Me.Option54 = True Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([OpDescription]=" & ([Combo58]) & ")And ([NCR Clsd?]=False)"
End If
If Not IsNull(Me.Combo58.Value) And Me.Option56 = True Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([OpDescription]=" & ([Combo58]) & ")And ([NCR Clsd?]=True)"
End If
If Not IsNull(Me.Combo58.Value) And Me.Option54 = False And Me.Option56 = False Then
Filter = "([Date] Between #" & Format([TxtDtStrt], "yyyy\/mm\/dd") & "# And #" & Format([TxtDtEnd], "yyyy\/mm\/dd") & "#) And ([OpDescription]=" & ([Combo58]) & ")"
End If
End Sub

DoCmd.AppltFilter with multiple criteria overwriting

I have an MS.Access form with 2 filters, one filters between dates using text boxes
DoCmd.ApplyFilter , "[Start Date] Between #" & Format([AVstrtdt], "yyyy\/mm\/dd") & "# And #" & Format([AVEnDt], "yyyy\/mm\/dd") & "#"
And the other filters by department using a combo box..
DoCmd.ApplyFilter "", "[OpDescription]=" & ([CmbBxDepart]) & ""
Both work perfectly on there own but they overwrite each other IE the date range overwrites the department filter or the department filter overwrites the date filter so I've been trying to stick them together as one filter using the Or function
But keep getting a type mismatch error...
Private Sub ApplyDtFilt_Click()
DoCmd.ApplyFilter , "[Start Date] Between #" & Format([AVstrtdt], "yyyy\/mm\/dd") & "# And #" & Format([AVEnDt], "yyyy\/mm\/dd") & "#" Or "", "[OpDescription]=" & ([CmbBxDepart]) & ""
End Sub
I've searched and found similar posts but I cant seem to find what's wrong... any ideas?
looks ok to me..
Thanks..
Use one expression - as a WHERE clause in SQL:
DoCmd.ApplyFilter , "([Start Date] Between #" & Format([AVstrtdt], "yyyy\/mm\/dd") & "# And #" & Format([AVEnDt], "yyyy\/mm\/dd") & "#) Or ([OpDescription]=" & [CmbBxDepart] & ")"
Alternatively, set the Filter property of the form:
Dim Filter As String
Filter = "([Start Date] Between #" & Format([AVstrtdt], "yyyy\/mm\/dd") & "# And #" & Format([AVEnDt], "yyyy\/mm\/dd") & "#) Or ([OpDescription]=" & [CmbBxDepart] & ")"
Me.Filter = Filter
Me.FilterOn = True

Access query returning dates out of range input

I have constructed a query to select all dates between a date range which is inputted by the user.
When the user selects the date before todays date, the current date or a day after the current date it brings back dates not within the date range.
Please see my examples below:
Before todays date returning incorrect dates in range:
Selecting date 3 days after current date query query perfectly:
Option Compare Database
Private Sub Command121_Click()
Dim strCriteria, task As String
strCriteria = "([PromisedDeliveryDate] >= #" & Me.DateFrom & "# And [PromisedDeliveryDate] <= #" & Me.DateTo & "#)"
task = "select * from SageOrderLine_Live where (" & strCriteria & ")"
DoCmd.ApplyFilter task
End Sub
EDIT: How would I add to query by the customeraccountnumber as well? I have tried to add it on but it's bringing up an error:
DoCmd.ApplyFilter _
"select * from SageOrderLine_Live where " & _
"[PromisedDeliveryDate] >= " & Format(Me.DateFrom, "\#mm\/dd\/yyyy\#") & " and " & _
"[PromisedDeliveryDate] <= " & Format(Me.DateTo, "\#mm\/dd\/yyyy\#") & " and " & _
"[CustomerAccountNumber] = " & Me.CustomerAccount & ""
The "Enter parameter box" is appearing when selecting search:
When referencing date values within a SQL statement, you will need to ensure that such dates adhere to the expected format of:
#mm/dd/yyyy#
Else, a date such as 08/11/2019 (8th November 2019) will be interpreted within the SQL as 11/08/2019 (11th August 2019), yielding the results that you are witnessing.
You can achieve this using the Format function:
Format(Me.DateFrom, "\#mm\/dd\/yyyy\#")
Here, the additional backslashes \ preceding the octothorpes & forward slashes act as Escape Characters, forcing MS Access to display the next character as a literal.
For example:
?Format(Date(), "\#mm\/dd\/yyyy\#")
#11/09/2019#
In your code, this might be written:
Private Sub Command121_Click()
DoCmd.ApplyFilter _
"select * from SageOrderLine_Live where " & _
"[PromisedDeliveryDate] >= " & Format(Me.DateFrom, "\#mm\/dd\/yyyy\#") & " and " & _
"[PromisedDeliveryDate] <= " & Format(Me.DateTo, "\#mm\/dd\/yyyy\#")
End Sub
Alternatively, you could reference the form controls directly within the SQL statement, in which case, no formatting is required:
Private Sub Command121_Click()
DoCmd.ApplyFilter _
"select * from SageOrderLine_Live where " & _
"[PromisedDeliveryDate] >= [Forms]![YourFormName]!DateFrom and " & _
"[PromisedDeliveryDate] <= [Forms]![YourFormName]!DateTo "
End Sub
(Updating YourFormName to the name of your form, of course).
EDIT: If you wish to add the CustomerAccountNumber to the filter, since this is a text field, you'll need to enclose the value with either single or double quotes, e.g. using single quotes:
DoCmd.ApplyFilter _
"select * from SageOrderLine_Live where " & _
"[PromisedDeliveryDate] >= " & Format(Me.DateFrom, "\#mm\/dd\/yyyy\#") & " and " & _
"[PromisedDeliveryDate] <= " & Format(Me.DateTo, "\#mm\/dd\/yyyy\#") & " and " & _
"[CustomerAccountNumber] = '" & Me.CustomerAccount & "'"
Or double-quotes:
DoCmd.ApplyFilter _
"select * from SageOrderLine_Live where " & _
"[PromisedDeliveryDate] >= " & Format(Me.DateFrom, "\#mm\/dd\/yyyy\#") & " and " & _
"[PromisedDeliveryDate] <= " & Format(Me.DateTo, "\#mm\/dd\/yyyy\#") & " and " & _
"[CustomerAccountNumber] = """ & Me.CustomerAccount & """"

Syntax error 3075 in calling from another procedure

I m trying to filter records between two dates on a split form through Search button which calls the following code
Sub search()
Dim strCriteria ,taSk As String
Me.Refresh
If IsNull(Me.txtDiaryDatefrom) Or IsNull(Me.txtDiaryDateTo)
Then
MsgBox "Please Enter Date Range", vbInformation, "Date Range Required"
Me.txtDiaryDatefrom.SetFocus
Else
strCriteria = "([DiaryDate]>= # " & Me.txtDiaryDatefrom & " # And [DiaryDate] <= #" & Me.txtDiaryDateTo & " # )"
taSk = "SELECT * from tblDiary where ( " & strCriteria & ")"
DoCmd.ApplyFilter , taSk
End If
End Sub
Upon execution it gives the Run-time error 3075 reading
syntax error in query expression 'Select * from tblDiary .....
Could anyone figure out what is needed to fix it ??
You need to remove the spaces between the hashes.
"([DiaryDate]>= #" & Me.txtDiaryDatefrom & "# And [DiaryDate] <= #" & Me.txtDiaryDateTo & "# )"
I would change the query so that the end [DiaryDate] < Date + 1. This will ensure that records that have date/time values will not accidentally get filtered out.
"([DiaryDate]>= #" & DateValue(Me.txtDiaryDatefrom) & "# And [DiaryDate] < #" & DateValue(Me.txtDiaryDateTo) + 1 & "# )"

MS Access 2010 Date range results not as desired

I am using this command to open a report
DoCmd.OpenReport "fullreport_rpt", acViewReport, , _
"[Assignment] ='" & lngMyEmpID & "' AND [Start] BETWEEN DateValue(#" & Me.DateRange1_txt & "#) AND DateValue(#" & Me.DateRange2_txt & "#)", _
acDialog, OpenArgs:=Me.reporting_combo & " For " & Me.rMonthly_txt & Me.DateRange1_txt & "-" & Me.DateRange2_txt
[Start] is DateValue([StartTime]) and StartTime is a DateTime field.
I want to generate a report from 01/05/2015 to 20/05/2015 but the report generated includes rows from April 2015;
image link is http://s29.postimg.org/i066yqbl3/scrshot.png
Not sure whats wrong with it.
I would appreciate any help.
I would do this in two ways. First is to convert the date value to Double as that is how JET interprets dates.
DoCmd.OpenReport "fullreport_rpt", acViewReport, , _
"[Assignment] = '" & lngMyEmpID & "' AND [Start] BETWEEN " & CDbl(Me.DateRange1_txt) & " AND " & CDbl(Me.DateRange2_txt) & _
, acDialog, OpenArgs:=Me.reporting_combo & " For " & Me.rMonthly_txt & Me.DateRange1_txt & "-" & Me.DateRange2_txt
Using CDbl is a very valid operation on Dates that come from a Date Control. If anything else, you need to use the right formatting, Access has a problem with International Date Formats, so try this.
DoCmd.OpenReport "fullreport_rpt", acViewReport, , _
"[Assignment] = '" & lngMyEmpID & "' AND [Start] BETWEEN " & Format(Me.DateRange1_txt, "\#mm\/dd\/yyyy\#") & " AND " & Format(Me.DateRange2_txt, "\#mm\/dd\/yyyy\#") & _
, acDialog, OpenArgs:=Me.reporting_combo & " For " & Me.rMonthly_txt & Me.DateRange1_txt & "-" & Me.DateRange2_txt
Use DateValue correctly:
BETWEEN DateValue(" & Me.DateRange1_txt & ") AND DateValue(" & Me.DateRange2_txt & ")", _
DateValue("1-5-2015") returns 2015-05-01 while
DateValue(#1-5-2015#) using your settings returns 2015-01-05 which will include dates of April.