Solved

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

Posted on 2010-09-01
6
617 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
ID: 33575126
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
ID: 33575129
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]
ID: 33575139
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:Al230762
ID: 33575281
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
ID: 33575296
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
ID: 33575350
YohanF: that worked. Thank you very much. Points awarded.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

821 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