Solved

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

Posted on 2010-09-01
6
619 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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33575139
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

792 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