473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Handle Triggers in MS Access 2003 with SQL Server as Back-End

Ben
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.trTrackInsert

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.NewCustTracker table

SET @recordCount = (SELECT count(*) FROM dbo.NewCustTracker);

BEGIN TRANSACTION insertIntoNewCustTracker

IF (@recordCount 0)

BEGIN

-- get the info in the NewCustTracker table...

SET @userName = (SELECT UserName FROM dbo.NewCustTracker);

RAISERROR(N'There is a pending new customer record to be completed by %s.
Please recheck in a couple of minutes.',16,1,@userName);

ROLLBACK TRANSACTION insertIntoNewCustTracker;

END

ELSE

BEGIN

-- record the new customer record in the NewCustTracker table for next
validation...

INSERT INTO dbo.NewCustTracker(CustNum, UserName)

SELECT [Customer Number], user_name() FROM inserted;

IF @@TranCount 0

COMMIT TRANSACTION insertIntoNewCustTracker;

END

END

GO

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -

Any ideas/suggestions are appreciated.

Thanks,

Ben


Jun 27 '08 #1
8 2078
As I understand it, from reading numerous threads over the years, triggers
are not allowed/cannot be used in any version of Access.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200804/1

Jun 27 '08 #2
Ben
Below is our software setup:

- Front-End MS Access
- Back-End MS SQL Server

The script has been created in the back-end
"Linq Adams via AccessMonster.com" <u28780@uwewrote in message
news:8296d31df70e0@uwe...
As I understand it, from reading numerous threads over the years, triggers
are not allowed/cannot be used in any version of Access.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200804/1

Jun 27 '08 #3
Ben wrote:
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:
I don't believe any "result" would be usable by Access other than if you
raise an error.

What are you expecting or desiring to have happen?

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Jun 27 '08 #4
On Sat, 12 Apr 2008 22:37:48 GMT, "Ben" <pi******@sbcglobal.net>
wrote:

I didn't test this, but I would think the RAISEERROR would generate a
trappable runtime error in your Access app. Depending on the exact
details of your app, it could be trapped in the Form_Error event, or
if your form is unbound or the record is inserted using ADO or
similar, in that procedure.

-Tom.
>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.trTrackInsert

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.NewCustTracker table

SET @recordCount = (SELECT count(*) FROM dbo.NewCustTracker);

BEGIN TRANSACTION insertIntoNewCustTracker

IF (@recordCount 0)

BEGIN

-- get the info in the NewCustTracker table...

SET @userName = (SELECT UserName FROM dbo.NewCustTracker);

RAISERROR(N'There is a pending new customer record to be completed by %s.
Please recheck in a couple of minutes.',16,1,@userName);

ROLLBACK TRANSACTION insertIntoNewCustTracker;

END

ELSE

BEGIN

-- record the new customer record in the NewCustTracker table for next
validation...

INSERT INTO dbo.NewCustTracker(CustNum, UserName)

SELECT [Customer Number], user_name() FROM inserted;

IF @@TranCount 0

COMMIT TRANSACTION insertIntoNewCustTracker;

END

END

GO

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - -

Any ideas/suggestions are appreciated.

Thanks,

Ben


Jun 27 '08 #5
Ben (pi******@sbcglobal.net) writes:
>
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
I'm not really sure what you are asking, but if you plan to return
to result sets from triggers, think twice. Microsoft has deprecated
this and plan to remove it in a later version.

Error messages are of course a fair game. How you best handle them in
your Access application, I leave to other to answer.
Another note: you have BEGIN and COMMIT in the trigger. This is a
little overkill, because a trigger always runs in the context of
the statement that fired the trigger. Furthermore, if there is an
error or a rollback in the trigger, this aborts the batch and rolls
back the transaction entirely.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #6
Ben wrote:
-- 1. Check if there is already a record in the NewCustTracker
-- 1.2. If there is record
-- 1.2.2 Perform roll back of the insert in the Customers table
RAISERROR(N'There is a pending new customer record to be completed by %s.
Please recheck in a couple of minutes.',16,1,@userName);
Out of curiosity, why do you need to serialize this process?
Jun 27 '08 #7
Ben
Update on this issue. I have finished my changes to the front-end and
back-end to make it work.

I did the following:
a. Created the insert and update triggers at the back-end
b. Modified my stored procedure to capture the user-defined error message I
created for this task
c.
"Ben" <pi******@sbcglobal.netwrote in message
news:0R***************@newssvr29.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.trTrackInsert

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.NewCustTracker table

SET @recordCount = (SELECT count(*) FROM dbo.NewCustTracker);

BEGIN TRANSACTION insertIntoNewCustTracker

IF (@recordCount 0)

BEGIN

-- get the info in the NewCustTracker table...

SET @userName = (SELECT UserName FROM dbo.NewCustTracker);

RAISERROR(N'There is a pending new customer record to be completed by %s.
Please recheck in a couple of minutes.',16,1,@userName);

ROLLBACK TRANSACTION insertIntoNewCustTracker;

END

ELSE

BEGIN

-- record the new customer record in the NewCustTracker table for next
validation...

INSERT INTO dbo.NewCustTracker(CustNum, UserName)

SELECT [Customer Number], user_name() FROM inserted;

IF @@TranCount 0

COMMIT TRANSACTION insertIntoNewCustTracker;

END

END

GO

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -

Any ideas/suggestions are appreciated.

Thanks,

Ben


Jun 27 '08 #8
Ben
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.NewCustTracker);

BEGIN TRANSACTION insertIntoNewCustTracker

IF (@recordCount 0)

BEGIN

SET @userName = (SELECT UserName FROM dbo.NewCustTracker);

RAISERROR(70000,16,1,@userName) WITH SETERROR;

END

ELSE

BEGIN

INSERT INTO dbo.NewCustTracker(CustNum, UserName)

SELECT [Customer Number], user_name() FROM inserted;

IF @@TranCount 0

COMMIT TRANSACTION insertIntoNewCustTracker;

END

END
[UPDATE TRIGGER SCRIPT]
--------------------------------------------
CREATE TRIGGER [trUpdateCustomer]

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.NewCustTracker WHERE [CustNum] =
@custNumber)

IF (@recCount 0)

BEGIN

IF (UPDATE([Last Name]) OR UPDATE(Company))

DELETE FROM dbo.NewCustTracker;

END

END
[STORED PROCEDURE SCRIPT]
--------------------------------------------------
CREATE PROCEDURE [dbo].[sp_InsertNewCustomer]

@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
.ActiveConnection = <your connection string here...>
.CommandText = "sp_InsertNewCustomer"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters("@NewCustomerID") = NextCustID
.Execute
End With

errorNumber = 0

If Not IsNull(com.Parameters.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 "Encountered 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******@sbcglobal.netwrote in message
news:0R***************@newssvr29.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.trTrackInsert

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.NewCustTracker table

SET @recordCount = (SELECT count(*) FROM dbo.NewCustTracker);

BEGIN TRANSACTION insertIntoNewCustTracker

IF (@recordCount 0)

BEGIN

-- get the info in the NewCustTracker table...

SET @userName = (SELECT UserName FROM dbo.NewCustTracker);

RAISERROR(N'There is a pending new customer record to be completed by %s.
Please recheck in a couple of minutes.',16,1,@userName);

ROLLBACK TRANSACTION insertIntoNewCustTracker;

END

ELSE

BEGIN

-- record the new customer record in the NewCustTracker table for next
validation...

INSERT INTO dbo.NewCustTracker(CustNum, UserName)

SELECT [Customer Number], user_name() FROM inserted;

IF @@TranCount 0

COMMIT TRANSACTION insertIntoNewCustTracker;

END

END

GO

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -

Any ideas/suggestions are appreciated.

Thanks,

Ben


Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nancy | last post by:
Hi, Guys, I am new to Python. I am trying to following the example on http://www.modpython.org/live/current/doc-html/tut-pub.html In this example, it gives the following html code, <form...
1
by: jason_s_ford | last post by:
I have several sql server databases that were recently moved to a new server. In the process of migrating the databases, any triggers and constraints attached to tables were removed on accident. ...
2
by: ecastillo | last post by:
i'm in a bit of a bind at work. if anyone could help, i'd greatly appreciate it. i have a web app connecting to a sql server using sql server authentication. let's say, for example, my...
1
by: com | last post by:
Extreme Web Reports 2005 - Soft30.com The wizard scans the specified MS Access database and records information such as report names, parameters and subqueries. ......
52
by: Neil | last post by:
We are running an Access 2000 MDB with a SQL 7 back end. Our network guy is upgrading to Windows Server 2003 and wants to upgrade Office and SQL Server at the same time. We're moving to SQL Server...
1
by: Gabe Garza | last post by:
In the following code snippet: Microsoft.Web.Services.Security.Signature sig; try { sig = new Signature(x509Token); } catch(Exception ex) { txtStatus.Text = ex.Message + "-" +...
7
by: needin4mation | last post by:
Hi, I have an Access 2002 - 2003 database. I am using Access 2003. Whenever I link an image all it shows is the filename. Not the image. Other versions of Access can link the image just fine. ...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
2
by: suzanne | last post by:
I have a database that had been stable until 3 weeks ago. The Access 2003 database occasionally (a couple of times a week) gets corrupted when the last user exits the application. At least, that's...
4
by: --CELKO-- | last post by:
I need to convert a bunch of DB2 triggers to Oracle. Is there any kind of tools for this?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.