473,789 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Jun 27 '08 #1
5 2247
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 #2
Ben (pi******@sbcgl obal.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****@sommarsk og.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 #3
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'The re 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 #4
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******@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


Jun 27 '08 #5
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.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


Jun 27 '08 #6

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

Similar topics

3
3663
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 action="form.py/email" method="POST"> Name: <input type="text" name="name"><br> Email: <input type="text" name="email"><br> Comment: <textarea name="comment" rows=4 cols=20></textarea><br> <input type="submit"> </form>
1
2097
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. I need to add these objects back into the databases, however I'm worried that I will have problems with referential integrity since there is a chance someone may have deleted or updated a record that should have cascaded changes to other tables...
2
3417
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 login/password is dbUser/dbUser. the web app however, is using windows authentication. so if I am logged into the network as 'DOMAIN\Eric', when I access my web app, my web app knows that I am 'DOMAIN\Eric'. but to the sql server db, I am user...
1
3348
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. ... www.soft30.com/download-1-11975.htm - 31k - Cached - Similar pages MDBSecure 1.0.8.0 - Soft30.com Utility which makes it easy to create secure MS Access Databases, ... MS Access 2000/2003 format. 30 day money back guarantee, 30 day trial. ...
52
9989
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 2005, and, since he already has licenses for Office Pro 2002, he wants to upgrade to that. I've been saying that we need to upgrade to Access 2003, not 2002, even if Office is kept at 2002. We are also looking to do a fair amount of...
1
17897
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 + "-" + x509Token.Certificate.GetName(); }
7
10165
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. The properties look the same whether I use XP or 2003 versions of Office. If I take an earlier version of Access and link the image, it puts the image. I can also see the image in 2003. If I link the image using 2003 it only shows the filename....
6
2053
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 your business layer and/or data access layer? suppose in my data access layer, I provide try and catch, log the exception and re-throw it back to business layer, then in yoru business layer, what do you do? throw it back to the code behind or...
2
2015
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 what I'm assuming since they get no error while working. If I make a copy of the database prior to the last user logging out, that copied DB remains undamaged. If I attempt to log back the original DB in once everyone is out, I get the dreaded...
4
5746
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
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.