Hi. Im struggling to work out how to form a suitable WHERE clause in a query.
What I wish to return are records where a date stored in a field called "started" falls into the current month.
E.g. if the current month is January 08 (which I believe it is!) all records where the month and year part of "Started" equals January and 2008 respectively.
In a separate query (and I think this is a little trickier), to return all records "started" during the previous month. E.g. (assuming Jan 08 again) where Month is December and Year is 2007.
Hi all, Please help me correct this criteria for a filter in my query. I want to show all records between todays date and the past 14 days. I've tried,
Below is the SQL I have on a Union Query. Each Query is based on a date range. The first Query date is Planned Immplementation Date. The second Query date is Revised Planned Implementation Date. If the Date in the Revised Planned Implementation Date is higher than the EndDateTxt Date Range I do not want the results to appear for that ECN. Any suggestions on how to accomplish this?
SELECT ECNBCNVIPtbl.[ECN Analyst], ECNBCNVIPtbl.[ECN Number], ECNDetailtbl.[ECN Description], ECNDetailtbl.[Planned Implementation Date], ECNDetailtbl.[Revised Planned Implementation Date], ECNBCNVIPtbl.[Serial Number Break Required?], ECNBCNVIPtbl.[Implementation Reporting Required?], ECNBCNVIPtbl.[Do Not Process] FROM ECNBCNVIPtbl INNER JOIN ECNDetailtbl ON ECNBCNVIPtbl.[ECNBCNVIP ID] = ECNDetailtbl.[ECNBCNVIP ID] WHERE (((ECNBCNVIPtbl.[ECN Number])<>"sample") AND ((ECNDetailtbl.[Planned Implementation Date]) Between [Forms]![EcnVisualStatusFRM]![StartDateTxt] And [Forms]![EcnVisualStatusFRM]![EndDateTxt]) AND ((ECNBCNVIPtbl.[Do Not Process])="yes")) ORDER BY ECNBCNVIPtbl.[ECN Analyst], ECNBCNVIPtbl.[ECN Number] UNION SELECT ECNBCNVIPtbl.[ECN Analyst], ECNBCNVIPtbl.[ECN Number], ECNDetailtbl.[ECN Description], ECNDetailtbl.[Planned Implementation Date], ECNDetailtbl.[Revised Planned Implementation Date], ECNBCNVIPtbl.[Serial Number Break Required?], ECNBCNVIPtbl.[Implementation Reporting Required?], ECNBCNVIPtbl.[Do Not Process] FROM ECNBCNVIPtbl INNER JOIN ECNDetailtbl ON ECNBCNVIPtbl.[ECNBCNVIP ID] = ECNDetailtbl.[ECNBCNVIP ID] WHERE (((ECNBCNVIPtbl.[ECN Number])<>"sample") AND ((ECNDetailtbl.[Revised Planned Implementation Date]) Between [Forms]![EcnVisualStatusFRM]![StartDateTxt] And [Forms]![EcnVisualStatusFRM]![EndDateTxt]) AND ((ECNBCNVIPtbl.[Do Not Process])="yes")) ORDER BY ECNBCNVIPtbl.[ECN Analyst], ECNBCNVIPtbl.[ECN Number];
I'm trying to hash two scripts I've found into 1 functioning filter, however I'm still relatively new to vba and can't figure out how to get this working.
I'm trying to use Allen Browne's Search Criteria:
with another snippete of code I found here:
Code: 'Purpose: This module illustrates how to create a search form, _ where the user can enter as many or few criteria as they wish, _ and results are shown one per line.
[Code]....
It's the date part I'm having trouble with, the rest of the search criteria work fine without the date, but I can't get it working when I try to modify and merge the date sections of each code.
Also I'm using a listbox for the "Yesterday";"Last 4 days";"Last 9 days" and not a combo box.
I have a query form that allows user to search by two criteria (in which, user can select "And" or "Or" clause for the two criteria). I also allow user to filter the results by date range. On the form, I have two command button, one will prompt the results in a query table, and the other will prompt a report.
Problem 1 I manage to prompt report with a date range (i.e. two unbound text boxes for start date and end date). But, I dont' manage to disable the filter if the date range is null. Below is the code for this report command button. Should I use a toggle button to make a select case?
Private Sub cmdReport_Click() Dim varItem As Variant Dim strDocName As String Dim str1MainCate As String Dim str2MainCate As String Dim str2MainCateCondition As String Dim strDate As String Dim strSQL As String Dim strFilter As String ' Build criteria string for 1st ComboBox For Each varItem In Me.fstMainCate.ItemsSelected str1MainCate = str1MainCate & ",'" & Me.fstMainCate.ItemData(varItem) & "'" Next varItem If Len(str1MainCate) = 0 Then str1MainCate = "Like '*'" Else str1MainCate = Right(str1MainCate, Len(str1MainCate) - 1) str1MainCate = "IN(" & str1MainCate & ")" End If ' Build criteria string for 2nd Combo Box For Each varItem In Me.SecMainCate.ItemsSelected str2MainCate = str2MainCate & ",'" & Me.SecMainCate.ItemData(varItem) & "'" Next varItem If Len(str2MainCate) = 0 Then str2MainCate = "Like '*'" Else str2MainCate = Right(str2MainCate, Len(str2MainCate) - 1) str2MainCate = "IN(" & str2MainCate & ")" End If ' Get 1toggle button condition If Me.optAnd2MainCate.Value = True Then str2MainCateCondition = " AND " Else str2MainCateCondition = " OR " End If ' Build SQL statement strSQL = " SELECT NewsClips.IssueDate, NewsClips.Title_Eng, NewsClips.Titile_Chi, NewsClips.NewsSource, NewsClips.[1CategoryMain], NewsClips.[1Sub-Category], NewsClips.[2CategoryMain], NewsClips.[2Sub-Category], NewsClips.hyperlink, NewsClips.FirstTwoPara, NewsClips.Notes, NewsClips.attachment FROM NewsClips " & _ "WHERE NewsClips.[1CategoryMain] " & str1MainCate & _ str2MainCateCondition & "NewsClips.[2CategoryMain] " & str2MainCate & ";" ' Build criteria string for Date If Not IsNull(Me![dateTo]) Then strDate = strDate & " NewsClips.IssueDate Between #" + Format(Me![datefrom], "mm/dd/yyyy") + "# AND #" & Format(Me![dateTo], "mm/dd/yyyy") & "#" 'Format(Me.dateTo, "mm/dd/yy") Else strDate = strDate & " NewsClips.IssueDate >= #" + Format(Me![datefrom], "mm/dd/yyyy") + "#" End If ' filter string strFilter = strDate ' Open report strDocName = "RptCateDateQry" DoCmd.OpenReport strDocName, acViewDesign, , strFilter With Reports(strDocName) .RecordSource = strSQL .Filter = strFilter .FilterOn = True End With DoCmd.Save acReport, strDocName DoCmd.OpenReport strDocName, acViewPreview Exit_cmdReport_Click: End Sub
Problem 2 I have no idea how to filter the query results by date range in the query table. Below is the code of the query table button, which do not offer the filter feature. Would really appreciate it if you can give me some advice.
Private Sub cmdOK_Click() On Error GoTo cmdOK_Click_Err Dim blnQueryExists As Boolean Dim cat As New ADOX.Catalog Dim cmd As New ADODB.Command Dim qry As ADOX.View Dim varItem As Variant Dim strDate As String Dim str1MainCate As String Dim str2MainCate As String Dim str1MainCateCondition As String Dim str2MainCateCondition As String Dim strSQL As String ' Check for the existence of the stored query blnQueryExists = False Set cat.ActiveConnection = CurrentProject.Connection For Each qry In cat.Views If qry.Name = "QryCateDateForm" Then blnQueryExists = True Exit For End If Next qry ' Create the query if it does not already exist If blnQueryExists = False Then cmd.CommandText = "SELECT NewsClips.IssueDate, NewsClips.Title_Eng, NewsClips.Titile_Chi, NewsClips.NewsSource, NewsClips.[1CategoryMain], NewsClips.[1Sub-Category], NewsClips.[2CategoryMain], NewsClips.[2Sub-Category], NewsClips.hyperlink, NewsClips.FirstTwoPara, NewsClips.Notes, NewsClips.attachment FROM NewsClips" cat.Views.Append "QryCateDateForm", cmd End If Application.RefreshDatabaseWindow ' Turn off screen updating DoCmd.Echo False ' Close the query if it is already open If SysCmd(acSysCmdGetObjectState, acQuery, "QryCateDateForm") = acObjStateOpen Then DoCmd.Close acQuery, "QryCateDateForm" End If ' Build criteria string for Date If Not IsNull(Me![dateTo]) Then strDate = strDate & " NewsClips.IssueDate Between #" + Format(Me![datefrom], "mm/dd/yyyy") + "# AND #" & Format(Me![dateTo], "mm/dd/yyyy") & "#" 'Format(Me.textStartDate, "mm/dd/yy") Else strDate = strDate & " NewsClips.IssueDate >= #" + Format(Me![datefrom], "mm/dd/yyyy") + "#" End If
' Build criteria string for 1MainCate For Each varItem In Me.fstMainCate.ItemsSelected str1MainCate = str1MainCate & ",'" & Me.fstMainCate.ItemData(varItem) & "'" Next varItem If Len(str1MainCate) = 0 Then str1MainCate = "Like '*'" Else str1MainCate = Right(str1MainCate, Len(str1MainCate) - 1) str1MainCate = "IN(" & str1MainCate & ")" End If ' Build criteria string for 2MainCate For Each varItem In Me.SecMainCate.ItemsSelected str2MainCate = str2MainCate & ",'" & Me.SecMainCate.ItemData(varItem) & "'" Next varItem If Len(str2MainCate) = 0 Then str2MainCate = "Like '*'" Else str2MainCate = Right(str2MainCate, Len(str2MainCate) - 1) str2MainCate = "IN(" & str2MainCate & ")" End If ' Get 1MainCate condition If Me.optAnd1MainCate.Value = True Then str1MainCateCondition = " AND " Else str1MainCateCondition = " OR " End If ' Get 2MainCate condition If Me.optAnd2MainCate.Value = True Then str2MainCateCondition = " AND " Else str2MainCateCondition = " OR " End If ' Build SQL statement strSQL = " SELECT NewsClips.IssueDate, NewsClips.Title_Eng, NewsClips.Titile_Chi, NewsClips.NewsSource, NewsClips.[1CategoryMain], NewsClips.[1Sub-Category], NewsClips.[2CategoryMain], NewsClips.[2Sub-Category], NewsClips.hyperlink, NewsClips.FirstTwoPara, NewsClips.Notes, NewsClips.attachment FROM NewsClips " & _ "WHERE NewsClips.[1CategoryMain] " & str1MainCate & _ str2MainCateCondition & "NewsClips.[2CategoryMain] " & str2MainCate & _ str1MainCateCondition & strDate & ";" ' Apply the SQL statement to the stored query cat.ActiveConnection = CurrentProject.Connection Set cmd = cat.Views("QryCateDateForm").Command cmd.CommandText = strSQL Set cat.Views("QryCateDateForm").Command = cmd Set cat = Nothing ' Open the Query DoCmd.OpenQuery "QryCateDateForm"
' If required the dialog can be closed at this point ' DoCmd.Close acForm, Me.Name ' Restore screen updating cmdOK_Click_Exit: DoCmd.Echo True Exit Sub cmdOK_Click_Err: MsgBox "An unexpected error has occurred." _ & vbCrLf & "Procedure: cmdOK_Click" _ & vbCrLf & "Error Number: " & Err.Number _ & vbCrLf & "Error Description:" & Err.Description _ , vbCritical, "Error" Resume cmdOK_Click_Exit End Sub
Sorry for posting this question again, as I thought it's better to make it a seperate posting, rather than a reply to my early post. Your advice will be greatly appreicated.
I have a table, tblDailyCalls, that contains agent_name, date, calls_ answered, and talk_time. Ideally on a form, the user will select an agent, enter the date range in txtStartDate and txtEndDate and a report opens to show what the total amount of calls and talk time is for that date range.
All I've managed so far is doing a simple expression on the report itself to sum the fields I want. But my method returns every date in the range. I would like to only display the total.
I've been trying with Totals in the query and crosstab queries but am not familiar with them.
I am having trouble getting a query or report to show only the most recent data.
We have salesmen that use a handheld data collector scanners to count inventory in stores. The scanner data is imported to a Access table. Each record line is one scanned item. I have a query with totals that counts the records and gives me a total count of each item at the store on that date.
I then need to filter the data to only show the most recent date. Using Max Date I get the most recent date but the count fields are showing totals for all dates. I am also getting the unique item from the earlier date in this query which I do not want.
Here is my data table: Inventory Scans from stores.
Scan Date Item Scanned location
1/1/2014 item123 Store ABC
1/1/2014 item123 Store ABC
......
Here is my Query with Totals that counts the item records:
Scan Date Item Scanned location (Item Scanned) count
1/1/2014 item123 Store ABC 2
1/1/2014 item 456 Store ABC 3
1/1/2014 item 789 Store ABC 1
2/1/2014 item123 Store ABC 2
2/1/2014 item 456 Store ABC 1
This is what I am trying to get - only the most recent date of counted items:
Scan Date Item Scanned location (Item Scanned) count
created a query (in Access 2010) that joins several linked tables (to an Oracle database). The query runs in about 20 seconds when I filter with a hard coded date (e.g., #12/31/2014#). The Oracle table column Im filtering on is defined as date/time.
When I attempt to change the hard coded value to a soft coded value (e.g., Forms![Form1]![Latest_Extract_Date]), the query runs over 5 minutes. In this case, the form field has the exact same value (12/31/2014).
Ive encountered similar issues using Access 2000, 2003, etc. This is quite frustrating. Does Access interpret #date value# is a special way? Is there a way to trick Access into the thinking a soft coded date is a hard coded date?
I have a date field that I need to filter. Individuals are on a committee list that typically has a begin and end date. The end_date field can be populated with a specific end date such as 09/02/2005 or it may be left blank which indicates that it is an open term with no specific end date.
Is it possible to query all indiviiduals who have either an end_date >=09/02/2005 or IsNull (ie the field is empty) which would indicate an open term in the required report.
I have a problem. I want to apply a filter on my form.When I filter on the combobox it works fine. But when I filter on the date the form jumps into the error handler. Below is the code that I use for my filter:
Sub ApplyFilter() Dim sFilter As String sFilter = ""
'Filter on date If Me.txtFilterDatumMelding = "" Or IsNull(Me.txtFilterDatumMelding) Or Me.txtFilterDatumMelding = 0 Then If sFilter = "" Then sFilter = "" Else sFilter = sFilter & "" End If Else If sFilter = "" Then sFilter = "[DatumMelding] = " & "'" & txtFilterDatumMelding & "'"
Else sFilter = sFilter & " and [DatumMelding] = " & "'" & txtFilterDatumMelding & "'" End If End If
'Filter on combobox If Me.cboFilterCallMelder = "" Or IsNull(Me.cboFilterCallMelder) Or Me.cboFilterCallMelder = 0 Then If sFilter = "" Then sFilter = "" Else sFilter = sFilter & "" End If Else If sFilter = "" Then sFilter = "[CallMelder_ID] = " & cboFilterCallMelder Else sFilter = sFilter & " And [CallMelder_ID] = " & cboFilterCallMelder End If End If
If Len(sFilter) > 0 Then Me.Filter = sFilter Me.FilterOn = True Else Me.Filter = "" Me.FilterOn = False End If End Sub
I hope that you gus can help me. Could it be that the filter is a string and the date filter field is a date??????
I have a date field on one of my continous forms. When I try to apply a date between filter, it gives me an error message saying: "Enter a valid date" I choose the boundary dates from that little calendar. The date after and date before filters are working without problem. The date selection filter is working alike. Only the date between filter complains.It had worked before.
Hello All I have been having problems with the filter that I created for a report. I created a form that filters the form by a date and the name of the class. The code looks like this:
If Me("Filter1") <> "" Then strSQL = strSQL & "[" & Me("Filter1").Tag & "] " & " = " & Me("Filter1") & " And " ElseIf Me("Filter2") <> "" Then strSQL = strSQL & "[" & Me("Filter2").Tag & "] " & " = " & "#" & Me("Filter2") & "#" & "And """ End If
The problem that I am having is that this works fine on my computer at home but when I try it on another computer i get error messages for the date or the report appears empty showing me that there are no classes on the specified day that I have chosen. I have no idea why on some computers the date filter works an on others it doest. I could possibly be a problem with the date format on each computer and if this is the case how do I get around this problem. Thanks in adavance for your help. Amr
I have 2 tables. One for customer details. With customer ID,Company Name, Customer Name and Address.The other for sales details with Order ID, Customer ID, salesprice and sales date.I would like to have a list of customer ID, Company name,total sales last year(sum of salesprice for 2013), Total sales till date(Jan 2014 til now). All should include customers details who have not made any sale as well.
Hello everyone. I am having trouble with applying filter on 2 date fields in form. I have succeeded by applying fiter on report by Date, but I just can't seem to work it out on Form! I believe there has to be something to do with the DoCmd code.
I have a main form (frmRate) which has fields (txtDate & txtValidity) that contain dates. I have a subform called (frmSearchRateCustomer) which has 4 fields (txtStartDate, txtEndDate, txtStartDate2, txtEndDate2) where the data range should be filled in as search criteria. I have also an additional combobox (cboCustomer) where user can select customer as a search criteria as well. I have a button that has to do the Search in frmRate. The combobox criteria works very well, just the date filtering is not working. I am pasting my code as follows:
Code:Private Sub cmdSearch_Click()On Error GoTo Err_cmdSearch_ClickDim strCustomer As StringDim strFilter As StringDim stDocName As StringDim strForm As StringDim strField As String 'Name of your Order field.Dim strWhere As String 'Where condition for OpenReport.Const conDateFormat = "#dd/mm/yyyy#"Dim strField2 As String 'Name of your Arrival field.Dim strWhere2 As String 'Where condition for OpenReport.Const conDateFormat2 = "#dd/mm/yyyy#" strForm = "frmRate" strField = "txtDate" strField2 = "txtValidity" ' For the Order Date Search Criteria Date range If IsNull(Me.txtStartDate) Then If Not IsNull(Me.txtEndDate) Then 'End date, but no start. strWhere = strField & " < " & Format(Me.txtEndDate, conDateFormat) End If Else If IsNull(Me.txtEndDate) Then 'Start date, but no End. strWhere = strField & " > " & Format(Me.txtStartDate, conDateFormat) Else 'Both start and end dates. strWhere = strField & " Between " & Format(Me.txtStartDate, conDateFormat) & " And " & Format(Me.txtEndDate, conDateFormat) End If End If ' Debug.Print strWhere 'For debugging purposes only. ' For the Arrival Date Search Criteria Date rangeIf IsNull(Me.txtStartDate2) Then If Not IsNull(Me.txtEndDate2) Then 'End date, but no start. strWhere2 = strField2 & " < " & Format(Me.txtEndDate2, conDateFormat2) End If Else If IsNull(Me.txtEndDate2) Then 'Start date, but no End. strWhere2 = strField2 & " > " & Format(Me.txtStartDate2, conDateFormat2) Else 'Both start and end dates. strWhere2 = strField2 & " Between " & Format(Me.txtStartDate2, conDateFormat2) & " And " & Format(Me.txtEndDate2, conDateFormat2) End If End If If IsNull(Me.cboCustomer.Value) Then strCustomer = "Like '*'" Else strCustomer = "='" & Me.cboCustomer.Value & "'" End If ' Build filter string strFilter = "[txtCustomerName] " & strCustomer DoCmd.OpenForm strForm, acNormal, , strWhere ' for date DoCmd.OpenForm strForm, acNormal, , strWhere2 ' for date stDocName = "frmRate" DoCmd.OpenForm stDocName, acNormal ' Apply filter to report With Forms![frmRate] .Filter = strFilter .FilterOn = True End WithExit_cmdSearch_Click: Exit SubErr_cmdSearch_Click: MsgBox Err.Description Resume Exit_cmdSearch_Click End Sub
I have used the same code as I have used for my Report, just changed few things like rpt --> frm etc...
I have form that user can filter the records and generate a report but I have difficult trying filter null date.
If I have check box called filter null if it has a tick in I would like it only show records that have no value (is null) in field "date start" but if unticked I would like it to only show records with a date in field "date start" ...
I have a query that pulls scores for this month only for each class member. Problem is, I only need a count of these scores (per person) and because the date is in the query, it doesn't group the scores together and count them as one. i.e. it sees score 1 and score 2 as separate because they have different dates so they won't count together.
The only purpose of date in this query is to filter out only this month's dates. Is there some advanced query expression something or other that will tell it to leave date out of the count and only use it to filter?
I have a cross-tab query that is filtered on user-defined date range using a where condition of:>[Forms]![Reports_Menu]![txtFilterDateFrom] AND <[Forms]![Reports_Menu]![txtFilterDateTo]
the Query functions perfectly. The report based on that query, however, only functions with a DateFrom that is before 1/28/14 and a DateTo that is after 2/27/14. If I enter a date in either field that is outside of those ranges, I get the following error:The Microsoft Access database engine does not recognize " as a valid field name or expression.I have defined both of my Query Parameters as Date/Time in the Xtab query design.
I am trying to set a date filter that filters between two dates (Start/End) after I have selected filters from other combo boxes. This is what I Have so far and is a bit of a mess.
I have two text boxes - txtStartDate and txtEndDate
If Nz(Me.txtStartDate.Text) = "" Then Me.Form.Filter = "" Me.FilterOn = False ElseIf Me.cboCity.ListIndex <> -1 Then Me.Form.Filter = Me.Form.Filter = "NextCallDate
[Code] .....
So the idea is I will filter all records by my name then by status then by city and then by start date and end date.
I am trying to use VBA to create a filter by date range. the user inputs 2 dates and the database filters all records by dates in between the 2 dates,
Code below
Dim var_CustDate1 As String Dim var_CustDate2 As String var_CustDate1 = InputBox("Please enter start date in format DD/MM/YYYY", "Enter Date", Date) If Not IsDate(var_CustDate1) Then MsgBox ("not a valid Date")
[Code] ....
I've tried every combination of format for the dates but this is the closest ive got it to work,
if i enter dates 01/09/2013 and 12/09/2013 the filter works for the days in the month but also displays previous years, but if i change the dates to 01/09/2013 and 13/09/2013 it starts displaying all dates for all years in the months September, October, November and December.
I have a form in datasheet view, with a date column. When I filter for last week it gives me all the entries for that week, but when I try and filter on a single day it only gives me 1 entry even though I know there are several entries for that day. Why would it be doing this? It also is the same when I go directly to the table and try to filter. I know the underlying data that was imported is a full date and time stamp, which I'm displaying as just the date portion, but surely it should show me all that are on a particular date, not just 1?
Just checked to see whether it would give me "yesterday", and while it seem to be able to when I checked 2 days ago, now it's not even giving me those.