Solved

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

Posted on 2010-09-01
6
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

726 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