problem on a module that i made on microsoft access 2016 vba thar are changing a date that is written on dd/mm/yy to mm/dd/yy


i have a problem on ms access 2016 (on the recent versions it's worked fine). the problem is that by my country the Date format is dd/mm/yy but for sql queries on access i has to change the date format to mm/dd/yyyy. if not i don't get the right results.
so i have this function that change the date to a string that is mm/dd/yyyy and on every sql string on vba that is using a date field i use this module like this:
"select * FROM InvoiceT WHERE InvoiceDate>=#" & fixdate(me.invoiceDate) & "#"

this is the module
Public Function FixDates(d As Date) As String

FixDates = month(d) & "/" & DAY(d) & "/" & year(d)

End Function

Open in new window

Until now  was a successful solution, but for some reason now when i write this code;  
msgbox fixdates(01/10/2015)

Open in new window

that the result has to be on mm/dd/yy format 10/01/2015 but the result is  12/30/1899
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
The problem is the way you are calling the function.
When you write:

Open in new window

It is being interpreted as if you are writing
fixdates(1 divided by 10 divided by 2015)
Mind you, the engine behind cannot tell whether you are trying to do integer division or pass it a date.

Therefore when dealing with dates, they should usually be surrounded by # just like we surround strings with quotes.

For more information on this issue try to look at Datetimes and their delimiters
bill201Author Commented:
thanks for your answre but on this module i saw the problem (and it's in "#"), it's a module that get the current tax rate by the recent date that the tax rate has been changed :
Public Function CurrentTaxRate(ab As Date) As Double
    Dim strab As String
    strab = FixDates(ab)
    Dim DblTaxRate As Double
    Dim DateTaxRate As Date
    DateTaxRate = Nz(DMax("DateTaxRate", "MaamRateT", "DateTaxRate<=#" & strab & "#"), 0)
    DblTaxRate = Nz(DLookup("TaxRate", "MaamRateT", "DateTaxRate=#" & FixDate(DateTaxRate) & "#"), 0)
    CurrentTaxRate = DblTaxRate
End Function

Open in new window

Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
Hmm. I don't have Access 2016 myself, so can't really check it out myself.

As far as I can tell, it should work. What you could try is to convert the date to a double (and then not use the # signs. But that by itself can yield other problems such as how the decimal separator is formatted in different regions.

Have you checked that a bad entry hasn't made it's way into the table?

What exactly is erroring? The code you posted for CurrentTaxRate looks fine, but the way you called it in your original post using msgbox does NOT look fine.

If you write
msgbox fixdates(#01/10/2015#)

Open in new window

Do you get the result you expect?
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

You might be better off with the Format() Function:
Public Function FixDates(d As Date) As String
    FixDates = Format(d, "mm/dd/yyyy")
End Function

Open in new window

bill201Author Commented:
hi GrahamSkan

thanks for your comment. i do like your advice but the result that i type on the vba immediate window: msgbox fixdate(#01/10/15#) is 00:00:00.

Anders Ebro thanks for your comment, thanks a alot for your solution.
Gustav BrockCIOCommented:
> the result that i type on the vba immediate window: msgbox fixdate(#01/10/15#) is 00:00:00.

If so, your module or something else is broken.
Run a repair and compact, or create a new database and import all objects from the old database file.

Assuming that you are using one of 'fixdate' or 'fixdates' for both the function name and the call, then that is surprising.
What, out of interest, does this do?
msgbox #01/10/15# 

Open in new window

Gustav BrockCIOCommented:
I guess fixdate was a typo.

MsgBox #01/10/15# will return 10-01-2015

as the date value will be casted to a string using the local date format from Windows.

Dates in Access (and SQL Server, other RDBMS, and even Excel) are stored as double precision numbers.  The integer portion is the number of days from the origin date + or - and the decimal represents the time of day.  When you get 12/30/1899 as a date, that means that the underlying value is actually 0.0 since for Access, the origin date is 0 and dates are counted from that value.  So 1 = 12/31/1899 and -1 = 12/29/1899.

Put a stop in the code in the conversion function and display the value that is passed in.  That may shed some light on the issue.
bill201Author Commented:
thanks for your effort to resolve this  frustrating problem , i made changes on the code, and it's working just fine, it's seems that microsoft made some changes on  access 2016 (it semms that  they change the way that a date text filed displayed on a form ,but not in a veriable, maybe it's just a bug).

 thanks to all of  you , I will divide the points.

this is my new code
   Dim r As Double
   Dim d As Date
   Dim db As Database
   Dim rs As Recordset
   Set db = CurrentDb
   Dim Sqlstr As String
   Sqlstr = "SELECT Max(MaamRateT.DateTaxRate) AS MaxOfDateTaxRate " & _
            "FROM MaamRateT " & _
            "WHERE (((MaamRateT.DateTaxRate)<=#" & Me.InvoiceDate & "#));"
   Set rs = db.OpenRecordset(Sqlstr, dbOpenSnapshot)
   d = rs!Maxofdatetaxrate
   Sqlstr = "SELECT * FROM MaamRateT WHERE DateTaxRate=#" & FixDate(d) & "#"
   Set rs = db.OpenRecordset(Sqlstr, dbOpenSnapshot)
   CurrentTaxRate = rs!taxrate
   Set rs = Nothing
   Set db = Nothing

Open in new window

and i'm calling this function with this code

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOCommented:
This line should read:

     "WHERE (((MaamRateT.DateTaxRate)<=#" & FixDate(Me.InvoiceDate) & "#));"

bill201Author Commented:
It is the only solution that solved the problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.