Results 1 to 12 of 12

Thread: *Resolved* Help With Calling a SP from VB.Sincere Thanks to VBForums :)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    86

    Unhappy *Resolved* Help With Calling a SP from VB.Sincere Thanks to VBForums :)

    Here is the code that I am working on : I need to call the Stored Procedure from VB and fetch the data. Please tell me where I am going wrong :

    VB Code:
    1. Public Function RS1(ByVal pId As Integer, ByVal cId As Integer, ByVal FrmDt As String, ByVal ToDt As String) As Integer
    2.  
    3.     Dim adoCon As New ADODB.Connection
    4.     Dim adoRset As New ADODB.Recordset
    5.     Dim adoCmd As New ADODB.Command
    6.     Dim retCode As Integer
    7.     Dim rStr As String  
    8.  
    9.     On Error GoTo ErrorHandler
    10.        
    11.     With adoCmd
    12.         .ActiveConnection = connString
    13.         .CommandType = adCmdStoredProc
    14.         .CommandText = "proc_name"
    15.          
    16.         .Parameters.Append .CreateParameter("@retrn", adInteger, adParamReturnValue, ,0)
    17. .Parameters.Append .CreateParameter("@output", adInteger, adParamOutput, , 999)
    18.          
    19.         .Parameters.Append .CreateParameter("@p_id", adInteger, adParamInput, , pId)
    20.         .Parameters.Append .CreateParameter("@c_id", adInteger, adParamInput, , cId)
    21.         .Parameters.Append .CreateParameter("@frm_dt", adVarChar, adParamInput, 12, FrmDt)
    22.         .Parameters.Append .CreateParameter("@to_dt", adLongVarChar, adParamInput, 12, ToDt)
    23.      End With
    24.        
    25.      Set adoRset = adoCmd.Execute
    26.      Do While (Not adoRset Is Nothing)
    27.           If adoRset.State = adStateClosed Then Exit Do
    28.           While Not adoRset.EOF
    29.               For i = 0 To adoRset.Fields.Count - 1
    30.                   rStr = rStr & " : " & adoRset(i)
    31.               Next i
    32.               Debug.Print Mid(rStr, 3, Len(rStr))
    33.               adoRset.MoveNext
    34.               rStr = ""
    35.           Wend
    36.           Set adoRset = adoRset.NextRecordset
    37.           Loop
    38.      
    39.       retCode = adoCmd.Parameters("@retrn").Value
    40.       Debug.Print "Return: " & adoCmd.Parameters ("@retrn").Value        
    41.       GoTo Shutdown
    42.      
    43.    
    44. ErrorHandler:
    45.  
    46.     Set adoCmd = Nothing
    47.     RS1 = -100
    48.  
    49. Shutdown:
    50.           Set adoCmd = Nothing
    51.           Set adoRset = Nothing
    52.           Set adoCon = Nothing
    53.  
    54. End Function
    55.  
    56. Public Function PpltRS() As Integer
    57.          RS1 p1_Id, c1_Id, Frm_Dt, To_Dt
    58. End Function
    Piece of SQL code :
    Code:
    create procedure proc_name
    @p_id 	int=null,
    @cr_id	int=null,
    @frm_dt	varchar(12),
    @to_dt	varchar(12),
    @retrn	int output
    as
    Also,how can I call this function at a click of a button ?

    Thanks.Any help is appreciated .
    Last edited by NewbieVB2003; Jun 6th, 2003 at 07:23 PM.

  2. #2
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Active Connection property of the command object should be a cn object - not the cn string of a cn object.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    86
    Thank you for replying back ;but I have no problem with the Connection String.The issue is something to do with fetching recordset.

  4. #4
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    What line and what error?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    86
    Thank you Briantcva for spontaneous reply.

    I have updated my Original Post with another function from which I am calling RS1 function.

    What I am trying to do here is execute the SP and then return the OUTPUT value if successful.

    On executing the code, it is displaying the default value set in PpltRs function,namely , pRtrn =10
    whereas it should be displaying a different value.

  6. #6
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Active Connection property of the command object should be a cn object - not the cn string of a cn object.
    I agree here. I just don't know why you are not getting any error. Here's how I usually build mine:

    VB Code:
    1. '-BUILD ADOs
    2.    With adoCN
    3.       .ConnectionString = CNSTR
    4.       .CursorLocation = adUseClient
    5.       .Open
    6.    End With
    7.    With adoCMD
    8.       .ActiveConnection = adoCN
    9.       .CommandType = adCmdStoredProc
    10.    End With

  7. #7
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    VB Code:
    1. Public Function PpltRS() As Integer
    2.     Dim pRtrn As Integer 'I got This
    3.     pRtrn = 10 ' I got this but why is it here? it is not being used by anything?
    4.          RS1 p1_Id, c1_Id, Frm_Dt, To_Dt 'Opening the Rs I assume?
    5.     PpltRS = pRet 'Where is pRet coming from????
    6.     Debug.Print "pRet ::::: " & Str(pRet)
    7. End Function
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    86

    Exclamation

    Thank you all for replying back.

    My Bad ...Correct Code Snippet :
    VB Code:
    1. Public Function PpltRS() As Integer
    2.     Dim pRtrn As Integer 'I got This
    3.     pRtrn = 10 ' I got this but why is it here? it isn't being used by anything?
    4.          RS1 p1_Id, c1_Id, Frm_Dt, To_Dt 'Opening the Rs I assume?
    5.     PpltRS =pRtrn
    6.     Debug.Print "pRet ::::: " & Str(pRtrn)
    7. End Function
    Basically what I want to achieve is that the function RS1 will be invoked and we will pass parameters p1_Id, c1_Id, Frm_Dt, To_Dt and,it should give me an Output which confirms that the procedure is run sucessfully .I can be sure if the pRet value matches the Return value after running procedure.

    You are right!!! I have messed up this part of the code. As it is not setting the value from the 1st Function. But how can I fix it.
    Thanks
    Last edited by NewbieVB2003; Jun 5th, 2003 at 02:55 PM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    86
    Thank you ARPRINCE and Briantcva ,
    I have made the changes ;but the Output field is still showing the wrong resultant.
    Thanks

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    86
    Using Debug tool , I found out that it is skipping this piece of code and directly going to ErrorHandler :

    VB Code:
    1. Do While (Not adoRset Is Nothing)
    2.           If adoRset.State = adStateClosed Then Exit Do
    3.           While Not adoRset.EOF
    4.               For i = 0 To adoRset.Fields.Count - 1
    5.                   rStr = rStr & " : " & adoRset(i)
    6.               Next i
    7.               Debug.Print Mid(rStr, 3, Len(rStr))
    8.               adoRset.MoveNext
    9.               rStr = ""
    10.           Wend
    11.           Set adoRset =adoRset.NextRecordset
    12.           Loop
    Last edited by NewbieVB2003; Jun 5th, 2003 at 03:48 PM.

  11. #11
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Remove your error handler and run your routine again to know where exactly your code craps out and will give you a description of the error.

    Anyway, if it is skipping that code lines entirely, then it would fall into the line where you are trying to retrieve your recordset.

    VB Code:
    1. set adoRset = adocmd.execute

    Did you ever change your active connection = adocn?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    86
    You were so very correct.
    I have been able to fix my error now.

    Thanks
    Last edited by NewbieVB2003; Jun 5th, 2003 at 10:10 PM.

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