Question : Begin Trans.Commit Trans.Rollback Trans

Hello,
This problem is with access VBA.
My question is,I have 2 tables Customer and Address.
Address table has a foreign key from customer table.
one customer can have many address
Using Transaction i want  to insert into customer and for that customer i want to insert the address.
if anything goes wrong with the address table  i want to roll back or clear everything in both tables .
Plz help me
Thank you

Answer : Begin Trans.Commit Trans.Rollback Trans

try this:


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
begin transaction

declare @NewId int

begin transaction

INSERT INTO [CUSTOMER_TABLE] (Name) VALUES ('sam')
set @NewId = @@Identity

if @@ROWCOUNT = 0 begin -- if no rows were inserted 
        rollback transaction -- rollback 
        return -- stop doing stuff
end
    
insert into [Address_TABLE]  (Cust_ID,Addr_Line1) VALUES (@NewId,'Test St')
if @@ROWCOUNT = 0 begin -- if no rows were inserted 
        rollback transaction -- rollback 
        return -- stop doing stuff
end

commit transaction -- save changes
Random Solutions  
 
programming4us programming4us