Solved

String to smalldatetime errors VB6 app to SQL Express

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What query can i write to find where a function is 4 34
how to Classify of email (pdf attachments) 7 35
SQL Insert parts by customer 12 31
VB.net and sql server 4 33
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…

785 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