So i have the following query:
DoCmd.RunSQL "delete * from [TABLE NAME] where month = '" & Format(PrevMonth, "yyyy-mm-dd") & "'"
month is a Text field.
Lets say PrevMonth = August 2010 so instead of delete the rows where the date is august 2010, i want it to delete august 2010 and all after that? so september 2010, october 2010, and so on.
thanks.
You can use the CDate() function to cast your text date to date/time data type.
Then:
DELETE FROM [TABLE NAME]
WHERE CDate([month]) >= #2010/08/01#;
I enclosed the field name in brackets because Month() is an Access VBA function ... the brackets let Access know to treat month as a field rather than the function. If it were my database, I would rename the field.
First create a query based on [TABLE Name] where you convert month from text to date
SELECT *,CDate("1 " & [Month]) AS DateDate
FROM Table Name;
Now you can use the dates as normal
DELETE Query1.DateDate
FROM Query1
WHERE (((Query1.DateDate) Between #1/1/2010# And #12/31/2010#));
Hope this helps
Related
I currently have a table in my database keeping track of a start date and end date for a service. I need to compare a date entered by the user to see if it is between the start and end dates. My issue right now is that in the table, access stores that start date as DD/MM/YYYY, the textbox in my form that the user puts their date in is formated as DD/MM/YYYY. However, once I hit VBA and run an SQL query, it reads the date as MM/DD/YYYY.
My query is:
SELECT * FROM table WHERE #09/01/2018# BETWEEN startDate AND endDate
My test entry is:
table:
startDate endDate service
08/01/2018 02/02/2018 ABC
This should return this entry, however as it reads it as MM/DD/YYYY it does not. If I enter #13/01/2018# it returns the entry as it detects that 13 is the date and cannot be a month. How could I correct this so that it takes 09/01/2018 and returns the entry as well?
If this is an VBA query then your observations are correct. In the Query designer these will work as expected.
Review Allen Browne's excellent explanation here; http://allenbrowne.com/ser-36.html
with solutions as well.
Your query should use either the "reverse" US sequence or the ISO sequence:
SELECT * FROM table WHERE #2018/01/09# BETWEEN startDate AND endDate
or, if you build it from code:
SearchDate = SomeDateValue
SQL = "SELECT * FROM table WHERE #" & Format(SearchDate, "yyyy\/mm\/dd") & "# BETWEEN startDate AND endDate"
or, if you always search for today:
SQL = "SELECT * FROM table WHERE Date() BETWEEN startDate AND endDate"
I'm using the code below to find the most recent values from a record, so this instance gets the [Process No] where the [Time] field equals my DMax value. For some bizarre reason it's suddenly stopped working and only returns null now.
This same code worked last week, but since an entry was made over the weekend (by someone else) all similar DLookup statements only return null values. The same code still works in my other forms and I've since deleted what I though was the dodgy record but no luck.
DMaxTime = DMax("[Time]", "Daily Oven/Product Record")
'Find last Process No entered
lastProcNoLookup = DLookup("[Process No]", "Daily Oven/Product Record", "[Time] = #" & DMaxTime & "#")
*DMaxTime and lastProcNoLookup are both variants
When you add the Date/Time value for the DLookup Criteria option, format the date part of that value as yyyy-m-d.
'Find last Process No entered
Dim strCriteria As String
strCriteria = "[Time] = " & Format(DMaxTime, "\#yyyy-m-d hh:nn:ss\#")
Debug.Print strCriteria
lastProcNoLookup = DLookup("[Process No]", "Daily Oven/Product Record", strCriteria)
Explanation:
When you give the db engine a date string in dd/mm/yyyy format, Access can interpret it correctly if the string does not represent a valid date in mm/dd/yyyy format.
For example, since there is no month 25, the db engine knows #25-1-2015# must mean Jan, 1 2015. So this query interprets the value as you intend.
SELECT Year(#25-1-2015#) AS [Year], Format(#25-1-2015#, 'mmm d, yyyy') AS Formatted_date
Year Formatted_date
---- --------------
2015 Jan 25, 2015
However #3-2-2015# will be interpreted as Mar 2 instead of Feb 3, 2015:
SELECT Year(#3-2-2015#) AS [Year], Format(#3-2-2015#, 'mmm d, yyyy') AS Formatted_date
Year Formatted_date
---- --------------
2015 Mar 2, 2015
You can avoid such confusion by always giving the db engine dates in yyyy-m-d format.
I'm having some problem with this query, it works on my localhost (IIS 6) but when i uploaded the same code to web server (IIS/7.5) it returns no data from the Database.
Here is the query:
Set RsTdL = Con.Execute("Select * From COURSE1 Where DUE_DATE Like '"&Date&"%' and CLASS = '"&CCode&"' ORDER BY SUBJECT, LECT_NO, SUBTOPIC")
'" & Date & "%' matches the system date in the database so it'll show current date record.
And in the DUE_DATE column the date is in this format 9/3/2013 3:42:03 PM
i don't know how to debug this query because its working on my local machine and the WEB SERVER doesn't return any errors so im stuck ...
Please help,
thanks
DUE_DATE is Date/Time type and you want to match all values for today's date ignoring the time of day. So ask for DUE_DATE greater than or equal to the earliest time of today (midnite) and less than tomorrow's date.
"SELECT * From COURSE1" & vbCrLf & _
"WHERE DUE_DATE >= Date() AND DUE_DATE < DateAdd('d', 1, Date())" & vbCrLf & _
"AND [CLASS] = '" & CCode & "' ORDER BY SUBJECT, LECT_NO, SUBTOPIC"
Also CLASS is a reserved word, so enclose that name in square brackets.
The Database may be using the date format set on the server.
Which could be different than your locale machine. Do a simple select on the date fields in each environment to see how they look.
For safe date comparisons use the datediff function and use d ( which counts the number of days ) as the parameter. Select rows with a datediff of zero to retrieve all courses on the same date. This will work even if the datestamp includes the time.
This is my first question, please be kind. I am writing a macro in access and I want a series of reports to run one after another. the problem is all report have a date range that needs to be entered. Some are for the previous week some are for the previous month.
Is there a way in VBA or the macro creator to automatically calculate a date range for the previous month or week and populate the field to fully automate the process without manually entering the date range each time.
I am a new to VBA. Any help would be great, just point me in the right direction. Have a good day.
This query is created using the query design window in MS Access and then cut from SQL view. It will show records for last week, where ww is week number, in table1
SELECT Table1.AKey, Table1.atext, Table1.ADate,
Format([ADate],"ww") AS Week, Month([ADate]) AS [Month],
Year([ADate]) AS [Year]
FROM Table1
WHERE (((Format([ADate],"ww"))=Format(Date(),"ww")-1)
AND ((Year([ADate]))=Year(Date())));
You will notice that one column is called Month. You can use this to set a previous month in a similar way to setting the previous week. For example, both last week and last month:
SELECT Table1.AKey, Table1.atext, Table1.ADate,
Format([ADate],"ww") AS Week, Month([ADate]) AS [Month],
Year([ADate]) AS [Year]
FROM Table1
WHERE (((Format([ADate],"ww"))=Format(Date(),"ww")-1)
AND ((Year([ADate]))=Year(Date())))
OR (((Month([ADate]))=Month(Date())-1)
AND ((Year([ADate]))=Year(Date())));
The SQL could be written much more neatly, but you may as well start with the query design window.
i guess the date has a own field in the database you open
then you can do something like this
strSQL = "SELECT * FROM reports WHERE Date >= " & now() -7
rs.open(strSQL)
' for the last week
strSQL = "Select * FROM reports WHERE Date >= " & now() - 30
rs.open(strSQL)
' for the last month
but you will need to format now() to the same format as it is in your Table
and that is just kinda the rawest code. i had to handle something similar and this worked out quite well
I have a time/date field in my select query and I would like to set the criteria to only output records from the last 90 minutes. please give me the proper SQL to copy/paste
thanks very much, Nathaniel
SELECT SYSADM_CUSTOMER_ORDER.PRINTED_DATE
FROM SYSADM_CUSTOMER_ORDER;
For MS Access you are looking for something like
SELECT SYSADM_CUSTOMER_ORDER.PRINTED_DATE
FROM SYSADM_CUSTOMER_ORDER
WHERE (((SYSADM_CUSTOMER_ORDER.PRINTED_DATE) Between DateAdd("n",-90,Now()) And Now()));
Have a look at Now Function and DateAdd Function
I'm assuming you're using MS Access with and MS Access backend. Because this is VBA code it will not work in the query screen.
Dim dteBeginTime as Date
dteBeginTime = DateAdd("n", -90, Now())
SELECT SYSADM_CUSTOMER_ORDER.PRINTED_DATE
FROM SYSADM_CUSTOMER_ORDER
WHERE PRINTED_DATE > #" & dteBeginTime & "#;"