Solved

ADO Command object using Named Parameters

Posted on 2010-11-10
12
3,047 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
  • 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 142

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
 

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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34111998
please assign the CommandText BEFORE you defined and append the parameters
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 142

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now