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.
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 have a table, tblCandidates, with field PacerCallDuration_1 of type Date/Time. I would like to find the average time of the calls between today and today - DateRng. DateRng is an integer of how many days I want to go backwards in data, e.g. 30.
Dim PCD1 As Date
Dim DateRng as integer
Dim AvgCallTime As Date
AvgCallTime = DoCmd.RunSQL "select cdate(AVG([PacerCallDuration_1]))_
from tblCandidates"
It does not like my SQL statement and and other items in that line I have tried.
Searching the site I find "How can I get the average of a date field?" was close but not close enough.
Thank you for your help.
You can use DAvg:
DateRng = 5 ' for example.
AvgCallTime = DAvg("[PacerCallDuration_1]","tblCandidates","[CallDate] Between DateAdd('d', -" & DateRng & ", Date()) And Date()")
where CallDate is ... the call date.
I have to generate a rolling sum or Cumulative sum of the OIL field for each WELLNMBR as per its MONTH value.
The query used is
{SELECT tblProductionData.WELLNMBR, tblProductionData.[Month], tblProductionData.DAYS, tblProductionData.OIL, tblProductionData.GAS, tblProductionData.WATER, DSum("[OIL]","tblProductionData","WELLNMBR=" & "'" & [WELLNMBR] & "'" & "AND MONTH <=" & "#" & [MONTH] & "#") AS Expr2
FROM tblProductionData;
In the result the sum changes after every year, not after a month. I would like to know the reason for this strange behavior and the method to obtain the rolling sum in every month.
Incorrect result
http://i.stack.imgur.com/NFBRm.png
Desired result
http://i.stack.imgur.com/U8I9r.png
Your query begins '{'. This needs to be removed. Also 'MONTH' is a reserved word. Try replacing it with 'Mth', then proceed from there.
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
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