Previous Thread

8/17/2006 7:00:02 AM    Call SP from command button
Hello using ADP, I have a form that takes user input in various text boxes 
 
that are all unbound (not tied to rowsources) because the inserts need to be 
 
dynamic. There is a checkbock that determines how the record(s) will be 
 
inserted. (unchecked causes single table insert, checked performs a 2 table 
 
insert) How do i code a command button's onclick event procedure to call a 
 
store procedure to do this? 
 
How should I code the SP to perform the double inserts (into different 
 
tables) pulling the autonumer from the first insert to populate a field in 
 
the second insert? Thank you.



8/17/2006 7:21:26 AM    Re: Call SP from command button
Simply use ADO Command object,  which passes the parameters, to execute 
 
whatever SP you choose. Here is pseudo-code: 
 
Private Sub CommandButton1_Click() 
 
Dim cmd As ADO.Command 
 
Dim pmt As ADO.Parameter 
 
Set cmd=New ADO.Command 
 
Set cmd.ActiveConnection=CurrentProject.Connection 
 
cmd.CommandType=adCmdStoredProc 
 
cmd.CommandText="MySPName" 
 
''Create Paramters required by the SP 
 
Set pmt=cmd.CreateParameter(....) 
 
cmd.Parameters.Append pmt 
 
''More parameters here 
 
''Execute the SP 
 
cmd.Execute 
 
End 
 
As for performing double insertion i a SP, it should be farly easy, like: 
 
Create Procedure "MySPForInsertion" 
 
( 
 
@Para1 varchar(50), 
 
@Para2 int, 
 
... 
 
) 
 
AS 
 
DECLARE @ID int 
 
/*Assume Table1 has a Identity column*/ 
 
INSERT INTO Table1 (Col1,Col2,...)Values (@Para1,@Para2,...) 
 
SET @ID=SCOPE_IDENTITY() 
 
/*Insert to Table2 
 
INSERT INTO Table2 (ParentIDCol,Col1,Col2,...) VALUES 
 
(@ID,'Value1','Value2',...) 
 
RETURN 
 
You can also return the auto-generated ID back to ADO.Command object by 
 
using output parameter, so  the Command object will execute the SP and get 
 
the new ID back in one round trip to the sql server. 
 
"Steven" <Steven@discussions.microsoft.com> wrote in message 
 
news:6337C748-16FF-49F2-A037-D6A982E2597C@microsoft.com...

8/17/2006 9:14:28 AM    Re: Call SP from command button
If you don't need a result/recordset, you can simply call the sp as though 
 
it were a method on the access connection, like this: 
 
Access.CurrentProject.AccessConnection.p_thisIsMyProc(somearg, anotherarg) 
 
(the above assumes your stored proc is called 'p_thisIsMyProc' and takes two parameters). 
 
Assuming you want a single stored proc to perform the two inserts sequentially: 
 
you will want to use the SCOPE_IDENTITY function to retrieve the value of the IDENTITY column from the first insert to use it in the 
 
2nd insert.  Look it up in SQL Server Books on-line (BOL).