elational databases: make a related record automatically

M

Moch

I have the following problem.

I have two tables that are related. One tabel is contacts and the other is
projects.

Both tables are filled via a form. On the contacts-form i have a button "add
project". Then the project forms opens so i can fill in the data for this
specific project. But in the project form i always have to choose to wich
contact the projects belong.

Is there a way to make let the contatc be chosen automatically. In other
words: is it possible to make a button "add project" so that i can only add
projects for the specific contact i was working in?

Moch
 
M

Mike Labosh

Disclaimer: I've been up since friday on a Access -> SQL Migration, so if
this post sounds like I'm on crack... :)
Is there a way to make let the contatc be chosen automatically. In other
words: is it possible to make a button "add project" so that i can only
add projects for the specific contact i was working in?

Certainly. The DoCmd.OpenForm method has an OpenArgs parameter that lets
you pass stuff to the form you're opening.

From your button on the Contacts form, pass the current Primary Key value of
the Contact record as the OpenArgs parameter of DoCmd.OpenForm when you open
the Projets form.

Then, in the project form's code, (Open or Load event, I forget which) you
can get this value and pre-populate the foriegn key contactid control.

By the way, if you are using surrogate keys (AutoNumber) you should'nt let
the user see those, so the control(s) that host them should be Visible=No
 
S

Steve Schapel

Moch,

One approach to this would bo to put your Projects form as a subform
onto the Contacts form. If you don't have enough space on the Contacts
form, you could put it on a page of a Tab Control, with your other
Contacts controls on the first tab. Then, instead of a button to open
the Projects, you are just changing pages on the Tab control, and the
correct contact will automatically be assigned to the new project
record, as determined by the LinkMasterFields and LinkChildFields
properties of the subform.

Otherwise, you can simply assign the value to the foreign key field
(presumably ContactID or some such?) when the Projects form is opened.
The code on your button's Click event something like this...
DoCmd.OpenForm "Projects"
DoCmd.GoToRecord acDataForm, "Projects", acNewRec
Forms!Projects!ContactID = Me.ContactID
 
J

John Vinson

Elational databases must be a great deal of fun to work with!

(Sorry... couldn't resist taking advantage of the minor typo).
I have the following problem.

I have two tables that are related. One tabel is contacts and the other is
projects.

Both tables are filled via a form. On the contacts-form i have a button "add
project". Then the project forms opens so i can fill in the data for this
specific project. But in the project form i always have to choose to wich
contact the projects belong.

Is there a way to make let the contatc be chosen automatically. In other
words: is it possible to make a button "add project" so that i can only add
projects for the specific contact i was working in?

Steve's suggestion of a Subform is certainly by far the easiest way to
handle this. As an alternative you can pass the ContactID in the
OpenArgs argument of the OpenForm code behind the button, and set the
ConactID's DefaultValue property to the passed value in the form's
Open event.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 

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