Link to home
Start Free TrialLog in
Avatar of HooKooDooKu
HooKooDooKu

asked on

Open RS with Parameterized Queries (using ? rather than Named parameters)

I recently learned how to do Paramerized queries using '?' in the SQL and an array of variants for the Parameters.  But I can only figure out how to do this using ADODB.Connection object and an ADODB.Command object with a Command.Execute()

Example:
Cmd.Connection = Conn
Cmd.CommandText = "SELECT * FROM TABLE WHERE FIELD1 = ? AND FIELD2 = ?"
Set RS = Cmd.Execute(RecordsAffected,Array(10,"ABC"))

Open in new window

(where Array(10,"ABC") represents an Array of Variants the way the VBScript would)

But what I need to do is an RS.Open command where I can set properties of the Record Set such as CursorType and LockType.  For example, I'd logically like to do the following:
Cmd.CommandText = "SELECT * FROM TABLE WHERE FIELD1 = ? AND FIELD2 = ?"
Cmd.Parameters = Array(10,"ABC")
RS.Open(Cmd, adOpenDynamic, adLockPessimistic) 

Open in new window

But this is not valid code (you can not assign an array of variants to the Cmd Parameters).

Now I know how to accomplish this creating individual parameter objects and using named parameters in the SQL.  But that method seems to require that I define the data type for each parameter.  My situation is that I must convert a bunch of SQL commands to parameterized SQL commands in a program that I'm not familiar with and most field values are contained in variables (whos datatype isn't always apparent).

So I know I can take the time to look up each field in the database and determine how to create parameters for each field and use named parameters.

But I'd get things going much faster (with less room for mistakes in picking the wrong data types) if I could simply replace field values with '?' and pass in an array of variants for the parameters.

NOTE: I'm utilizing ADODB objects in a VB6 project.
ASKER CERTIFIED SOLUTION
Avatar of HooKooDooKu
HooKooDooKu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial