Results 1 to 3 of 3

Thread: RESOLVED: Odd VB Error in connection with SQL Stored Procedure

  1. #1

    Thread Starter
    Addicted Member New2VB's Avatar
    Join Date
    Jun 2000
    Location
    Los Angeles
    Posts
    161

    Resolved RESOLVED: Odd VB Error in connection with SQL Stored Procedure

    Trying to figure out some basic applications of Stored Procedures with VB (VB6).
    I have the following Stored Procedure in MS SQL Server:
    Code:
    CREATE PROCEDURE dbo.MySP
    	@MyState varchar(15) = 'California', 
    	@MySort varchar (50) = 'Zip' As
    	Declare @MySz varchar(100)
    	Select @MySz = 'Select * From MyTable Where State = ' + @MyState + ' Order by  ' + @MySort
    Exec(@MySz)
    GO
    MYSp is connected to my app through a DataEnvironment object as a data command named "MyCommand" which has (in addition to the RETURN_VALUE), two parameters: (1) "MyState" and (2) "MySort" (both of Data Type "adVarChar" and Host Data Type "String(VT_BSTR)");

    I have called MyProcedure from VB6 as follows:
    VB Code:
    1. Private Sub Form_Load()
    2.     Text1.Text = ""
    3.     DE.MyCommand4 "CA", "Zip"
    4.     With DE.rsMyCommand
    5.         Do While Not .EOF
    6.             If Not Text1.Text = "" Then
    7.                 Text1.Text = Text1.Text + ", "
    8.             End If
    9.             Text1.Text = Text1.Text + .Fields(1).Value
    10.             .MoveNext
    11.         Loop
    12.         .Close
    13.     End With
    14. End Sub
    When I do, I get the following error:
    Run-time error '-2147217900 (80040e14)':
    Invalid column name 'California'.
    MyTable looks as follows:
    IDNo Name City State Zip
    {39... Jose Coral Gables Florida 035180895
    {08... Gretchen WLA California 900250012
    {FA... Leon Jr. Miami Florida 035120088
    {79... Leon Sr. LA California 900690354
    {F5... Zack WLA California 900250012
    Why is VB seeking to equate the MyState variable with a column in the SQL DB when the SQL statement places the @MyState variable into a conditional context as in
    Code:
    ' . . . WHERE State = " + @MyState
    ????

    I appreciate any help I can get. Thanks.
    Last edited by New2VB; Jul 22nd, 2005 at 12:02 PM. Reason: Resolved

  2. #2
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: Odd VB Error in connection with SQL Stored Procedure

    @MyState is a string variable and therefore must be surrounded by quotes:
    Code:
    CREATE PROCEDURE dbo.MySP
    	@MyState varchar(15) = 'California', 
    	@MySort varchar (50) = 'Zip' As
    	Declare @MySz varchar(100)
    	Select @MySz = 'Select * From MyTable Where State = ''' + @MyState + ''' Order by  ' + @MySort
    Exec(@MySz)
    GO
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  3. #3

    Thread Starter
    Addicted Member New2VB's Avatar
    Join Date
    Jun 2000
    Location
    Los Angeles
    Posts
    161

    Re: Odd VB Error in connection with SQL Stored Procedure

    Thanks. That worked!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width