Solved

ADO Command object using Named Parameters

Posted on 2010-11-10
12
3,541 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 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