Solved

ADO Command object using Named Parameters

Posted on 2010-11-10
12
2,965 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 74

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

747 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

9 Experts available now in Live!

Get 1:1 Help Now