Date Format changes when inserting into table if start date and end date cross one or more months - date

I have an Access 2013 form which has two unbound date fields, FromDate and ToDate. I insert these into a table (TblGuestBooking) which has an autonumber key field, so this doesn't feature in the SQL statement to follow.
If the FromDate and ToDate are in the same month, the dates are entered as dd/mm/yy, the format of the form field. If, however, the From date is in one month and the to date is in the next month or later month, the format changes to mm/dd/yy for the subsequent months.
for example 26/2/14 to 3/3/14 results in the following table entries:
26/02/14,
27/02/14,
28/02/14,
03/01/14,
03/02/14,
03/03/14
This is the code snippet I am using to put the dates into the table (BookingID is obtained from the form.)
Dim BookingDate As Date
Dim SQLString As String
....
BookingDate = FromDate
Do
SQLString = "INSERT INTO TblGuestBooking ([BookingDate], [BookingID]) VALUES (#" & BookingDate & "#" & "," & Me.GuestSuiteBookingID & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL SQLString
DoCmd.SetWarnings True
BookingDate = BookingDate + 1
Loop Until BookingDate = ToDate + 1
If you've read this far, thank you for your time. If you can help me out, many, many thanks.

When processing date literals (text values enclosed by "hash marks" #) Access SQL will always interpret ambiguous xx-yy-zzzz dates as mm-dd-yyyy, regardless of the regional setting in place on the computer. So, if your machine is configured to display short dates as dd-mm-yyyy and you create an Access query that uses #04-02-2014# it will always be interpreted as April 2, not February 4.
The solution is to always format date literals as unambiguous yyyy-mm-dd values. In your case, instead of
... VALUES (#" & BookingDate & "#" ...
you would use something like
... VALUES (#" & Format(BookingDate, "yyyy-mm-dd") & "#" ...

In my case I've used following code for an ASP page that create and update a Date field in Access database.
strDay = Request("Day")
strMonth = Request("Month")
strYear = Request("Year")
InputDate = strYear & "-" & strMonth & "-" & strDay
if IsDate(InputDate) Then
InsertDate = CDate(InputDate)
else
'if date typed wrong, use Todays Date
InsertDate = Date()
end if
SQLUPD = "UPDATE MyDataBase SET DateField = '"& FormatDateTime(InsertDate, vbShortDate) &"'"
SQLINS = "INSERT INTO MyDataBase (DateField, Alist, Atype, Acomment) VALUES ('"& FormatDateTime(InsertDate, vbShortDate) &"', .....
Then all sorting and output opration with dates are going well.

Related

creating a time range filter when the data type of dates is text

I have a table with order date, usually most of the data is date, but sometimes I need to write text, so that's why I want to choose text as data type. My data in the table is as follows:
BestellDatum
20.10.2021
22.10.2021
03.01.2022
02.01.2022
23.01.2022
I have a query I created from this table and a form I created from this query. In this form I want to filter by date range (two text fields, names txt.orders_from and txt.orders_to). My code for this:
Private Sub SearchbyDateRange_Click()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.txt.orders_from) Or IsNull(Me.txt.orders_to) Then
MsgBox "Please enter a date range"
Me.txt.orders_from.SetFocus
Else
strCriteria = "([OrderDate] >= #" & Format(Me.txt.orders_from, "yyyy/mm/dd") & "# And [OrderDate] <= #" & Format(Me.txt.orders_to, "yyyy/mm/dd") & "#)"
task = "select * from qryOrders where(" & strCriteria & ") order by [OrderDate]"
DoCmd.ApplyFilter task
End If
End Sub
However, if I try to search for the period from 20.10.2021 to 03.01.2022, for example, no dates are displayed. This code works when the OrderDate column has the data type as date/time. Is there anything I can do to use this code with data type as Text or do you have any other idea?
Format expects a Date as the first parameter and you are feeding it a Text. Instead of trying to convert text into different text what you need is a way to convert your text into a date.
The function you are looking for to convert text to a date value and make date comparisons is CDate.
I am not 100% sure CDate will be able to read your specific date format but it is worth a try. If not you will have to do the hard work of breaking it down into a text array and recombining it to a more compatible format.
Convert your text dates to true dates:
strCriteria = "CDate(Replace([OrderDate], ".", "-")) >= #" & Format(Me.txt.orders_from, "yyyy\/mm\/dd") & "# And CDate(Replace([OrderDate], ".", "-")) <= #" & Format(Me.txt.orders_to, "yyyy\/mm\/dd") & "#"

dlookup not working for other month than MAY

I am using dlookup (date = date) its fine when the date is of "May" but when I put date of any other month like 1/6/19 the dlookup remains silent.
in Dlookup if date is of MAY its fine but not working when date id of JUN the code I am using is as below
Me
Total = DLookup("Total", "Dailycash", "Cdate =#" & sdate & "#")
Apply a proper format to the date value expression:
Total = DLookup("Total", "Dailycash", "Cdate = #" & Format(sdate, "yyyy\/mm\/dd") & "#")

VBA Error 3075 When Filtering Between Dates

I know that there are a lot of answered questions regarding this topic but none of them answer the following question:
I have got this little and simple code:
Private Sub btnHwEOLNext2_Click()
Dim filterString As String
Dim startDate As Date
Dim endDate As Date
startDate = Date
endDate = Date + 730
filterString = "[HW End of Support] BETWEEN '" & startDate & " And " & endDate & "'"
Me.Filter = filterString
DoCmd.SetOrderBy "[HW End of Support] ASC"
Me.FilterOn = True
End Sub
all i am trying to do is to filter between 2 dates. the startDatedate will always be the day you use the form and the endDate will always be 730 days after startDate
why do i keep on getting
error 3075
? Help please!
If you put the date between # signs, then VBA considers it as a date.
As far SQL is using American date system with MM-DD-YYYY, you can change the format between the # signs, before parsing:
"[HW End] BETWEEN #" & Format(sd,"MM-DD-YYYY") & "# And #" & Format(ed,"MM-DD-YYYY") & "#"

Create Dynamic List of Business Dates Since Arbitrary Start Date in Access

Using MS Access 2010, I need to generate a complete list of dates from an arbitrary start date, say #1/1/2015#, to now, Date(). I would like this list to live in its own table, although a query would work, too. I also would prefer to only grab business days/week days. Can anyone help?
I'm tracking business process errors by date. A new error record is made for each error, and each record is tagged with a date. However, there is not an error on every date. So reporting over time does not give a correct visualization, as the dates without errors are not represented.
I appreciate help generating this list in Access, as well as any alternative ideas for representing this information.
Cheers,
Burgess
Update - I've been able to make a list of dates since a start date. Here's my code:
Sub createDatesTable()
'Declare variables'
Dim startDate As Date
Dim endDate As Date
Dim countDate As Date
Dim length As Long
Dim i As Long
Dim dates() As Date
'Initialize'
startDate = #6/23/2015#
endDate = #9/1/2015#
countDate = startDate
length = endDate - startDate
'Define date array length'
ReDim dates(1 To length)
'Generate date list in array'
For i = 1 To (length)
dates(i) = countDate
countDate = countDate + 1
Next i
'Print array to Immediate Window'
For i = 1 To length
Debug.Print dates(i)
Next i
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tblDateList", dbOpenDynaset)
'Add array data to existing table'
For i = 1 To length
rs.AddNew
rs!DateList = dates(i)
rs.Update
Next i
rs.Close
db.Close
End Sub
Now, I'm very new to VBA, so this may well contain elementary mistakes. Thanks for the feedback.
-Burgess
Your table can have a structure like this:
Column name | Data type
---------------+----------
dt | DateTime
isBusinessDay | Yes/No
And as for the filling, something like this:
public sub fillDates(startDate as date, endDate as date)
dim db as DAO.database, rs as DAO.Recordset
dim strSQL as string
dim d as date
strSQL = "delete from tbl_dates"
docmd.runsql strSQL
set db = currentdb()
set rs = db.openRecordset("tbl_dates", dbOpenDynaset, dbAppendOnly)
for d = startDate to endDate
with rs
.addNew
![dt] = d
![isBusinessDay] = (format(d, "w", vbMonday) <= 5)
.update
end with
next d
rs.close
db.close
end sub

Convert Long number into Date in access vba

I am having a field which contains date in the form of number and need to convert into equivalent date for further operations such as checking between the date with other date variables.
For example : My long number variable is
Dim ndate as Long
ndate=20140901
I need to get this ndate as date variable such as 01/09/2014 (dd/mm/yyyy)
Thanks in advance
dim actualdate as date
actualdate = dateserial(ndate\10000, (ndate mod 10000)\100, ndate mod 1000000)
DateSerial takes the arguments year, month, day. The \ operator performs division discarding the remainder, and mod performs a division returning the remainder.
This can give you the answer
If the date is 20140901
then newdate will have 01/09/2014
dim newdate as date
newdate = CDate(Right(ndate, 2) & "/" & Mid(ndate, 5, 2) & "/" & Left(ndate, 4))