Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping subform movement and new records

Status
Not open for further replies.

DerekMcDonald

Technical User
Oct 5, 2000
26
US

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.

Thanks! [sig][/sig]
 
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..)

HTH
Robert
[sig][/sig]
 
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. [sig][/sig]
 
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.

HTH
Robert
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top