473,326 Members | 2,095 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,326 software developers and data experts.

How to perform an action when a new record is created?

Seth Schrock
2,965 Expert 2GB
I have a feeling like I should know this, but I can't think of anything. I'm trying to create a file management portion to my database (which tracks loans) so that every document that is associated with a particular loan will be readily accessible. My plan is to create a folder for each record using the PK as the folder name so that it can be found relatively easily using Windows Explorer. I would like for the folder to be created when the record is created. However, I can't think of what to test for to know when the record gets created. I don't want to check for the folder's presense in the form's AfterUpdate event as that will trigger frequently and there is no need to do so each time. I'm looking more for a NewRecordModified property. I suppose that I could use the form's OnCurrent event to set a flag that the current record is a new record. Then in the form's AfterUpdate event I could test to see if the flag is set to NewRecord and then create the folder. However, that seems more complicated than it should be.
Mar 16 '13 #1
14 5559
zmbd
5,501 Expert Mod 4TB
Before insert event fires before the record is even inserted into the table.
After insert event fires after the record has been inserted into the table, yet all but the autonumber fields have been populated:
Order of events for database objects (V2003) (Sorry, just realized this is an older link; however, I've found most of the event order is still valid. I'll have to find a V2010 now :) ) In any case one of these events should help.
If you use the on current event then you should check if you are in a new record or an existing record.
Mar 16 '13 #2
Seth Schrock
2,965 Expert 2GB
The autonumber field displays the value even before the BeforeUpdate event of the form runs, so even if it hasn't been inserted into the table (which surprises me), I can still reference the control to get the value.

So you think that the flag idea is a good one?
Mar 16 '13 #3
zmbd
5,501 Expert Mod 4TB
I to think a tad on your case.
I would use the before update event.
Present a confirmation message to the user to confirm the changes.
You don't need a flag to check to see if the current record is a new record, you already have that in the form properties (frm.NewRecord) and if so then run the sub-code to create the required information and the folder etc... You should be able to refer the value in the control for the PK on the form at this point.

This way, if the user cancels or aborts then no new folder.
If the record is an existing record then your conditional wont run the code to make the folder etc....
Mar 16 '13 #4
TheSmileyCoder
2,322 Expert Mod 2GB
It might help if you consider the "insert" event as the part in which access reserves a row for your record. Its during the insert that access will reserve a autonumber for the record. Once the insert event has run, an autonumber will be available for use, and will not accidentally be used by someone else.
Mar 16 '13 #5
TheSmileyCoder
2,322 Expert Mod 2GB
I have a similar file system. Note that if the user wants to OPEN any stored document, I have code that copies the document to a working folder, so that the user never works on the original document. Of course whether this is relevant or not, depends a bit on whether you want updates to documents to be stored.
Mar 16 '13 #6
Seth Schrock
2,965 Expert 2GB
I do want changes to be allowed and saved to the server location. Most of the time the documents will be PDFs so that part won't be a factor, but the few doc files will need to be edited.

So the frm.NewRecord remains true until the record is first saved (between the before and after update events)? I didn't think about that being a possibility.
Mar 16 '13 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Me.NewRecord will return true when you are on a new record, that has not been saved first time yet. Me.NewRecord will also return true if the insertion has not yet started.
Mar 17 '13 #8
NeoPa
32,556 Expert Mod 16PB
Unlike most Before_ and After_ type events, Before_Insert and After_Insert do not come around a single event. Before_Insert happens at the point where a record is being prepared (and has become dirty), but also where the action is still possible to be canceled. After_Insert, on the other hand, occurs only after the new record has been saved and thus inserted into the table. It seems to me that the trigger you're looking for is this After_Insert event.
Mar 17 '13 #9
TheSmileyCoder
2,322 Expert Mod 2GB
Thanks NeoPa. It would seem I that event wrong in my head. I always assumed that the After_Insert would run immediately after the Before_Insert (provided it was not cancelled). I was even so sure I had to draw up a test form and test the order of those events to be sure, but you were absolutely right.

So AfterInsert is an event that only happens occurs for new records, AFTER they are saved. This is the order of events as tested:
(On dirty)
Form_BeforeInsert

