[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2010-09-01
6
Medium Priority
?
632 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
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.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

650 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