Solved

String to smalldatetime errors VB6 app to SQL Express

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

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

Suggested Solutions

Title # Comments Views Activity
sql query help 2 45
How to simplify my SQL statement? 14 50
Help with SQL - TOP 10 by date and by group 13 34
Sql query 107 27
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…

920 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