Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2005, 02:02 PM
Registered User
 
Join Date: Dec 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default RecordSets vs Stored Procedures in Access

I have an Access Project with a Microsoft SQL backend, in which I give the users several thousand records for browsing. My problems began when I took this advice to heart: "It is better to use ADO Recordsets only for retrieving data, and to use SQL stored procedures via ADO Command objects to perform insert, update, and delete operations."

I have three stored procedures: spGet, spAdd(parms), spUpdate(parms) corresponding to which I've made the Command objects: comGet, comAdd, comUpdate. I'm able to retrieve records into a recordset and use that recordset as a source for my form. I'm also able to invoke each command and change the backend table.

My problem is how to get the Add/Update stored procedures to mesh with the recordset approach? (RecordSet.AddNew and .Update methods seem clearly to clash with the stored procedure approach.)

I've been able to Add and Update by programmatically unbinding the controls in the form, gathering new values, then calling spAdd or spUpdate, then reinvoking spGet to retrieve several thousand records. But surely there must be a more efficient way. Below is some shortened code from my Form (I show only Add, but Update works similarly).

' initialize command objects and recordset, retrieve initial records
Private Sub Form_Load()
   With comAdd ' module variable for command object
      .ActiveConnection = CurrentProject.Connection
      .CommandType = adCmdStoredProc
      .CommandText = "spAdd"
      .Parameters.Append spAdd.CreateParameter("@name", adVarChar, adParamInput, 50)
   End With
       '-- SIMILAR CODE GOES HERE FOR comGet and comUpdate
   Set mRst = New ADODB.Recordset ' module level recordset
   mRst.Open comGet, , adOpenStatic ' retrieve data
   Set Me.Recordset = mRst ' link form to recordset
   Me.txtName.ControlSource = "Name" ' bind control
End Sub

' add button – unbind and blank control so new value can be gathered
Private Sub cmdAdd_Click()
   With Me.txtName
      .SetFocus
      .ControlSource = "" ' unbind control so I can assign a value
      .Text = ""
   End With
End Sub

' save button – call stored procedure and retrieve records
Private Sub cmdSave_Click()
   With comAdd
         .Parameters("@name") = Me.txtName
         .Execute , , adExecuteNoRecords
      End With
   End If
   Set mRst = comGet.Execute ' reexecute stored proc to retrieve records - again!!
   Set Me.Recordset = mRst ' relink to form recordset
   mRst.MoveLast
End Sub

As I say, this approach works, but I'm not happy having to retrieve all those records I already had everytime I make a single change to the data. Is there a better way to make stored procedures cooperate with recordsets?

Roy







Similar Threads
Thread Thread Starter Forum Replies Last Post
Recordsets and Stored Procedures Roy0 Access VBA 17 March 14th, 2007 04:32 AM
Stored Procedures returning no results to access 50bmg_de SQL Server 2000 5 February 21st, 2006 04:58 AM
Converting Access Queries Into Stored Procedures markw SQL Language 1 March 15th, 2005 11:49 AM
Using recordsets in Stored Procedures MaxGay SQL Server 2000 3 February 25th, 2005 11:38 AM
stored procedures and MS Access madhukp Classic ASP Basics 5 August 26th, 2004 12:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.