Solved

How to update a datetime field in SQL Server 2008 database from Access database query

Posted on 2010-09-01
6
615 Views
Last Modified: 2012-06-27
I have an Access database from which I run an update query on a SQL Server 2008 database table. This query always worked fine in SQL Server 2005 but now having migrated the database to SQL Server 2008 on a new Windows 2008 server, despite the database running under 2005 mode, the Access query fails when updating a datetime column. We always use the European dd/mm/yy format. The date is taken from an Access textbox with a Shortdate format (dd/mm/yy).

With a date of 1st Sept the date is stored as "2010-01-09.00:00:00:000" which is incorrect. With a date of 31st August I get an error: "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value".

I have checked the regional settings in Windows 2008 which are set to Short date= dd/mm/yyyy as I had it on our old server.

The query is as shown in the code snippet. Hope you can help.


Dim DateStarted As Date

DateStarted = Format(Me.txtDateStarted, "dd/mm/yy")

(I have also tried DateStarted = Date)



strSQL = "INSERT INTO [SchDetails] (SchID, SchName, DateStarted, Paid, Dict) " & _

                "VALUES (" & schID & " , '" & SchName & "', '" & DateStarted & "', " & Paid & ", 'UK')"

Open in new window

0
Comment
Question by:Al230762
6 Comments
 
LVL 6

Expert Comment

by:YohanF
Comment Utility
Use following, this will put the the month in 3 letter format in middle and hence the DB will recognise the date formnat used. I use this as a work around for the problem you just mentioned.
strSQL = "INSERT INTO [SchDetails] (SchID, SchName, DateStarted, Paid, Dict) " & _
                "VALUES (" & schID & " , '" & SchName & "', #" & format(DateStarted,"dd-mmm-yyyy") & "#, " & Paid & ", 'UK')"

Open in new window

0
 
LVL 22

Expert Comment

by:Om Prakash
Comment Utility
Try
Dim DateStarted As Date
DateStarted = Format(Me.txtDateStarted, "mm/dd/yy")
strSQL = "INSERT INTO [SchDetails] (SchID, SchName, DateStarted, Paid, Dict) " & _
                "VALUES (" & schID & " , '" & SchName & "', '" & DateStarted & "', " & Paid & ", 'UK')"
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:Al230762
Comment Utility
YohanF: thanks for your response - I get an error whne trying your suggestion: "Incorrect syntax near '#'

om_prakesh_p: thank you also. Trying your suggestion, with a date of 1st Sept the query runs and the date is stored correctly as  "2010-09-01.00:00:00:000" HOWEVER - with 31st August date I get the same old error:"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value".


0
 
LVL 6

Accepted Solution

by:
YohanF earned 500 total points
Comment Utility
ok try it like this then -

strSQL = "INSERT INTO [SchDetails] (SchID, SchName, DateStarted, Paid, Dict) " & _
                "VALUES (" & schID & " , '" & SchName & "', '" & format(DateStarted,"dd-mmm-yyyy") & "', " & Paid & ", 'UK')"
0
 

Author Comment

by:Al230762
Comment Utility
YohanF: that worked. Thank you very much. Points awarded.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now