473,657 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AfterInsert Event not Triggered as Expected on Pasted Records

132 New Member
Hi all!!
I found my in position to ask for your assistance!
I have a form(frmTable2, in DatasheetView) set to Append NewRecords to other tables and Update the Oldrecords as might be required.
On the AfterInsert Event I have the following Code!
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblMytable ( ID, Code, SName, IDNumber, DateOfBirth)
  2. SELECT tblTable1.ID, tblTable1.Code,tblTable2.SName, tblTable2.IDNumber, tblTable2.DateOfBirth, 
  3. FROM (tblDepartment INNER JOIN tblTable1 ON tblDepartment.DeptID = tblTable1.DeptID) INNER JOIN 
  4. tblTable2 ON tblDepartment.DeptID = tblTable2.DeptID
  5. WHERE (((tblTable2.[SID])=[Forms]![frmTable2]![SID]));
AfterUpdate Event set the following Code
Expand|Select|Wrap|Line Numbers
  1. UPDATE tblDepartment INNER JOIN (tblTable2 INNER JOIN (tblTable1 INNER JOIN tblMytable ON 
  2. tblTable1.Code = tblMytable.Code) ON tblTable2.[SID] = tblMytable.[SID]) ON (tblDepartment.DeptID = tblTable2.DeptID) 
  3. AND (tblDepartment.DeptID = tblTable1.DeptID) SET tblMytable.[SName] = [tblTable2].[SName], tblMytable.[IDNumber] = [tblTable2].[IDNumber], 
  4. WHERE (((tblMytable.[SID])=[Forms]![frmTable2]![SID]));
They are both works fine if you enter the data one by one!
AfterInsert Event Never triggered if you decide to paste the data(form on DatasheetView) instead of typing one record after another.
Is there any means of Iterating all fields so that to force the AfterInsert Event, even if you PASTE the data to the forms???
Aug 27 '11 #1
7 5568
Stewart Ross
2,545 Recognized Expert Moderator Specialist
AfterInsert is only triggered when you manually insert records. Pasting records, as you have found, does not trigger this event.

Without knowing why you are pasting records it is difficult to advise you on this. There is no event I know of which you can pick up upon to advise that you pasted records, although other posters may have found a different approach.

In any case, I would advise that it is better to avoid manual cut-and-paste when you want to have additional control. Provide some means by which your users can select records to copy, then use a fully-programmed approach to copying and appending your records - allowing you to take whatever actions are necessary under program control at the time.

-Stewart
Aug 27 '11 #2
NeoPa
32,568 Recognized Expert Moderator MVP
What a thoroughly confusing thread. It starts with a post saying the code in the AfterInsert event procedure is made up of two bits of SQL code, but that's impossible as event procedures must be VBA. It then goes on to say that the AfterInsert event isn't triggered when multiple records are pasted in, then strangest of all, Stewart agrees this is the case.

I don't know what's different between what you guys are seeing and what I have here, except maybe the Access version (I'm testing with 2003), but when I tried to duplicate this (very basic pasting into a table of multiple records from the clipboard) it worked fine for me. Each record inserted triggered both the Form_AfterUpdat e, as well as the Form_AfterInser t event procedures and both procedures had full access to the appropriate single record being processed. I'm testing in Datasheet view, just as stated quite clearly in the question. Obviously, I'm assuming the AfterInsert event being asked about is the Form_AfterInser t rather than one for any of the individual controls (which wasn't quite so clearly stated in the question) as I'm pretty sure that's the only one that would make sense in this scenario.

Normally I would just assume that neither of you had looked into it clearly enough, but as one of you is Stewart I don't feel that would be too bright an assumption on my part. We all make mistakes sometimes of course, but I certainly wouldn't be comfortable assuming that is the case just because I don't understand (yet) how it might not be.
Aug 27 '11 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi All. Well, I 'fess up - sometimes I get it wrong (I was thinking of VBA-based insertion here). AfterInsert is indeed triggered by a paste (which it would not be if you used a programmed approach).

Apologies for my error.

-Stewart
Aug 27 '11 #4
Stewart Ross
2,545 Recognized Expert Moderator Specialist
I also confirm after testing that A2007 behaves identically to A2003 as tested by NeoPa(as expected). The after update event for individual controls and the after insert event for the form itself is triggered normally by a paste append in datasheet view.

Making amends for my mistake earlier, I can see that the actual problem that is occurring results from the WHERE clauses of the two event procedures:

Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblTable2.[SID])=[Forms]![frmTable2]![SID]));
The reference to [Forms]![frmTable2]![SID] will not work for a group of pasted records. It will be referring to the first of the records in the group only. When you paste the records the first of these is taken by Access to be the Current Record (the one which has focus, and therefore the record whose values will be returned when controls are being referred to in expressions).

Although the events ARE being fired, the code CANNOT refer to each record in sequence unless there is some value in a field somewhere that was common to all and could be used to identify the group of records pasted.

The On Current event is fired twice for a paste append of records, once before the paste (on the new record row in effect) and once after (on the same row with the new contents). It does not fire at all as the paste takes place until all the records have been pasted, although the individual After Update and After Insert events do.

Moral of this one for me: never assume, test it first!

-Stewart
Aug 28 '11 #5
NeoPa
32,568 Recognized Expert Moderator MVP
Stewart's human after all :-D

Good job with the explanation too.

