Solved

ADO Command object using Named Parameters

Posted on 2010-11-10
12
3,396 Views
Last Modified: 2012-05-10
Using VB 6, Microsoft ActiveX Data Objects Library 2.6, SQL Server 2005.

Named Parameters do not work, why?

    Dim strSQL As String
    Dim cmd As ADODB.Command
    Dim DBConn As Object

    Set DBConn = CreateObject("DBConn.busConnect")
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = ConnDB.DBConnString(strSvrName, strDBName) 'returns the connection string.
    cmd.CommandType = adCmdText
    cmd.NamedParameters = True

    cmd.Parameters.Append cmd.CreateParameter("@MiddleName", adVarChar, adParamInput, 60, MiddleName)
    cmd.Parameters.Append cmd.CreateParameter("@LastName", adVarChar, adParamInput, 60, LastName)

    strSQL = ""
    strSQL = strSQL & " Update Users Set MiddleName = @MiddleName Where Lastname = @LastName "

    cmd.CommandText = strSQL
    cmd.Execute

    Set cmd = Nothing
    Set DBConn = Nothing

Error received:
err.Number = -2147217900
err.Description = Must declare the scalar variable "@MiddleName".
0
Comment
Question by:heartland-tech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
12 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
ID: 34108010
Classic ADO doesn't support *true* named parameters from what I can tell. You will have to use the question-mark syntax and add your parameters in the order they are to appear in the query:
strsql = "Update mytable Set MiddleName = ? Where Lastname = ?"

' @MiddleName comes before @LastName in the query, so add it first
cmd.Parameters.Append cmd.CreateParameter("@MiddleName", adVarChar, adParamInput, 20, MiddleName)
cmd.Parameters.Append cmd.CreateParameter("@LastName", adVarChar, adParamInput, 20, LastName)

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 total points
ID: 34109575
>Classic ADO doesn't support *true* named parameters from what I can tell.

ADO 2.6+ can use named parameters, but you have to set the NamedParameters property of the adodb.command object to true before:
http://msdn.microsoft.com/en-us/library/ms675840%28VS.85%29.aspx

ADO.net: can use named parameters
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx


Data provider vs Parameter naming syntax

System.Data.SqlClient
Uses named parameters in the format @parametername.

System.Data.OleDb
Uses positional parameter markers indicated by a question mark (?).

System.Data.Odbc
Uses positional parameter markers indicated by a question mark (?).

System.Data.OracleClient
Uses named parameters in the format :parmname (or parmname).

Open in new window

0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34110062
Hi, use "?" in place of parameter name in SQL Query.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:heartland-tech
ID: 34111977
angellll,
If you look at my code example, you can see I have set the Command's NamedParameters property to true.  My code from above: cmd.NamedParameters = True

Please show an example in VB6 code on how to make Named Parameter work in the Command's CommandText.

Again I am using VB 6 with a project reference to Microsoft ActiveX Data Objects Library 2.6, and SQL Server 2005
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34111998
please assign the CommandText BEFORE you defined and append the parameters
0
 

Author Comment

by:heartland-tech
ID: 34113989
angellll:
Your suggestion does not work.  Here is my errors:
?err.Number
-2147217900
?err.Description
Must declare the scalar variable "@MiddleName".

Attached is my Code.

Do you have another suggestion?



Dim strSQL As String
    Dim cmd As ADODB.Command
    Dim DBConn As Object

    Set DBConn = CreateObject("DBConn.busConnect")
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = ConnDB.DBConnString(strSvrName, strDBName) 'returns the connection string.
    cmd.CommandType = adCmdText
    cmd.NamedParameters = True

    strSQL = ""
    strSQL = strSQL & " Update Users Set MiddleName = @MiddleName Where Lastname = @LastName "

    cmd.CommandText = strSQL

    cmd.Parameters.Append cmd.CreateParameter("@MiddleName", adVarChar, adParamInput, 60, MiddleName)
    cmd.Parameters.Append cmd.CreateParameter("@LastName", adVarChar, adParamInput, 60, LastName)

    cmd.Execute

    Set cmd = Nothing
    Set DBConn = Nothing

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
ID: 34115772
@angellll

>>  ADO 2.6+ can use named parameters, but you have to set the NamedParameters property of the ...

That's what I would have thought, but I couldn't get it to work either, no matter which connection string I used to connect to the server. Several other message boards mentioned the same issue.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34122293
can you please show the connection string?
depending on the provider/driver you are using, the syntax will indeed vary ....
0
 

Accepted Solution

by:
heartland-tech earned 0 total points
ID: 34923468
I will upload the connection string tomorrow.  This thread also shows good evidence to support theory that ADO does not support Named Parameters.  Please do not delete it as other developers will have to go through the same trouble.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 34968313
Restarting auto-close process on heartland-tech's behalf.
 
modus_operandi
EE Admin
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

737 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