c. At the front-end, MS Access, captured the error code and error message
using variant variables
This is the general overview of the things I did to make it work.
Below are the scripts:
[INSERT TRIGER SCRIPT]
----------------------------------------
CREATE TRIGGER [trTrackInsert]
ON [dbo].[Customers]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @recordCount int;
DECLARE @userName nvarchar(200);
SET @recordCount = 0;
SET @userName = '';
SET @recordCount = (SELECT count(*) FROM dbo.NewCustTrac ker);
BEGIN TRANSACTION insertIntoNewCu stTracker
IF (@recordCount 0)
BEGIN
SET @userName = (SELECT UserName FROM dbo.NewCustTrac ker);
RAISERROR(70000 ,16,1,@userName ) WITH SETERROR;
END
ELSE
BEGIN
INSERT INTO dbo.NewCustTrac ker(CustNum, UserName)
SELECT [Customer Number], user_name() FROM inserted;
IF @@TranCount 0
COMMIT TRANSACTION insertIntoNewCu stTracker;
END
END
[UPDATE TRIGGER SCRIPT]
--------------------------------------------
CREATE TRIGGER [trUpdateCustome r]
ON [dbo].[Customers]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @custNumber nvarchar(15);
DECLARE @recCount int;
DECLARE @lastName nvarchar(50);
DECLARE @company nvarchar(34);
SET @custNumber = (SELECT [Customer Number] FROM deleted)
SET @recCount = (SELECT count(*) FROM dbo.NewCustTrac ker WHERE [CustNum] =
@custNumber)
IF (@recCount 0)
BEGIN
IF (UPDATE([Last Name]) OR UPDATE(Company) )
DELETE FROM dbo.NewCustTrac ker;
END
END
[STORED PROCEDURE SCRIPT]
--------------------------------------------------
CREATE PROCEDURE [dbo].[sp_InsertNewCus tomer]
@NewCustomerID nvarchar(15),
@ErrNum int OUTPUT,
@ErrMsg nvarchar(4000) OUTPUT
AS
BEGIN TRY
INSERT INTO Customers ([Customer Number], Active, [Customer Record Type])
VALUES (@NewCustomerID , 1, 'Both')
END TRY
BEGIN CATCH
SELECT @ErrNum = ERROR_NUMBER(), @ErrMsg = ERROR_MESSAGE() ;
END CATCH
[MS ACCESS FRONT-END SCRIPT]
-----------------------------------------------------
....
....
Dim errorNumber As Variant, errorMessage As Variant
Dim errMsgNum As String
....
....
Set com = New ADODB.Command
With com
.ActiveConnecti on = <your connection string here...>
.CommandText = "sp_InsertNewCu stomer"
.CommandType = adCmdStoredProc
.Parameters.Ref resh
.Parameters("@N ewCustomerID") = NextCustID
.Execute
End With
errorNumber = 0
If Not IsNull(com.Para meters.Item(2). Value) Then
errorNumber = com.Parameters. Item(2).Value
errMsgNum = Str(errorNumber )
errorMessage = com.Parameters. Item(3).Value
Set com = Nothing
'Check if there is an error returned from the addition of a new
customer...
If errorNumber <0 Then
MsgBox "Encountere d error: " + errMsgNum + ". " + errorMessage,
_
vbOKOnly, "New Customer Warning!"
End If
End If
....
....
....
As we all know, there are other ways to solve the task even better ways.
But this serves my own purpose.
"Ben" <pi******@sbcgl obal.netwrote in message
news:0R******** *******@newssvr 29.news.prodigy .net...
Hi!
I have a trigger created for Customer table. My front-end is access.
What is the best approach to handle a trigger result when adding a new
customer record?
Below is the trigger script:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -
CREATE TRIGGER dbo.trTrackInse rt
ON dbo.Customers
FOR INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Validate the new record.
-- Criteria:
-- 1. Check if there is already a record in the NewCustTracker
-- 1.1. If no record add to the table and record the user info
-- 1.2. If there is record
-- 1.2.1 Inform user that there is pending new record to be completed
-- 1.2.2 Perform roll back of the insert in the Customers table
-- Initialize variables to use in getting some info in the NewCustTracker
and Customer
-- tables.
DECLARE @recordCount int;
DECLARE @userName nvarchar(200);
SET @recordCount = 0;
SET @userName = '';
-- get the record count in the dbo.NewCustTrac ker table
SET @recordCount = (SELECT count(*) FROM dbo.NewCustTrac ker);
BEGIN TRANSACTION insertIntoNewCu stTracker
IF (@recordCount 0)
BEGIN
-- get the info in the NewCustTracker table...
SET @userName = (SELECT UserName FROM dbo.NewCustTrac ker);
RAISERROR(N'The re is a pending new customer record to be completed by %s.
Please recheck in a couple of minutes.',16,1, @userName);
ROLLBACK TRANSACTION insertIntoNewCu stTracker;
END
ELSE
BEGIN
-- record the new customer record in the NewCustTracker table for next
validation...
INSERT INTO dbo.NewCustTrac ker(CustNum, UserName)
SELECT [Customer Number], user_name() FROM inserted;
IF @@TranCount 0
COMMIT TRANSACTION insertIntoNewCu stTracker;
END
END
GO
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -
Any ideas/suggestions are appreciated.
Thanks,
Ben