There was another thread posted by the same OP at a similar time which, though different and correctly posted separately, seems to be related to the same issue. I suspect the issue here is very similar to the one there too (which I guessed as being related to the updates all being completed before any updated data from the table is available).

There may well be an element (It's hard to know from what's been posted to be fair) of the logic for each record expecting and requiring the updates from the previously added records to be reflected in the data to work correctly. I hope that makes sense. The other thread, for interest, is DMax Function Not Responding on Pasted Records.
Aug 28 '11 #6
Mr Key
132 New Member
As Stewart explains, if you paste a single record, the AfterInsert event is trigered but if you paste multiple records at once only the first record is Appended to the Output table but the rest remains on the original table unnoticed!
In this design I set tblTable2.[SID]=[Forms]![frmTable2]![SID] to restrict record with focus only to be Appended to other table.
The reason for this is to prevent duplicates of data to the output table.
The AfterUpdate Event is set to allow users to be able to edit the records as they wish.
I have to have both AfterInsert and AfterUpdate on the same form.
As NEOPA point out, I face the same problem on the DMax functions on the thread Dmax Function Not Responding on Pasted Records
Please help!!!
Aug 28 '11 #7
Stewart Ross
2,545 Recognized Expert Moderator Specialist
You could try moving to the next new record after completion of your AfterInsert actions, by adding the following lines at the very end of your AfterInsert routine, replacing [Your First Control Name] with the real name of the control that should have focus after you insert a record:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.GoToRecord , , acNewRec
  2. Me![Your First Control Name].Setfocus
Although I have not for test purposes run a SQL update as you do, the AfterInsert tests I did successfully referred to each of the pasted records one after the other when the DoCmd.GotoRecor d was included. The SetFocus was required to maintain the normal operation of the standard insert if you were manually entering a record instead of pasting groups of records.

Please note that these lines must be placed at the end of the sub just before the normal exit of the routine. This approach will only work if you do the move to the new record as the very last action undertaken by your AfterInsert routine.

-Stewart
Aug 28 '11 #8

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

Similar topics

1
4754
by: andree | last post by:
Hello, I have a form where a user may paste multiple records. The form has an AfterInsert procedure. The procedure copies the newly inserted record into a different table for audit purposes. When one record is pasted, all is working fine. If multiple records are pasted, the AfterInsert event gets fired before the Paste Confirmation Dialog Box, therefore no records are inserted into my audit table.
1
6106
by: Mark | last post by:
This question refers to a main form with a continuous form subform. After an error occurs after entering several records in the subform, how can I delete all the data in the main form and all the records in the subform? I have tried undoing both the main form and the subform and I have tried deleting the record in the main form. Thanks! Mark
2
1686
by: Support | last post by:
Is it possible to add a record in one table and have Access automatically add date from one of the fields into another table?? In more detail: Table1 has the following fields: Product_ID, Price, Weight etc... Table2 has the following: Product_ID, Category, Department I need to add a new product into Table1 and automatically have the product_id added to Table2.
1
1602
by: rigasp | last post by:
Hello, is there an event I can call when moving between the records of a form (either from the navigation buttons or by pressing down from the keyboard? Thankx
3
1757
by: celineusa | last post by:
Hello! Here is part of my database: tblStudents: StudentID tblMajorsAndStudents: StudentID, MajorID tblMajors: MajorID tblMajorsAndClasses: MajorID, ClassID, QuarterTaken tblClasses: ClassID tblStudentsAndClasses: StudentID, ClassID
0
7848
by: Dzemal Tipura | last post by:
I populate dataset using this code Me.KorisniciTableAdapter.FillByID(Me.DsKorisnici.Korisnici, IDKorisnika) columns from this dataset are bounded to controls on the form and I want to save changes back to database with this code: KorisniciBindingSource.EndEdit() KorisniciTableAdapter.Update(DsKorisnici.Korisnici) and I get error mentioned in subject.Why?
4
1468
by: Ron | last post by:
Hi all, I've got a frmCustomer form (designed via access 2000 form wizard) that uses a qryCustomerName query (also designed by query wizard) ordered by customer last name, first name, mi. Of course, it adds records to the end of the dataset rather than putting new entries into alpha order. And, there they sit until I leave the form and re-enter it. Is there a way to input the new record so that once it's entered it pops it into...
2
4247
rsmccli
by: rsmccli | last post by:
Access 2002 Hi, I have a number of comboboxes/textboxes in the header of a form that the user selects values from. These comboboxes/textboxes correspond to controls in a datasheet view subform. When a new record is created in the subform I want these values to automatically write to the new record in the subform. Right now I have this autofill procedure running on the AfterInsert event of the subform. It works well, except for the fact...
2
2360
by: phill86 | last post by:
Hi, i have a recordset that updates a table which works fine when you add, update and paste a single record but if you try and paste multiple records it will only update the table with the prevoius records and it will ignore the new pasted records i am running the following code on the afterupdate event in the form Please Help Dim dbsCurrent As Database Dim dbsLinkedData As Database
0
1742
by: virtualKing | last post by:
this is my code for update, please why the error public void Modificar_HuespedV(int no_orden, int nno_orden, string nombre, string apellidos, double ci, int no_habitacion, string fecha_entrada, string fecha_salida, int cant_acompanantes, int dias_hospedado, int cuota_persona, int total_cobrar, int no_cobrado, string fecha_cobrado) { int pos = 0; for (int i = 0; i <...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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
8605
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
6166
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
5632
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
4155
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
2726
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
1955
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.