dd/mm automatically got changed to mm/dd - vba

I coded something using Date statement in Access VBA. It was working fine until the start of this month, but now I am seeing that the Date has automatically changed the format from dd/mm/yyyy to mm/dd/yyyy. Has anyone else encountered the same problem?

The default Access SQL date format, regardless of locale, is mm/dd/yyyy. If you use an invalid date format, it will 'helpfully' try to convert that to a valid date for you.
So, if you use '30/09/2008', it will recognize you're using dd/mm/yyyy, and convert it appropriately. However, a value like '10/01/2008' is a valid mm/dd/yyyy value to begin with, so it will not be converted, and stored incorrectly in case you actually meant dd/mm/yyyy....
The solution is to always convert your date values to a mm/dd/yyyy string prior to using them in Access SQL statements. You have to be a bit careful here, as using VBA date format masks may not work entirely as you'd expect on non-US locales (e.g. 'helpfully' interpreting "mm/dd/yyyy" as "the localized short date format"), so please test carefully using your particular Access/VBA version.

Access requires a date to be unambiguous. It is generally recommended that you use yyyy/mm/dd, regardless of locale. For example:
strSQL="SELECT SomeDate FROM tblT WHERE SomeDate=#" & Format(DateVar, "yyyy/mm/dd") & "#"

Try this code:
stLinkCriteria = "[ProjectDate] Between #" & Format(CDate(Me![txtDateFrom]), "mm/dd/yyyy") & "# And #" & Format(CDate(Me![txtDateTo]), "mm/dd/yyyy") & "#"
It works for me.

This works:
sentenciaSQL = "UPDATE Numeraciones " & _
"SET Valor = " & Valor & ", " & _
"Fecha = #" & **Format(fecha,"mm/dd/yyyy HH:nn:ss") & "#, " &** _
"Id_Usuario = " & Id_Usuario & _
" WHERE Nombre = '" & Nombre & "'"

I have been very successful using the datevalue() function. When getting dates from unbound controls it seems to be clever enough to interpret the format "dd/mm/yyyy" correctly. Thus, a Jet SQL query like
"Select * from DateTable where StartDate = datevalue(" & me!TxtStartDate & ");"
seems to work every time.

I was experiencing same issue while trying to build a SQL string through VBA.
My locale settings use dd/mm/yyyy and I was trying to put into SQL statement data taken from an unbound textbox in a form.
The issue was due to the fact I was declaring, let's say, varMyDate as "date" type, so the engine reverted the format back even after the format.
Since you are really building a string the logical and proper data type is "string", and that solved the problem.

Related

Managing Dates Query in ACCESS with SQL Server background

I am doing my project on ACCESS for the "front" part with the forms, and in SQL Server for the "back-end" part with the databases, everything works fine exect for one thing : I don't understand how to filter Access's forms with SQL Server's date type.
I have looked for everything on the web and all the solutions don't seems to work for my case (maybe because of the ACCESS/SQL Server env), things like :
Me.Form1.Filter = "[date born] = #" & Format("11/04/2022", "dd/mm/yyyy") & "#"
'with VBA directly
SELECT * FROM dbo_Person WHERE [date born] = Format('11/04/2022','dd/mm/yyyy') ;
-- with SQL Request from ACCESS on the table
but everytime I have the same issue: nothing is displayed even if there is rows who have this date in the right column. It is like if no row respected this condition even though it is not the case.
I'm guessing it may be an issue with SQL Server Date type who doesn't convert well on ACCESS.
That is how is desplayed by default in my ACCESS the dates of the column (my ACCESS software is setuped in French btw)
The data is well recognize as "not a String" because when I try to filter with a String an error appears.
Can someone help me to know where I missed something or misstyped something please ?
Edit : the Date column seems actually to be recognized as Short String in ACCESS, it may be the issue but still don't know how to fix because it is indeed a date type on sql server
"Texte court" can be translated to "short text" or short String
First, if you use data type DateTime2 in SQL Server, that will be read as text in Access.
So, change that to DateTime.
Next, use the universal format yyyy-mm-dd when filtering date values:
FilterDate = DateSerial(2022, 4, 11)
Me.Filter = "[date born] = #" & Format(FilterDate, "yyyy\/mm\/dd") & "#"
' Linked table in Access:
Sql = "SELECT * FROM dbo_Person WHERE [date born] = #" & Format(FilterDate, "yyyy\/mm\/dd") & "#;"

Why is my update query passing incorrect information to the table?

