Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Trapping subform movement and new records

Trapping subform movement and new records

Trapping subform movement and new records


I have a main form and a subform based on two separate tables with a one-to-one relationship.  If my data entry person wants to create a new main record or move to the next main record while the focus is on the subform, the subform record advances and she gets the inevitable "duplicate value" error message for the primary key.

Is there an easy way to trap creation of a new record and/ or movement and set focus to the first field on the main form in this situation?  I can do this if someone tries to tab to the next record in the subform, but not if they use the Access "new record" button or navigation buttons.


RE: Trapping subform movement and new records

Hi Derek,

OK first the scolding..
a one to one rel and a main and sub form to boot...!

when i use a one to "optional" one rel i include it on the same form using antirestrection (include all records from master table and those that match in the dependent table in the query record source)and lock the dependent table data items when there is no master record ie the user has to add a new master record before adding stuff to the dependent table this avoids the situation you have.

you could add code in the subform on error event and trap the error, undo the addition then set focus to what ever field you want the user to goto if the condition occurs but this has long pointy sharp barbs on it, from a programming point of view (pun intented) try basing the form on a query of the two tables include the key field from both, put the dependent table key field on the form set this items visable property to No (looks neater), that way when the user goes to a new record and adds an entry to the master table the dependent table will get a new record if the user adds data and the dependent table will add a new record if the user adds data to any fields in the dependent table  

(its late here..)


RE: Trapping subform movement and new records

Thanks for this tip... I'll be doing a bit of database redesign, I guess.  Actually I oversimplified my original description.  I'm using separate tables so that I can someday assign different security parameters to each table.  The subforms appear on different tabs in the main form, the theory being that a person without permissions to the private information won't be able to successfully open that tab.

RE: Trapping subform movement and new records

Hi Derek,

OK why not try this

the single form has the advantage you cant add additional records like you can with a subform.

keep the arrangement as is, be prepaired to do some coding and error trapping!

set the subform record source to to a query that contains both the parent key and the child key and the child fields.

you may have to do some experimenting with this at first show all the fields (both keys and etc) in the sub form.
have the query include all records from the parent and only those that match from the child (edit the rel for the query)

you might find that when a user adds data to the childs fields it will automatically add the childs key (if not you can do this in code in the sub form before update event. (i think this might be best)

in the main forms on current event check that the parent's key in not null and enable the subform other wise disable it. of course in the main forms after update event (ie the user adds a new record set the sub forms enabled property to enabled so they can add subform data only after an new parent entry has been made.

after this functions the way you want, you could hide the parent key and child key from the form (not the query) using  .visible = false

as well try setting the subform to different modes edit only (can't add records) you will need to change this to allow adding new entries this can be done in code.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close