Solved

String to smalldatetime errors VB6 app to SQL Express

Posted on 2008-10-30
4
1,649 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
  • 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

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.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now