Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ms Access date format?

Status
Not open for further replies.

mattscotney

Programmer
Jul 31, 2002
57
0
0
AU
Hi all,

I am trying to update my Access database through vb.net using the following sql statement:

Dim myDate As Date = Date.Now

selectString = "INSERT INTO Purchase(ClientID, Date) VALUES (50, '" & mydate & "')"

updateDB(selectString)

The Date field in access is a Date/Time type in short date format.

I know there is nothing wrong with my database connection or updateDB procedure as I can update the clientID and other fields without any hassles.

How do I format the date so that access will accept the sql date query?

Thanks,
Matt Scotney
 
The standard ToString() conversion for DateTime won't work for Access. You need to convert explicitly, like:

Code:
selectString = "INSERT INTO Purchase(ClientID, Date) VALUES (50, '" & mydate[COLOR=red].ToString("d")[/color] & "')"

The "d" tells it to format into the form mm/dd/yyyy, which Access likes.
 
Hi again,

I tried your code and I still get the same error.

Thanks,
Matt Scotney
 
What, exactly, is the error you're getting?
 
Try placing #'s around your date instead of single quote (')

Scott
Programmer Analyst
<{{><
 
Nope, #'s around the date didn't work either.

When quering a date eg. SELECT...WHERE...#&quot; &amp; mydate &amp; &quot;#... you have to use the #'s, not when updating / inserting though.

I finally fixed it simply by putting [] around the word date. Sql does not appreciate you using reserved words as field names!

Code:
selectString = &quot;INSERT INTO Purchase(ClientID, [Date]) VALUES (50, '&quot; &amp; mydate.ToString &amp; &quot;')&quot;

Hopefully if someone else has this same problem I have solved it for them too.

Thanks for all you help.
 
How do I format the date so that access will accept the sql date query?

I thought you were having problems with the value and not the column name. Yeah, the reserved word problem will drive you crazy....

Scott
Programmer Analyst
<{{><
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top