Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a front end (MS access) to call a stored procedure MS SQL Server that would insert or update records based on the parameters. The insert and update stored procedure are separate SP's and very straight forward statements involving the primary key as part of the where conditions in the update statement. The insert stored procedure runs smoothly but the update didn't change the records at all and does not produce any errors at all. When I tried to run through the debug it executes but no update in the server side. I tried to run the SP manually by setting up the parameters exactly the same as the one I have in the front end and it works perfectly fine. I been pulling up my hair on this and couldn't fine any solution, I think the problem is in my front end. I used the same function in access to call the SQL stored procedure. Anyone can please help me, truly appreciate it. The below are the sample codes:

On button click event,

..
SQL
XCmd "EXEC sp_Update @pk = " & Me.txtpk & ", @Value = " & Me.txtVal
..

below is my function in MS Access

Public Function XCmd(sSQL as String) as Boolean
   Dim Comm As ADODB.Command
   Dim lngRecordsAffected As Long

   If con.State = adStateClosed Then
      con.ConnectionString = conConnection
      con.Open
   End If

   Set Comm = New ADODB.Command
   With Comm
      .ActiveConnection = con
      .CommandText = sSQL
      .Execute lngRecordsAffected
   End With
   ExecuteMyCommand = True
End Function

and updae SP..

CREATE PROCEDURE [dbo].[sp_Update]
(
@pk int
,@Value varchar(30)
)
BEGIN TRY
    SET NOCOUNT ON;

    BEGIN TRANSACTION

       UPDATE [dbo].[Simpletbl]
       SET [FldValue] = @Value
       WHERE PKId = @pk

    IF @@ROWCOUNT = 0
       BEGIN
           RAISERROR('Cannot update!',16,1);
       END
       COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    BEGIN
        IF @@TRANCOUNT > 0
           BEGIN
              ROLLBACK TRANSACTION;
           END
    END;
END CATCH
Posted
Updated 29-May-15 10:09am
v3
Comments
ZurdoDev 29-May-15 14:36pm    
I'd suggest running SQL profiler so you can see exactly what Access is sending.
Putchicks 29-May-15 14:51pm    
Thank you very much for the prompt response RyanDev.
I cannot a run a trace (profiler) because I am not a member of sysadmin.
Richard Deeming 29-May-15 14:43pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Putchicks 29-May-15 15:04pm    
Thanks Richard Deeming for the suggestion, though this is not my issue right now and not a priority but will consider your suggestions. Do you happen to have a sample for us of parameterized query.
Richard Deeming 29-May-15 15:10pm    

1 solution

Try this :-

CREATE PROCEDURE [dbo].[sp_Update]
(
@pk INT,
@Value VARCHAR(30)
)
AS
BEGIN
SET NOCOUNT ON;

UPDATE [dbo].[Simpletbl]
SET [FldValue] = @Value
WHERE PKId = @pk
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900