?
Solved

String to smalldatetime errors VB6 app to SQL Express

Posted on 2008-10-30
4
Medium Priority
?
1,702 Views
Last Modified: 2008-11-05
We have 2 vb6 applications.
Both used to use SQL 2000 Standard.
Both databases were recently moved to SQL 2005 Express.

Both application use the same code to convert textbox values to sql date time values.

Public Function SQLDate(dtValue As Date) As String
10        SQLDate = "'" & Format(dtValue, "YYYY-MM-DD") & "'"
End Function

SQLExec = "UPDATE tbl SET CreateDate = "  & SQLDate(Text1.text) & " WHERE User = 1"
Adoconn.Execute SQLEXEC

Application 1 has no problems doing these updates.
Application 2 on the otherhand gives an error.
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.

I get the same error in both dbs when if copy'n'paste sqlexec into query analyzer. When the quotes around the date are removed, it works fine......

Application 2 works when I alter the date format to:

Public Function SQLDate(dtValue As Date) As String
10        SQLDate = "'" & Format(dtValue, "DD-MM-YY") & "'"
End Function

But only works on my development machine and not on a production machine.

I have tried changing the datatypes to DateTime instead of SmallDateTime to no avail.

What's going on here and how can I resolve it.

Thanks!
0
Comment
Question by:DennisPost
[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
  • 2
4 Comments
 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 22839176
Hello,
You can always use sql syntax like this:
SQLExec = "UPDATE tbl SET CreateDate = Convert(smalldatetime,"  & SQLDate(Text1.text) & ",105) WHERE User = 1"
Adoconn.Execute SQLEXEC

105 parameter for Convert T-SQL function is for second version of your SQLDate function but you can find something useful in documentation of Convert T-SQL function.

Your problem is probably because of using different national and region settings on laptop and sql server.
Best Regards
FooKy
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22839179
"But only works on my development machine and not on a production machine."


! - date format may different in development  and production(means in development  server may dd/mm/yyyy format  and in production may 'mm/dd/yyyy')

0
 
LVL 2

Author Comment

by:DennisPost
ID: 22840316
Fooky,
Your idea might work but I have a lot of date fields and I would have to make many changes in the application.
It also still doesn't explain why the exact same code does work from the other application.

Binuth,
My development machine, production machine and db server all have the same regional settings.

Doesn't work in SQL management
UPDATE tbl SET CreateDate = '2008-10-30 12:32:42'  WHERE User = 1

But htis does (no quotes)
UPDATE tbl SET CreateDate = 2008-10-30 12:32:42 WHERE User = 1

So I change the SQLDate function to not include any quotes.

Public Function SQLDate(dtValue As Date) As String
10        SQLDate = Format(dtValue, "YYYY-MM-DD")
End Function

No difference. :-( ??

0
 
LVL 2

Accepted Solution

by:
DennisPost earned 0 total points
ID: 22849277
Got it !!
I compared the 2 application databases using
DBCC UsersOptions
Found that the database for applcation 1 had date format YMD.
So I just changed the date format of the other applications databse to the same.
Set DateFormat YMD

 


0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

800 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