[2005] MS Access date problem
Hi;
My software tries to write and read date from ms access table. In the MS Access format is DD.MM.YYYY HH.mm.ss. Is there any date format in VB.Net which is like this? How can I write on this?
I used a code like this:
INSERT INTO TabelName Values(...., DD.MM.YYYY HH.mm.ss,...) and
INSERT INTO TabelName Values(...., 'DD.MM.YYYY HH.mm.ss',...)
but none of them worked :(
What can i do?
Re: [2005] MS Access date problem
Access requires a date to be delimited by the # sign. For example #1/4/2005#.
Try
VB Code:
"#" & Format(inDate, "yyyy-MM-dd hh:mm:ss") & "#"
Re: [2005] MS Access date problem
You shouldn't be worrying about format at all. Format is ONLY an issue when you are converting to or from a string, which you should NOT be doing. Keep your date object as date objects at ALL times, e.g.:
VB Code:
Dim myCommand As New OleDbCommand("INSERT INTO MyTable (ID, [Date]) VALUES (@ID, @Date)", myConnection)
myCommand.Parameters.AddWithValue("@ID", myID)
myCommand.Parameters.AddWithValue("@Date", myDate)
No need to worry about format at all because a binary date object has no format. Just one more reason to ALWAYS use parameters when building SQL statements with variables.
Re: [2005] MS Access date problem
I have tried lots of things. Format of date is dd.mm.yyyy hh:mm:ss I put # both sides too
INSERT INTO Stok VALUES (2, 'Rulman', 'f', 'fh', 5, 5, 'f', 'fs', #18.02.2007 17:02:55#, 'bf')
this is my string. Everything passes except date. It is identical to MS Access date. I mean as adding or viewing a date I see the same format :( but still it doesnt work. If I cant find any soluiton I will use string for date.
Re: [2005] MS Access date problem
OK, I say again: dates have NO format. The format you see is only how Access DISPLAYS a date, not how it is STORED. Date objects are always STORED the same way, regardless of how they are DISPLAYED. If you want to insert a literal date then you must represent it as #MM/dd/yyyy HH:mm:ss#. That literal value that you type will be interpreted as and converted to the appropriate binary date value, which will then be stored in Access and displayed to you using whatever format you've specified.
Now, having said that, I very much doubt that you're going to writing all literal values to insert like that. More likely each field value is going to come from a variable. I've shown you how to handle that in my previous post.