After Insert not firing on subform

M

medirate

I have a main form (Employees) and a subform (Accidents). They are linked by
EmployeeID.



On the subform, there is a numeric field, ClaimNumber, which I want to auto
populate. So on the After Insert property for Accidents, I have code like
this:



lngCounter = ahtGetCounter() 'get a new claim number from the
counters table and increment it

Me.ClaimNumber = lngCounter ' assign it as out claim number.



This works fine if I already have an employee in the database. But if I'm
adding a new employee, and then move to the subform to add a new accident,
the ClaimNumber field is not updated because the After Insert event is never
called (I placed a breakpoint in the After Insert but the program never
stopped).



In other words, if the employee exists in the main form, then adding a new
accident is no problem and the claimnumber field auto populates. But if the
employee does not exist in the main form then it will not work because the
code to execute the auto populate function (ahGetCounter) never gets
called.



Where should I place the code so that ClaimNumber is autopopulated?

I am using Access 2002 with an Access 2000 mdb. The ClaimNumber is always
zero until it gets a new value from the counters table.



Thanks!
 
G

Guest

I am guessing that the problem is that the new employee edit has not yet been
committed when you move to the subform.

On your "After Insert" event on the Accidents subform, add this line at the
beginning of the code:

Me.Parent.Dirty = False

That will cause the data just entered to be committed and available to the
subform.
 
G

Graham Mandeno

That is puzzling! When you shift focus from the main form to a subform, the
record bound to the main form is automatically saved, so the subform should
behave the same way whether or not the main form is displaying a new
(recently added) record.

I suggest you try doing a compact and repair on the database.

You might also try using the form's BeforeUpdate event instead of
AfterInsert:

If Me.ClaimNumber = 0 Then Me.ClaimNumber = ahtGetCounter()

The AfterInsert event fires after the record has been saved, so effectively
you are then dirtying the record again and requiring it to be saved a second
time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top