ADO Command object using Named Parameters

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".
heartland-techAsked:
Who is Participating?
 
heartland-techConnect With a Mentor Author Commented:
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
 
käµfm³d 👽Connect With a Mentor Commented:
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
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
>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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Alpesh PatelAssistant ConsultantCommented:
Hi, use "?" in place of parameter name in SQL Query.
0
 
heartland-techAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please assign the CommandText BEFORE you defined and append the parameters
0
 
heartland-techAuthor Commented:
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
 
käµfm³d 👽Connect With a Mentor Commented:
@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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you please show the connection string?
depending on the provider/driver you are using, the syntax will indeed vary ....
0
 
modus_operandiCommented:
Restarting auto-close process on heartland-tech's behalf.
 
modus_operandi
EE Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.