I have an SQL statement in VBA that when i run it, it updates my table with incorrect information. I've been struggling with this code for over a week trying workarounds and debugging but to no avail. I've searched online and found nothing even close to this.
DIM SQL as String
DIM periodStart as Date
DIM periodEnd as Date
periodStart = DateSerial(Year(Date), 12, 1)
periodEnd = DateSerial(Year(Date), 12, 15)
MsgBox "Period Start: " & periodStart & " Period End: " & periodEnd
SQL = "UPDATE EmpTime SET EmpTime.beginning = " & periodStart & " & EmpTime.ending = " & periodEnd & ";"
DoCmd.RunSQL SQL
The above code gives me a message box that shows me the periodStart and periodEnd variables are being built properly but then when i look to the table, the information is not the same as the Message box.
MsgBox
Table
Why is this happening and what can I do to fix it/avoid it ?
What I think is happening here is that your SQL is shaking out to be:
UPDATE EmpTime SET EmpTime.beginning = 12/1/2019, EmpTime.ending = 12/15/2019;
Access is not super amazing at guessing your intentions when you just send it math problems like this. Because it doesn't recognize your first date as a properly formatted string (12/01/2019 would be more appropriate) it is making the educated guess that you literally wanted to divide 12 by 1 by 2019. Which results in a decimal, or a very early time of the first date that MS Access can record: 12/30/1899 (like 12:05am, but there is no time dimension in play so it's dropped).
Instead try:
UPDATE EmpTime SET EmpTime.beginning = #" & Format(periodStart, "mm/dd/yyyy") & "# & EmpTime.ending = #" & Format(periodEnd, "mm/dd/yyyy") & "#;"
This does two things:
It formats (using the Format() function) your date into something access will recognize on its own.
It surrounds the date in # which is the microsoft office-y way of saying "This is explicitly a date, treat it as such or throw an error". Which is a much better scenario then "Guess what I meant when I send you this math/date"
Lastly, as Gordon mentions, and I also HIGHLY recommend is to switch this code over to use parameterized inputs in your SQL. here is a good write up of what that looks like. This solves two issues in your current code
Your malformed date would most likely error on being assigned to the correctly typed parameter before the SQL was executed alerting you that you have a bad date. (no guessing what went wrong and no bad data hitting your database)
You are protected from SQL Injection by users of your workbook. I assume this is not a super important facet of your workbook/application though since this is probably an internal company or personal thing and everyone using it can be trusted, but I am always in favor of hardening your code as best as possible since it's just good practice.
SQL = "UPDATE EmpTime SET EmpTime.beginning = #" & periodStart & "#, EmpTime.ending = #" & periodEnd & "#"

Conversion failed when converting date and/or time from character string

i have been getting this error "Conversion failed when converting date and/or time from character string." on a code that previously worked untill i migrated the database to a 2008 server.
Here is the code;
query1 = "INSERT INTO RequisitionSummary (RequisitionDate, RequisitionAmount, IssuingAmount) VALUES('" & dtpRequisitionDate.value & "','" & txtRequisitionAmt.Text & "','" & txtIssuingAmount.Text & "')"
I will appreciate if the error can be spotted because i have tried different approaches including converting the value in a variable befor inserting.
Thanks
Nelson
depending on the regional setting of your server vs your computer, you will get this error if it does not match.
You might need to convert into a date and then apply a formatting to be sure it match the setting in your database.
Or you can make a SQL Convert to regional setting in the sql (look at : http://msdn.microsoft.com/en-us/library/ms187928.aspx for more details on convert)

Access/VBA and SQL WHERE clause format issue

I have read a huge pile of problems and solutions, and I just can't figure out what I'm doing wrong.
BounceDate = DateValue(txtBounceDate.Value)
bncSql = "DELETE _BounceMaster.* FROM _BounceMaster" & _
" WHERE _BounceMaster.DateCheck >= #" & BounceDate & "#;"
DoCmd.RunSQL bncSql
_BounceMaster.DateCheck is in Date/Time format, which I think may be the issue, but I can't figure out what different format it should be in, or how to get there. As best as I can tell, BounceDate is correct - even using CDate didn't make a idfference. I have gotten both data mismatch errors, and currently, with code as above, I am getting syntax errors. What am I doing wrong?
I suppose the problem comes from date formatting. The BounceDate variable is DateTime type, so when you concatenate with string type variable, VBA automatically casts DateTime variable into String type using date format from your regional settings.
As I correctly remember, SQL interpreter from MS Access feels comfortable only with mm/dd/yyyy date format, so please try this:
BounceDate = DateValue(txtBounceDate.Value)
bncSql = "DELETE _BounceMaster.* FROM _BounceMaster" & _
" WHERE _BounceMaster.DateCheck >= #" & Format(BounceDate, "mm/dd/yyyy") & "#;"
DoCmd.RunSQL bncSql
It should be
DELETE FROM _BounceMaster
not
DELETE _BounceMaster.* FROM _BounceMaster
You should be using parameterized queries, as your code is subject to SQL injection attack.

Replace/Convert via sql while querying csv

I'm trying to query a CSV via ADODB/SQL and the results need to be ordered by date. The challenge is that the date is in dd-mon-yy format. I tried to use CONVERT and REPLACE but Excel 2003 keeps throwing Automation Error at me. I've tried both of the following separately.
With CONVERT:
"SELECT CONVERT(datetime,[Business Date],106) from [filename.csv]"
With REPLACE:
"SELECT REPLACE([Business Date],'-',' ') from [filename.csv]"
I don't have control over the CSV so manually correcting the dates is not an option.
How about:
SELECT CDate(Right([Business Date],2) & "/" _
& Mid([Business Date],4,3) & "/" _
& Left([Business Date],2)) from [filename.csv]
Depending, of course, on your locale. I do not believe Convert and Replace are available to ADO.