Solved

ADP - Must Declare the Scalar Varaible

Posted on 2007-11-22
6
939 Views
Last Modified: 2013-12-05
I am having difficulty with a ADP project, specifically regarding ADO / SQL usage.

I'm currently in the process of writing a function to return a resultset from a SQL table (SQL Server 2005), the code is fairly simple (see attached example).

The problem line looks to be: "Set rs_ = .Execute"... it throws the following error: "Must Declare the Scalar Variable @id".

I have tried to use '?' marks instead, but no luck; does anyone have any ideas (short of creating a stored procedure!).

Cheers
Function Example()

On Error GoTo ErrorHandler:
 

Dim command_ As New ADODB.Command

Dim rs_ As ADODB.Recordset
 

    With command_

    

        .ActiveConnection = CurrentProject.Connection

        .NamedParameters = True

        .CommandType = adCmdText

        .CommandText = "SELECT t.* FROM ticket t WHERE t.id = @id"

        

        .Parameters.Append .CreateParameter("@id", adInteger, adParamInput, , 11)

        

        Set rs_ = .Execute

        

        With rs_

        

            If .State = adStateOpen Then

            

                ' TODO: this bit!

            

            End If

        End With

    End With

    

Tidy:
 

    Set command_ = Nothing

    Set rs_ = Nothing

    

Exit Function

ErrorHandler:
 

    Debug.Print Err.Description: GoTo Tidy
 

End Function

Open in new window

0
Comment
Question by:MISLtd
  • 2
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 20333437
Replace "SELECT t.* FROM ticket t WHERE t.id = @id"

with

"SELECT t.* FROM ticket t WHERE t.id = " & the vale for @ID.

IF @ID is text then

"SELECT t.* FROM ticket t WHERE t.id = '" & @ID & "'"
0
 
LVL 1

Author Comment

by:MISLtd
ID: 20333538
While that would work, it leaves things a fairly open to SQL injection attacks as the @id parameter will be generated by user input.
0
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 20333549
OK, but you haven't said where @ID is coming from. What you have in the adp is a statement that will be executed. Using ADO you have to pass these parameters in from somewhere.

You are using adCmdText. This just executes the string you create
0
 
LVL 1

Author Comment

by:MISLtd
ID: 20333617
I gave the '?' mark another go and seem to have solved the problem.
Function Example()

On Error GoTo ErrorHandler:
 

Dim command_ As New ADODB.Command

Dim rs_ As ADODB.Recordset
 

    With command_

    

        .ActiveConnection = CurrentProject.Connection

        .NamedParameters = True

        .CommandType = adCmdText

        .CommandText = "SELECT t.* FROM ticket t WHERE t.id = ?"

        

        .Parameters.Append .CreateParameter("id", adInteger, adParamInput, , 11)

        

        Set rs_ = .Execute

        

        With rs_

        

            If .State = adStateOpen Then

            

                ' TODO: this bit!

            

            End If

        End With

    End With

    

Tidy:
 

    Set command_ = Nothing

    Set rs_ = Nothing

    

Exit Function

ErrorHandler:
 

    Debug.Print Err.Description: GoTo Tidy
 

End Function

Open in new window

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 21641033
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Select JOIN table 2 22
Azure SQL Insert not working suddenly 11 22
Need a starter for ETL protocol? 4 42
MSSQL: Substring and Charindex error 7 18
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

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

13 Experts available now in Live!

Get 1:1 Help Now