Solved

String to smalldatetime errors VB6 app to SQL Express

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

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

808 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