Subforms

K

KiwiB

I have a database of people's details on one table. I have other tables of
information about those people eg Agency visited, assessments completed,
training undertaken. I have linked the tables in one to many relationships.
I have created a 'main' form based on the details table. If I create
subforms on that form the subform is the one for the person one the main form
record visible at the time. I would like to have command buttons on the
details form that open the other forms. When I do that the 'sub' form is not
necessarily at the relevant record. What am I missing here?
 
K

Ken Sheridan

To use separate 'linked' forms rather than a subform, put code in each
button's Click event procedure like this:

DoCmd.OpenForm "YourOtherForm", "MyID = " & MyID

where YourOtherForm is the name of the form to be opened and MyID is the
name of the primary key of the current form's underlying recordset, and also
of the corresponding foreign key in the other form's underlying recordset.
I've assumed that MyID is a number data type. If its text use:

DoCmd.OpenForm "YourOtherForm", "MyID = """ & MyID & """"

Also in the current form's Current event procedure put:

On Error Resume Next
Forms! YourOtherForm.Filter = "MyID = " & Nz(MyID,0)
'''repeat for other linked forms'''

This assures that if a linked form has been open and you set focus back to
the current form, the other form will be kept in sync with the current form
if you navigate to other records. The Nz function is used here for when you
move the current form to an empty new record. Again, if MyID is a text data
type amend it to:

On Error Resume Next
Forms! YourOtherForm.Filter = "MyID = """ & Nz(MyID,"") & """"
'''repeat for other linked forms'''

Ken Sheridan
Stafford, England
 
K

Ken Sheridan

Opps! Left out the argument name:

DoCmd.OpenForm "YourOtherForm", WhereCondition:="MyID = " & MyID

or:

DoCmd.OpenForm "YourOtherForm", WhereCondition:="MyID = """ & MyID & """"

Ken Sheridan
Stafford, England
 
K

KiwiB

Ken Sheridan said:
Opps! Left out the argument name:

DoCmd.OpenForm "YourOtherForm", WhereCondition:="MyID = " & MyID

or:

DoCmd.OpenForm "YourOtherForm", WhereCondition:="MyID = """ & MyID & """"

Ken Sheridan
Stafford, England

Thanks for your help. This was so easy before Access 2007!! (Everything
worked well in 2003) Mark - I can't find any Master Child relationships. Ken
- I have no idea about code but have copied what you suggested. It is not
working yet. But I still need a bit bit more help. Where does the argument
name code go?
BarbaraNZ
 
K

Ken Sheridan

Barbara:

The argument name is 'WhereCondition'. Its an argument of the OpenForm
method, whose syntax is:
DoCmd.OpenForm(FormName, View, FilterName, WhereCondition, DataMode,
WindowMode, OpenArgs)
The list in parentheses is a list of arguments. All bar the FormName
argument are optional. The argument name can be omitted, but then you have
to put a series of commas in so its in the right place in the list. Or you
can name the argument, following it with := and then the expression. What
I'd done in my first post was omit the name, but not put in the necessary
commas. In my correction I added the name, so the commas aren't necessary.

Assuming you've created a form based on your main 'people' table and another
based on your related table, to open the latter from the former you'd add a
button to the former, and then put the line of code in its Click event
procedure. I'm not using Access 2007, but in earlier versions this is how
its been done the same way since before Doris Day was a virgin, so hopefully
it hasn't changed:

1. With the main form open in design view select the button, and make sure
its properties sheet is open.

2. Select the On Click event property on the Events tab of the properties
sheet.

3. Select the 'build' button; that's the one on the right with an ellipsis
(3 dots) on it.

4. In the dialogue which opens select Code Builder and click OK.

5. The VBA window will now open at the button's Click event procedure.
You'll see two lines already in place. Enter the line of code as a new line
between the two, changing YourOtherForm to the actual name of the form to be
opened , and MyID to the actual name of the primary/foreign key fields which
link the tables on which the forms are based. If the MyID fields are number
data type use:

DoCmd.OpenForm "YourOtherForm", WhereCondition:="MyID = " & MyID

If text data type use:

DoCmd.OpenForm "YourOtherForm", WhereCondition:="MyID = """ & MyID & """"

The latter wraps the value of MYID in quotes characters. When including a
quotes character in a string expression, a pair of contiguous quotes is used;
this is interpreted as a literal quotes character.

6. Save the form, switch to normal form view and click the button. If
all's well, the other form should open, filtered to just those records which
relate to the current record in the main form.

BTW when I was young my nickname was Kiwi, which puzzled people because I
don't have any New Zealand connections; I'm Irish. But my initials are KWS,
and at school I had these on my bag. Someone inserted two Is so it read
KIWIS, and from then on I was known as Kiwi.

Ken Sheridan
Stafford, England
 
K

KiwiB

Many thanks for you help. It looks like I need to do some study. Is the
English/Irishman going to explore his Kiwi roots? NZ is a great place for a
holiday.
Barbara
 
K

Ken Sheridan

Barbara:

Maybe when the England cricket team next tours New Zealand. You've got to
get your own back after what happened this weekend!

Cináed O'Siridean
(that's me in Irish)
Stafford, England
 
S

SharynInCambodia

Hello Ken,
Not sure if you'll read this but just letting you know that your answers
here have helped me after spending too many hours trying to solve this same
problem (wanting linked forms to link the related records!) Thanks so much!
I can move on now
 
K

Ken Sheridan

Sharyn:

It was kind of you to write. Its always nice to hear back from people, even
more so when my feeble efforts have been of some help.

Good luck with your project.

Ken Sheridan
Stafford, England
 

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