423,688 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

How to trigger an event when a table is opened or a field updated?

beacon
100+
P: 579
Hi everybody,

[Access 2003]

I created a database about a year ago that stores information about requests that users make to update reports. The fields in the database are updated using a Microsoft Outlook form. Basically, the user opens the Outlook form, fills in the info, sends it to an email group, the data is entered in the database, and the email is received by the members of the email group.

Usually, the Outlook form works just fine, but there have been few occasions when the email will get hung up in the user's Outbox. Strangely enough, the code behind the Outlook form still processes and enters the data into the database, but the email is sent to the members of the email group, which somewhat defeats the purpose of the database since it's used to notify the members of the group that a request has been submitted.

At some point in the future I plan on overhauling this whole process and using something a little more stable, but for the time being I've decided to make due with what I've got by making what I'm hoping are a couple of minor changes.

What I'm trying to do is setup a module or macro that will send an email to the email group whenever the table is opened or when certain fields are opened in that table (there's only one table...it's a simple database).

My problem is that I don't know how to trigger an event that monitors the opening of a table or the changing of data in a specific field on that table. I'm actually not even sure this is possible since the database is likely only open for a very short amount of time between when the email form is sent and the data is processed.

This database really isn't kept open at all and primarily only has action when an email form is sent, so I think (I could be wrong) that would eliminate using a timer to check periodically.

Is this possible and is there some example code I can view to create my module/macro?

Thanks,
beacon
Oct 5 '11 #1
Share this Question
Share on Google+
10 Replies


Expert Mod 2.5K+
P: 2,545
Hi Beacon. There are no events associated with Access tables at all, so there is no automatic way I can think of by which you could detect a new row insertion or a field update in real time.

An alternative approach which might be worth considering is to generate a change log showing all insertion or modification activity that has taken place (with a timestamp for each change in the log along with the user ID and so on). You could set up your Outlook code to make an entry in the change log each time an insertion or field update takes place.

By monitoring the change log table at intervals through code you could track activity and send out notifications of new entries and changes as necessary.