(On save)
Form_BeforeUpdate
Form_AfterUpdate
Form_AfterInsert

and, as written above, the Insert events only occur for NEW records.


With that said, I would argue that the Before_Update would still be the event to use, since you might not wish to save the record if the filecopy fails for some reason.
Mar 17 '13 #10
zmbd
5,501 Expert Mod 4TB
From the link I posted in #2:
Creating a new record

When you move the focus to a new (blank) record on a form and then create a new record by typing in a control, the following sequence of events occurs:

Current (form)Enter (control)GotFocus (control)BeforeInsert (form)AfterInsert (form)

The BeforeUpdate and AfterUpdate events for the controls on the form and for the new record occur after the BeforeInsert event and before the AfterInsert event
I'm going to change my mind here and go with that AfterInsert event as I was originally trying to steer Seth to in my post.... frankly I was distracted by that drag-n-drop question! :)
Mar 17 '13 #11
NeoPa
32,556 Expert Mod 16PB
Smiley:
I always assumed that the After_Insert would run immediately after the Before_Insert (provided it was not cancelled).
Absolutely. Me too. It seems logical - but nevertheless isn't so :-( I was as surprised then as you are now.

I'd generally go with the _AfterInsert myself, but now the facts are all detailed and out in the open I'm sure whoever reads this can make their own minds up based on their particular requirements.
Mar 18 '13 #12
Seth Schrock
2,965 Expert 2GB
I have never played with the Before/After Insert events before. I guess I always assumed that they meant inserting a value into the control, but I gather from the conversation here that it is the inserting of the values into the table (saving the record). I will look into these events more. Thanks everyone for helping out with this.
Mar 18 '13 #13
NeoPa
32,556 Expert Mod 16PB
Actually Seth, the _BeforeInsert and _AfterInsert events come around different events entirely. The _BeforeInsert event is before the record buffer is prepared for entering a new record (not updating an existing one) and the _AfterInsert event occurs after (new record only) has been saved away.

Not only is _AfterInsert not guaranteed after a _BeforeInsert, but the operator gets control between these two events.
Mar 19 '13 #14
Seth Schrock
2,965 Expert 2GB
Well, at least I was right that I needed to look into them more :)
Mar 19 '13 #15

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

Similar topics

2
by: Patrick Fisher | last post by:
Hi To display a message when a user attempts to edit a record in a multi-user environment where a forms Record Locking is set to Edited Record and another user is editing a record, is difficult...
1
by: khan | last post by:
I want QTY on hand to be updated when record in detail sale is deleted. can any one help
3
by: CoX | last post by:
I am currently doing an assignment for my scripting module at univercity. The project is to create an auction site (I've just based mine on ebay) in asp.net using vb. One of the "extra mark"...
1
by: Robert A. Boudra | last post by:
As a newcomer to .Net I'm continually frustrated with the subtle and not so subtle changes that have been made since VB 6.0 that don't seem to server any real purpose other than to frustrate people...
2
by: Dave | last post by:
I am looking to see if there is an example or pseudo code for how to have a C# application read a text file and perform an action that is parsed from the text file. For example TestDoc.txt...
3
by: onnodb | last post by:
Hi all, In the past few weeks, I've been struggling with setting up an Access (XP) user interface to a MySQL database. So far, I seem to have been able to overcome most of the difficulties, but...
7
by: tapanreddy | last post by:
I am looking to perform an action when we close the window using the close tab at the top of the screen. I know how to do it using a close button but I was wondering if there is way to achieve the...
1
by: gavm360 | last post by:
Hello, I have a subform that has a command button to move to the next record. If there are more records i would just like to move to the next record, When the last record is reached I would like...
3
gcoaster
by: gcoaster | last post by:
Hello! I am having problem with DLookup Function DLookup looks on form, and finds the combobox = cboFullName and then compares it to the column clientID in tblCLIENTS table. if they match,...
0
by: azoapes | last post by:
Hi, I've made a Setup Project to my C# WinApp and added a custom dialog to it that asks for user/pass for a database server. Then I created a Custom Action class and override the Install() to check...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.