Using Access 2013
I have table in Access where Column "DueDate" is defined as "Date/Time"
System default date format is dd/mm/yyyy
Want to change List's Row Source through VBA, based on value in a textbox "txtDueDateUpto".
So, that I get all the task where the dude date is less than equal to the date entered by user in "txtDueDateUpto"
mySql = "SELECT TaskTbl.TaskID "
& " FROM TaskTbl " _
& " WHERE " _
& " DueDate is not Null and " _
& " Format (DueDate," & """dd/mm/yyyy""" & ") <= " & Format(CDate(Me.txtDueDateUpto.Value), "dd/mm/yyyy") _
Me.listTask.RowSource = mySql
I have 3 Tasks for testing purpose.
where DueDate is saved as
TaskID DueDate
1 25-17-2015
2 01-07-2015
3 29-06-2015
and, value in txtDueDateUpto is 06-07-2015
txtDueDateUpto format property is set to "Short Date"
I was expecting taskID 2,3 to be returned, with the given SQL, but I am getting taskID 2
I sat all night, trying many permutations and combinations, but cannot get what is that, I am doing wrong.
newbie for Access VBA. Pls help, thanks in advance.
Always handle dates as dates, not strings, no exceptions.
If your textbox has been assigned a date/time format, you don't even have to use CDate or DateValue because Access then reads that value as of data type Date.
However, you must concatenate the value with the SQL code as a properly formatted string expression of the date value:
mySql = "SELECT TaskTbl.TaskID "
& " FROM TaskTbl " _
& " WHERE " _
& " DueDate is not Null and " _
& " DueDate <= #" & Format(Me!txtDueDateUpto.Value, "yyyy\/mm\/dd") & "#"
If you are going to format the dates as strings -- and compare them -- then always use YYYY-MM-DD format:
& " Format (DueDate," & """yyyy-mm-dd""" & ") <= " & Format(CDate(Me.txtDueDateUpto.Value), "yyyy-mm-dd") _
However, I'm pretty sure that MS Access can just do date comparisons without the conversion:
DueDate <= CDate(Me.txtDueDateUpto.Value)
Related
I have a some VBA code that takes in a date from and date to fields from two date picker values in textboxes.
However, when searching between the two dates I receive the 'Enter Parameter Value' prompt asking for input.
Dim SQLAllReject As String
Dim strDateFrom As String
Dim strDateTo As String
strDateFrom = Format(txtDate.Value, "mm/dd/yyyy")
strDateTo = Format(txtDateTo.Value, "mm/dd/yyyy")
When running directly in the Query Design wizard, searching between dates works fine:
WHERE (((XXXXXXXXXXXXXXXXXXXX.Date) Between #10/1/2021# And #10/27/2021#))
What is wrong here? How can I force Access/VBA to take the dates I've specified and search between them and NOT display the 'Enter Parameter Value' prompt?
When I debug the query and step through, it gives exactly the same dates as running it in Query Design view.
FYI if I use one date and the LIKE operator, this works fine.
SQLAllReject = "SELECT dbo_vw_busobj_file_rejections_load_access_temp5_copy.HashKey AS [ID], dbo_vw_busobj_file_rejections_load_access_temp5_copy.Reject_Date AS [Date], " & _
"FROM dbo_vw_busobj_file_rejections_load_access_temp5_copy " & _
"WHERE (((dbo_busobj_file_rejections_load_access_temp5_copy.Reject_Date) Between #" & strDateFrom & "# And #" & strDateTo & "#)) " & _
"ORDER BY dbo_vw_busobj_file_rejections_load_access_temp5_copy.Reject_Date DESC;"
Ok I somehow fixed it... the query string was trying to select from a view rather than the table and for some odd reason it was causing the parameter prompt.
I have changed dbo_vw_busobj_file_rejections_load_access_temp5_copy to dbo_busobj_file_rejections_load_access_temp5_copy table and managed to get it to work after doing some string to date conversion when selecting dates from the textboxes.
I have a form with one text box, two combo boxes (dropdowns), and two text boxes with input masks for mm/dd/yyyy 99/99/0000;0;_
I am attempting to use all of these fields as filters for a subform.
I have the controls set to fire after update and run a sub that builds a SELECT * INTO sql string for a temp Table that is then sourceobject'ed back to the subform.
In code I have for each control I have code building a snippet of the Where statement for the final sql. the snippet grows as needed, is labeled "Filter"
Some other non-Finished Code....
If Not IsNull(txtDateFrom) Then
If i > 1 Then Filter = Filter & " AND " & "([Date_LastSaved] >= " & Me.txtDateFrom & ")" & " And " & "([Date_LastSaved] <= " & Me.txtDateTo & ")"
End If
Dim sql As String
sql = "SELECT * INTO tmpTable FROM tblReview"
If Not IsNull(Filter) Then
sql = sql & " WHERE " & Filter
End If
My issue and question is that I am testing this one specific situation where there will be Filter = Filter & " AND " & "DATE_STUFF"
where the final sql looks like...
SELECT * INTO tmpTable FROM tblReview WHERE ReviewStatus = 'Quoted' AND ([Date_LastSaved] >= 09/12/2018) And ([Date_LastSaved] <= 10/16/2018)
which should have some result with the test data. Yet, the tmpTable is empty.
This is only happening when I apply a date range criteria.
I tried BETWEEN but could not nail down the syntax.
Any insight is greatly appreciated, Thanks!
UPDATE:
Answer:
If i > 1 Then Filter = Filter & " AND " & "([Date_LastSaved] >= #" & Me.txtDateFrom & "#)" & " And " & "([Date_LastSaved] <= #" & Me.txtDateTo & "#)"
If you want to use dates then they must be quoted. If you just say 10/16/2018 then you are dividing the integer 10 by 16 by 2018 which will yield an integer zero. It will then convert the dates to an integer to do the compare, which will yield a much bigger number, and thus you get no rows.
Any date testing should always be done using date types rather than strings. I think in msaccess you can surround it in #, but not sure. Just research this.
I have a query in Access named 'Laufzettel' and it looks in this way. The dates are in dd.mm.yyyy format
Antragsnummer Eingang esigniert Ausgang Anlage Policierung
111 2.10.2016 2.10.2016 3.10.2016 3.10.2016
222 3.10.2016 3.10.2016 3.10.2016 4.10.2016
333 5.10.2016 6.10.2016 7.10.2016 7.10.2016
I am creating a form named 'overview' with two Textboxes named StartDate and EndDate and search button. What I need is When I click the search button by giving in two dates it should retrieve me the related records from the above query with all the fields in form of a report.
The StartDate and EndDate are related to the Eingang field of the query. SO when I enter 2.10.2016 as StartDate and 5.10.2016 as EndDate it must produce the above query. To achieve this I have started to create a query using the form fields which in turn can produce me the required report when I click the search button. So I am trying with the code
SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung
FROM Laufzettel
WHERE Lauzettel.Eingang BETWEEN [Forms]![overview]![StartDate] and [Forms]![overview]![EndDate];
For which I get an error Access cannot recognise [Forms]![overview]![StartDate] and [Forms]![overview]![EndDate] as a valid field name or expression
How can I achieve the above? Can someone help me?
EDIT : Here is my working Query now. Although It doesn't give me the records from EndDate values.
PARAMETERS [Forms]![overview]![start] DateTime, [Forms]![overview]![end] DateTime;
SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.AEingangDatenstromZWorkflow, Laufzettel.BEingangesigniertDokumentZWorkflow, Laufzettel.CAusgangDatenstromZWorkflow, Laufzettel.DAnlageSchwebeVSL, Laufzettel.EPolicierungVSL
FROM Laufzettel
WHERE (((Laufzettel.AEingangDatenstromZWorkflow) Between [Forms]! [overview]![start] And [Forms]![overview]![end]));
Can someone tell me where am I going Wrong?
If build in code (VBA), try with:
SQL = "SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung " & _
"FROM Laufzettel " & _
"WHERE Lauzettel.Eingang BETWEEN #" & Format([Forms]![overview]![StartDate], "yyyy\/mm\/dd") & "# AND #" & Format([Forms]![overview]![EndDate], "yyyy\/mm\/dd") & "#"
If the "date" field really is text, apply DateValue:
SQL = "SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung " & _
"FROM Laufzettel " & _
"WHERE DateValue(Lauzettel.Eingang) BETWEEN #" & Format([Forms]![overview]![StartDate], "yyyy\/mm\/dd") & "# AND #" & Format([Forms]![overview]![EndDate], "yyyy\/mm\/dd") & "#"
I have a table in my ms access database which has 1 column that says downdate and 1 column that says downtime also 1 column that says update and 1 columns that says uptime. I have a created a query that concatenated the downdate and downtime, it was created in my query as Down: [downdate] & " # " & [downtime] and Up: [update] & "#" & [uptime].
What I wanted to do is to make another query where I can find all the records between the date from my first textbox to my second textbox with out losing the query that I have created to show up in my listbox. I really need your help everyone thanks in advance.
If you wish to filter for a date/time period, you can't use that weird string format. You must ad the date and time:
"Where #" & Format(date1, "yyyy\/mm\/dd") & "# Between [downdate] + [downtime] And #" & Format(date2, "yyyy\/mm\/dd") & "# Between [update] + [uptime]"
I'm trying to use the below code to bring back all records that are not older than 90 days from a table:
Set PlatinumList = db.OpenRecordset( _
"SELECT FORMATTED_CTN FROM CTN_LIST " _
& "WHERE ((Status='Available') AND (Category='Platinum')) " _
& "AND (In_Offer_List = True) " _
& "AND (DATEDIFF(day, Created_DT, current_date()< 90);", _
dbOpenSnapshot, dbReadOnly)
I keep getting a syntax error in query expression error when I try and run the code.
I think I'm formatting the date section of the code incorrectly.
The date token for day is "d" rather than day and your missing a closing )
.. AND (DATEDIFF("d", Created_DT, current_date()) < 90);"
(You also need current_date() as a vba func)