Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2008, 02:49 PM
Authorized User
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Implement parameterized queries in vb6

Hello,
I'm in desperate need of direction on how to implement parameterized queries in Visual Basic 6 codes. I've searched the net but can't seem to find any clear steps.

Any help would be appreciated.:)



 
Old July 24th, 2008, 03:16 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Parameterization of queries is a database technology, not a VB technology.

You use VB to create DB connections, and use those connections as the query communicator.

What is it exactly that you are trying to do? Perhaps knowing that, a good solution will become apparent.
 
Old July 24th, 2008, 03:38 PM
Authorized User
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Thank you for replying.
I have an app in vb6 with SQL server 2000 as my backend. I'm trying to save some data to the database; a part of the data to be saved is a date. However i kept getting the error: "The conversion of a char datatype to a datetime datatype resulted in an out-of-range datetime value". It was suggested that the problem arose from my vb app and that I should use parameterized queries to input data into the database. Hence my question.
I hope I've given you enough information so that you may be able to assist me, since i really need assistance.

 
Old July 24th, 2008, 09:04 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

No code, no help.

You don't even say if you are using ADO or DAO to access the DB.

If you are using ADO, then that sounds like you are passing an ADODB.Parameter that is using the wrong data type.

It *COULD* be as simple a thing as you trying to pass
    '24/07/2008'
to SQL Server, when SQL Server is expecting dates in MM/DD/YYYY (USA) format. Which is easy to fix in a couple of ways, but we need to see some code.
 
Old July 25th, 2008, 07:43 AM
Authorized User
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry about the adsence of code. Before I show you my code may i can give a little more info.
I've chosen the datatime data type (at the server end) for my column to store dates from my app. I'm inputing the system date via a date variable into the database. My OS is XP and my regional settings are English United States but the short date format is customized to dd/mm/yyyy; initially i'd chosen English United Kingdom but i changed it. I've tried saving the date and time, just the date alone, I've tried formating a mask edit control for the date format, but nothing seems to work.

Now here is my code:

The following is the declaration of Curdate:
Private CurDate As Date


Private Sub Form_Load()

medtDate.Text = Format(Now, (Date)) 'A mask edit box to display the date only

End Sub


Save Procedure:
Private Sub cmdSave_Click()

Call Globalconnect 'Connection string


CurDate = medtDate.Text


SQLInsert = "INSERT INTO Transac_Table (TransNumber, Date_Received,Quantity, Amount)" & _
"VALUES ('" & TCode & "', '" & CurDate & "', '" & txtQuantity.Text & "', '" & txtAmount.Text & "')"

connect.Execute SQLInsert

MsgBox "Data saved", vbOKOnly

Call Clear_all 'clearing cells after save

connect.Close

End Sub


 
Old July 25th, 2008, 11:43 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Databases are very picky about date formats. In Oracle, in a statement like yours, you must use an Oracle conversion function, and a conversion string specifying where the individual components of the date are to be found. It is an Oracle function, not a VB function.

It sounds like you are getting an error because, for instance, the year is where the day is expected, and a month day of 2,008 is a bit on the large side...

Look in your SQL Server documentation for inserting dates, and I think you'll find the info on the proper string to send from VB.
 
Old July 25th, 2008, 01:09 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Okay, so try this:

Code:
SQLInsert = "SET DATEFORMAT DMY; " _
          & "INSERT INTO Transac_Table (TransNumber, Date_Received,Quantity, Amount)" _
          & "VALUES ('" & TCode & "', '" & CurDate & "', '" _
          & txtQuantity.Text & "', '" & txtAmount.Text & "')"


That tells SQL Server that you want to use the format dd/mm/yyyy for the date. By default, SQL Server uses USA standard, mm/dd/yyyy.

BUT...

But this has *NOTHING* to do with "parameterized queries"!!

In fact, this is the opposite of a parameterized query; it's an AD HOC query.

On top of everything, this kind of query is the *MOST* susceptible to SQL Injection.

So you *REALLY* should be doing SQL Injection protection!

Also, I see other *probable* problems in there. You have apostrophes around the value of TCode
             ...('" & TCode & "',...
Yet the field in the DB is named TransNumber. If it's truly a number, kill those apostrophes. And SURELY that is also true about fields named Quantity and Amount. SURELY those are also numbers, no???

SO *GUESSING* at what you need...and it really is a guess...

Code:
SQLInsert = "SET DATEFORMAT DMY; " _
          & "INSERT INTO Transac_Table (TransNumber, Date_Received,Quantity, Amount)" _
          & "VALUES (" & CLNG(TCode) & ", '" & CurDate & "', " _
          & CDBL(txtQuantity.Text) & "," & CDBL(txtAmount.Text) & ")"


The calls to CLNG and CDBL in there are to *ENSURE* that nobody has managed to slip in SQL Injection to those form fields (may not be needed for TCode...can't tell).

If this still doesn't work, then use
    MsgBox SQLInsert
to see *exactly* what the query is that you are using and show that to us.
 
Old August 27th, 2008, 12:49 PM
Authorized User
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know this is an extremely late comment, but for the benefit of persons searching for a similar solution, the above solution (using SET DATEFORMAT DMY) works!!!

Thank you for your help experts.






Similar Threads
Thread Thread Starter Forum Replies Last Post
parameterized if-then not working hoqenishy XSLT 1 April 17th, 2008 01:25 PM
Parameterized Property surendraparashar C# 2005 4 October 17th, 2007 11:33 PM
Document() ... parameterized ? asearle XSLT 18 October 4th, 2006 02:46 AM
Parameterized SQLDataAdapter boyshey VB.NET 2002/2003 Basics 1 April 14th, 2006 07:08 AM
Combining Queries or results from 2 queries Ford SQL Server 2000 24 November 7th, 2005 08:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.