Results 1 to 2 of 2

Thread: How to update record using stored procedure & vb6?

  1. #1

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Question How to update record using stored procedure & vb6?

    Can someopne help me on how to update a record using stored procedures & vb6? my code below doesnt work. instead of updating the record it adds new record. I cnt understand y?

    VB Code:
    1. CREATE PROCEDURE [dbo].[sp_update]  @TestID bigint, @First_Name varchar (50), @Middle_Name varchar(50), @Family_Name varchar(50) AS
    2.  
    3. UPDATE    tblTest
    4. SET             tblTest.FirstName = @First_Name, tblTest.MiddleName = @Middle_Name, tblTest.FamilyName = @Family_Name
    5. WHERE     (tblTest.TestID = @TestID)
    6. GO
    VB Code:
    1. Private Function UpdateRecordset(mRecNO As Long)
    2.  
    3. Dim sp_updateRec As ADODB.Command
    4. Dim db As New ADODB.Connection
    5. Dim pr_update(4) As Variant
    6. Dim objParam As Long
    7.  
    8. db.Open myDB
    9. db.CursorLocation = adUseClient
    10. Set sp_update = New ADODB.Command
    11. sp_updateRec.ActiveConnection = db
    12. sp_updateRec.CommandType = adCmdStoredProc
    13. sp_updateRec.CommandText = "sp_update"
    14.  
    15. '2nd solution
    16. Set objParam = sp_ShowRec.CreateParameter("TestID", adBigInt, adParamInput, 8, mRecNO)
    17. sp_updateRec.Parameters.Append objParam
    18.  
    19. sp_updateRec.Parameters.Append sp_updateRec.CreateParameter("@First_Name", adVarChar, adParamInput, 50, Text1.Text)
    20. sp_updateRec.Parameters.Append sp_updateRec.CreateParameter("@Middle_Name", adVarChar, adParamInput, 50, Text2.Text)
    21. sp_updateRec.Parameters.Append sp_updateRec.CreateParameter("@Last_Name", adVarChar, adParamInput, 50, Text3.Text)
    22.  
    23. sp_updateRec.Execute
    24.  
    25. db.Close
    26. Set db = Nothing
    27. Set sp_addnew = Nothing
    28.  
    29. End Function

  2. #2
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: How to update record using stored procedure & vb6?

    Hello coolwater,
    VB Code:
    1. Private Function UpdateRecordset(mRecNO As Long)
    2.  
    3. Dim [B]sp_updateRec[/B] As ADODB.Command
    4. Dim db As New ADODB.Connection
    5. Dim objParam As [B][COLOR=Blue]Parameter[/COLOR][/B]
    6.  
    7. db.Open myDB ' I guess this is your connection string etc...
    8. db.CursorLocation = adUseClient
    9. Set sp_updateRec = New ADODB.Command
    10. [B]sp_updateRec[/B].ActiveConnection = db
    11. [B]sp_updateRec[/B].CommandType = adCmdStoredProc
    12. [B]sp_updateRec[/B].CommandText = "sp_update"
    13.  
    14.  
    15. Set objParam = [B]sp_updateRec[/B].CreateParameter("TestID", adBigInt, adParamInput, 8, mRecNO)
    16. [B]sp_updateRec[/B].Parameters.Append objParam
    17.  
    18. [B]sp_updateRec[/B].Parameters.Append [B]sp_updateRec[/B].CreateParameter("@First_Name", adVarChar, adParamInput, 50, Text1.Text)
    19. [B]sp_updateRec[/B].Parameters.Append [B]sp_updateRec[/B].CreateParameter("@Middle_Name", adVarChar, adParamInput, 50, Text2.Text)
    20. [B]sp_updateRec[/B].Parameters.Append [B]sp_updateRec[/B].CreateParameter("@Last_Name", adVarChar, adParamInput, 50, Text3.Text)
    21.  
    22. [B]sp_updateRec[/B].Execute
    23.  
    24. db.Close
    25. Set db = Nothing
    26.  
    27. Set sp_updateRec = Nothing
    28.  
    29. End Function

    I made some small changes to your code and tested it and it works!!

    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

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