Form Inserts due to Allow Additions property

radmanmm

New member
Local time
Today, 16:49
Joined
Nov 14, 2006
Messages
5
Hi All,

I have been searching and can't seem to find how to solve my problem. I have a sub form that allows adding new rows to a database. The detail contains a combobox and a few text fields. When I enter data everything works fine. However when I accidently tab into the next row (new blank row) and then try to get out of the form without adding a new row I get an error about "Index or Primary Key cannot be null" or something like that.

It is very frustrating because I can't exit the form until I add data to the new row and then leave without tabbing to much (thus creating another new row with no data).

How do I trap the new row and only allow the insert if it has data in it?

Thanks.
 
because, you are in the new row, and presumably some of your fields have default values, access immediately creates the new record (if you have record selectors, you will see the black triangle changes to a pencil.

when you close the form, this record tries to save, and fails, because something else on the record is incomplete.

in the forms close event, test if you are in a newrecord, and if so then determine whether you want to save it or not. if not do a application.runcommand accmdundo, which should cancel the changes and allow your form to close gracefully. This event will occur whether you close with the close x , or close by clicking a close button.

however if you try to change focus to another form without closing this one, you will still get the same error, so you might also need to trap the error in the on-error event, and possibly handle the situation there.

One other thing you could do is change the subform properties (cycle - its on the other tab) to current record instead of all records. this will prevent the tab moving to the new record (or indeed off the current record)
 

Users who are viewing this thread

Back
Top Bottom