This is just an idea rather than something fully-formed at present. Change logging is fairly straightforward to implement (although you'd need to double the number of tables you hold in the DB concerned from one to two!).

-Stewart
Oct 5 '11 #2

NeoPa
Expert Mod 15k+
P: 31,084
I'm afraid there are no events triggered in the course of opening table or query objects. Only Form and Report objects have that facility (sets of associated events).

Depending on exactly what happens within your Outlook code (You don't go into much detail there) it may be possible to be a little creative, but fundamentally there is nothing like what you ask for I'm afraid. Fields are never opened as such either, so that part is a no-go too.
Oct 5 '11 #3

beacon
100+
P: 579
@Stewart - The table I have already is setup basically like a change log. The problem is that I don't check it that often and I was hoping to avoid having to check it...opting instead to make it work for me by emailing me if the data changed. Lazy? Perhaps, but I don't like the idea of checking something daily if there may only be an update once or twice a month. Y'know?

@NeoPa
The code behind the Outlook form basically inputs the values on the form into the associated field using DAO.

Here's a snippet:
Expand|Select|Wrap|Line Numbers
  1. Function Item_Send()
  2.  
  3.     Set Dbe = Application.CreateObject("DAO.DBEngine.36")
  4.     Set MyDB = Dbe.Workspaces(0).OpenDatabase("C:\Tracking.mdb", False, False)
  5.     Set RS = MyDB.OpenRecordSet("tblRequest", DbOpenTable)
  6.  
  7.     'Add a new row to the database and add the data in the 
  8.     'form's fields to the corresponding fields in the database
  9.     '-----------------------------------------------------------
  10.     RS.AddNew
  11.     RS("ReportLocation") = Item.UserProperties("ReportLocation")
  12.     RS("RequestType") = Item.UserProperties("RequestType")
  13.     RS("RequestDate") = Item.UserProperties("RequestDate")
  14.     RS("TimeStamp") = Item.UserProperties("TimeStamp")
  15.     RS("RequestorFName") = Item.UserProperties("FirstName")
  16.     RS("RequestorLName") = Item.UserProperties("LastName")
  17.     RS("RequestorPhone") = Item.UserProperties("PhoneNumber")
  18.     RS("RequestReason") = Item.UserProperties("RequestReason")
  19.  
  20.     'Grab the unique ID from the database and 
  21.     'assign it to the AutoID field on the form
  22.     '-------------------------------------------
  23.     Item.UserProperties("AutoID").Value = RS("RequestID")
  24.  
  25.     'Update the database, move the cursor to the 
  26.     'last row in the database, and close the database
  27.     '--------------------------------------------------
  28.     RS.Update
  29.     RS.MoveLast
  30.     RS.Close
  31.     MyDB.Close
  32.  
This is probably not possible, but if I add a module with a subroutine that has code to email, can I call that subroutine from DAO?
Oct 5 '11 #4

Expert Mod 2.5K+
P: 2,545
As NeoPa and I have both mentioned, there are no events associated with tables. As there are no event listeners or handlers available, it is simply not possible for DAO to call a bespoke subroutine at all.

-Stewart
Oct 5 '11 #5

NeoPa
Expert Mod 15k+
P: 31,084
If the only time data is added to the table is within your own Outlook code then it should be easy enough to change the code such that, when any update occurs, it also notifies you that it has.

I think that's your best route to be fair.

PS. Don't worry. I think we are all lazy people looking to save time and energy by avoiding laborious tasks. That's my main reason for loving programming and logic. That and the fact that so many people simply can't do what I find relatively easy (Maybe that's a little conceit showing, I don't know).
Oct 5 '11 #6

Rabbit
Expert Mod 10K+
P: 12,272
So... I may be wrong here but it sounds like you're trying to fight fire with fire.

You have code that inserts data into a database and sends an e-mail to let people know something was inserted. The problem is that, from time to time, the e-mail goes into limbo and doesn't send. So what you want to do is send another e-mail, but based off a different trigger? Won't that run the risk of getting stuck in limbo as well?
Oct 5 '11 #7

beacon
100+
P: 579
@Stewart - Thanks...I just wanted to make sure I had turned over every rock.

@NeoPa & @Rabbit - The problem is that, based on our email setup, which I have no control over, the Outlook forms are somewhat finicky...despite best efforts, they are easy to one-off. So, instead of trying to get the Outlook form to send the notification like it's supposed to, but failing at occasionally, I was trying to see if there was a way to prompt the database to interface back with Outlook, outside of the Outlook form (using Redemption), to send a secondary email that I thought would be more stable than the other.

Maybe I can take Stewart's earlier response and tweak it so that it shows the last entry made on a given day, then write a script in VBScript and schedule it to open the database (that now has an AutoExec macro), check to see if the last entry in the main table matches the entry in the log, and if it's different, wait long enough for the module to run before closing the database. The log would then swap the last entry in the main table with the entry in the log (to keep it small...no reason to duplicate all the data) and the schedule would launch again the same time the next day.

Hopefully I could get it to run in the background once or twice a day without it interrupting other things I might be working on. Does that sound like a possible solution?

Thanks,
beacon
Oct 6 '11 #8

Rabbit
Expert Mod 10K+
P: 12,272
That's what I was going to suggest, however, instead of logging the last record sent, I would suggest an extra column.

[Sent] Boolean Default: False

Then you can use a query to send all the false records and run an update query to set them to true.

I'm looking into an alternative, I'll post back and let you know how it turns out.
Oct 6 '11 #9

NeoPa
Expert Mod 15k+
P: 31,084
You may find that Invoking a Database From the Command Line helps you to get something like that working. It may even give you some ideas as to what you might be able to achieve relatively straightforwardly.

I didn't realise this was what you were after earlier, otherwise I would have suggested it then.
Oct 6 '11 #10

Rabbit
Expert Mod 10K+
P: 12,272
I was trying to see if you can access a VBA function from an ADODB connection, which is what I assume you're using to run your update query. But it turns out you can't so that's a bust.

However, if it's just the e-mail code that's hanging, you could instantiate an Access object and run the VBA function in Access to send an e-mail.
Oct 6 '11 #11

Post your reply

Sign in to post your reply or Sign up for a free account.