Linking one subform to another subform

M

MollyDog8

I am building an attendance database for classes. The classes contain
sessions and students may or may not attend all sessions.

I've built a form that contains two subforms. The main form contains only
the class id. The first subform displays the sessions set up for that class
id.
So far, this subform seems to work just fine.

The problem is with the second subform. When a user clicks a session in the
first subform, I want the second subform to display the list of all students
enrolled in the class along with a Yes/No checkbox that can be clicked to
indicate that the student attended the selected session. If the user clicks
another session in the first subform, the list in the second subform again
displays all the students enrolled in the class and enables the user to click
whether the student attended the selected session.

For the life of me I cannot get this thing to work. Any ideas? It is
driving me bananas and I don't think it can be as hard as I'm making it.

Thanks very much!
 
K

Ken Sheridan

To correlate two subforms you add a hidden unbound text box control to the
main form which acts as the link between the two. So start by putting a text
box anywhere in the main form, name it txtSessionIDHidden say, and set its
Visible property to False (No).

Set the ControlSource property of the hidden text box so it references the
primary key column of the first (sessions) sub form. To reference a property
of a subform you do so by means of the subform control's Form property.
Subform control here means the control in the main form's Controls collection
which houses the subform, not the underlying form object (though both can
have the same name). So the text box's ControlSource would be something like
this.

=[sfcSessions].Form.[SessionID]

where sfcSessions is the name of the first subform control.

For the second (students enrolled) subform set the subform control's
LinkMasterFields property to the name of the hidden text box:

txtSessionIDHidden

and set its LinkChildFields property to the relevant foreign key column in
the subform's underlying table or query, e.g.

SessionID

As you navigate through the first subform the second should show the
students enrolled for the session currently selected in the first subform.

Ken Sheridan
Stafford, England
 
M

MollyDog8

Hi Ken:

Thank you for the quick and terrifically useful response. I'm sorry to say
that I'm being a dope here, but I'm having trouble selecting second (students
enrolled) subforms control's LinkMasterFields property to the name of the
hidden text box. I just don't see it on the Link Master Fields list. What
am I missing? Can you tell I'm new to this?


Ken Sheridan said:
To correlate two subforms you add a hidden unbound text box control to the
main form which acts as the link between the two. So start by putting a text
box anywhere in the main form, name it txtSessionIDHidden say, and set its
Visible property to False (No).

Set the ControlSource property of the hidden text box so it references the
primary key column of the first (sessions) sub form. To reference a property
of a subform you do so by means of the subform control's Form property.
Subform control here means the control in the main form's Controls collection
which houses the subform, not the underlying form object (though both can
have the same name). So the text box's ControlSource would be something like
this.

=[sfcSessions].Form.[SessionID]

where sfcSessions is the name of the first subform control.

For the second (students enrolled) subform set the subform control's
LinkMasterFields property to the name of the hidden text box:

txtSessionIDHidden

and set its LinkChildFields property to the relevant foreign key column in
the subform's underlying table or query, e.g.

SessionID

As you navigate through the first subform the second should show the
students enrolled for the session currently selected in the first subform.

Ken Sheridan
Stafford, England

MollyDog8 said:
I am building an attendance database for classes. The classes contain
sessions and students may or may not attend all sessions.

I've built a form that contains two subforms. The main form contains only
the class id. The first subform displays the sessions set up for that class
id.
So far, this subform seems to work just fine.

The problem is with the second subform. When a user clicks a session in the
first subform, I want the second subform to display the list of all students
enrolled in the class along with a Yes/No checkbox that can be clicked to
indicate that the student attended the selected session. If the user clicks
another session in the first subform, the list in the second subform again
displays all the students enrolled in the class and enables the user to click
whether the student attended the selected session.

For the life of me I cannot get this thing to work. Any ideas? It is
driving me bananas and I don't think it can be as hard as I'm making it.

Thanks very much!
 
J

Jeff Boyce

How depends on what.

Specifics on how to do what you're asking about will depend on the specific
table/data structure you're using.

I'll offer an alternate approach...

If your list of class sessions were in a listbox instead of a subform, you'd
only need one subform, and you could link it to the class session (ID)
implied by the class session selected in the listbox. This is predicated on
a fairly well-normalized table structure.

More info, please...

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
M

MollyDog8

Hi Ken:

Actually I figured it out (wrote too soon). It works just as you said it
would, which is wonderful.

Sadly, it doesn't do exactly what I hoped for, so I hope you won't mind me
annoying you a little more with this.

I'm afraid I'm really mucking this up trying to describe it, so I here's an
example:

Say I have a class called Math 101.

Mary, John and Sue are registered for Math 101.

Math 101 has 3 sessions: A, B and C.

Mary attended session A, but not B and C. John missed sessions A and C, but
attended session B. Sue attended all three sessions.

If my main form shows Math 101, and the first subform shows the sessions set
up for Math 101 (Session A, Session B and Session C), when I click on Session
A in the subform, I'd like the second subform to show Mary, John and Sue,
giving me the opportunity to click Yes/No for attendance. Thus, when I click
Session B in the first subform, Mary, John and Sue are displayed and I can
indicate in the second subform whether they attended the session.

Does that make any sense?

MD8

Ken Sheridan said:
To correlate two subforms you add a hidden unbound text box control to the
main form which acts as the link between the two. So start by putting a text
box anywhere in the main form, name it txtSessionIDHidden say, and set its
Visible property to False (No).

Set the ControlSource property of the hidden text box so it references the
primary key column of the first (sessions) sub form. To reference a property
of a subform you do so by means of the subform control's Form property.
Subform control here means the control in the main form's Controls collection
which houses the subform, not the underlying form object (though both can
have the same name). So the text box's ControlSource would be something like
this.

=[sfcSessions].Form.[SessionID]

where sfcSessions is the name of the first subform control.

For the second (students enrolled) subform set the subform control's
LinkMasterFields property to the name of the hidden text box:

txtSessionIDHidden

and set its LinkChildFields property to the relevant foreign key column in
the subform's underlying table or query, e.g.

SessionID

As you navigate through the first subform the second should show the
students enrolled for the session currently selected in the first subform.

Ken Sheridan
Stafford, England

MollyDog8 said:
I am building an attendance database for classes. The classes contain
sessions and students may or may not attend all sessions.

I've built a form that contains two subforms. The main form contains only
the class id. The first subform displays the sessions set up for that class
id.
So far, this subform seems to work just fine.

The problem is with the second subform. When a user clicks a session in the
first subform, I want the second subform to display the list of all students
enrolled in the class along with a Yes/No checkbox that can be clicked to
indicate that the student attended the selected session. If the user clicks
another session in the first subform, the list in the second subform again
displays all the students enrolled in the class and enables the user to click
whether the student attended the selected session.

For the life of me I cannot get this thing to work. Any ideas? It is
driving me bananas and I don't think it can be as hard as I'm making it.

Thanks very much!
 
M

MollyDog8

Hi Jeff:

My table structures are nearly identical to those suggested by Steve, who
answered just after your post.

Class table
Class ID
Description

Student table
Student ID
Student info

Session table
Session ID
Description

Student Class table
StudentClass ID
Student ID
Class ID

Attendance table
Attendance ID
Session ID
Student ID
Yes/No field

That's pretty much it. Is that a normalized table structure?

Thanks,
MD8
 
K

Ken Sheridan

With the tables as in your reply to Jeff, viz:

Class table
Class ID
Description

Student table
Student ID
Student info

Session table
Session ID
Description

Student Class table
StudentClass ID
Student ID
Class ID

Attendance table
Attendance ID
Session ID
Student ID
Yes/No field

there is no way of relating sessions to classes. The Sessions table needs a
Class ID foreign key column. Is that an accidental omission from the above?
The alternative would be to model a many-to-many relationships between
classes and sessions in the way Steve described. In that model the Session
table would have rows for each 'time-slot' available to all classes, not the
holding of a session for a particular class.

Assuming your model as described in your reply to Jeff and with the Class ID
column in Sessions, rather than Steve's variant, the main parent form would
be based on the Class table, the first subform on the Session table and
linked to the parent form by virtue of the LinkMasterFields and
LinkChildFields properties being set to Class ID

The parent form would contain the hidden text box referencing the Session ID
of the first subform,

The second subform would be based on the Attendance table and its
LinkMasterFields property would reference the hidden text box in the parent
form, its LinkChildFields property would be Session ID.

OK so far?

The drawback of your model is that you need to enter a row in Attendance for
each student enrolled in a class for every session of that class in advance
so that they'll appear in the attendance subform ready for you to check the
check boxes for the students who attend. You can either do this manually,
which would be a PITA, or you can automate it after enrolling a student in a
class, which you'd do in the AfterInsert event procedure of an enrolment form
based on the Student Class table. Before doing that, however you'd need to
have entered rows into the Session table for the class in question, so the
sequence of events from square one would be:

1. Enter a record into the class table for a new class.

2. Enter however many rows are necessary into the session table for the new
class.

3. Enter a record into the Student Class table for a student via an
enrolment form, in whose AfterInsert event procedure you'd run the following
'append' query to insert rows into Attendance:

INSERT INTO [Attendance]([Student ID], [Session ID])
SELECT [Student ID], [Session ID]
FROM [Student Class] INNER JOIN [Session]
ON [Student Class].[Class ID] = [Session].[Class ID]
WHERE [Student Class].[Student ID] = Forms![frmEnrolment]![Student ID]
AND [Session].[Class ID] = Forms![frmEnrolment]![Class ID];

where frmEnrolment is the name of the enrolment form. You'll need to make
sure the table and column names in the query exactly match your actual ones
of course.

Ken Sheridan
Stafford, England

MollyDog8 said:
Hi Ken:

Actually I figured it out (wrote too soon). It works just as you said it
would, which is wonderful.

Sadly, it doesn't do exactly what I hoped for, so I hope you won't mind me
annoying you a little more with this.

I'm afraid I'm really mucking this up trying to describe it, so I here's an
example:

Say I have a class called Math 101.

Mary, John and Sue are registered for Math 101.

Math 101 has 3 sessions: A, B and C.

Mary attended session A, but not B and C. John missed sessions A and C, but
attended session B. Sue attended all three sessions.

If my main form shows Math 101, and the first subform shows the sessions set
up for Math 101 (Session A, Session B and Session C), when I click on Session
A in the subform, I'd like the second subform to show Mary, John and Sue,
giving me the opportunity to click Yes/No for attendance. Thus, when I click
Session B in the first subform, Mary, John and Sue are displayed and I can
indicate in the second subform whether they attended the session.

Does that make any sense?

MD8

Ken Sheridan said:
To correlate two subforms you add a hidden unbound text box control to the
main form which acts as the link between the two. So start by putting a text
box anywhere in the main form, name it txtSessionIDHidden say, and set its
Visible property to False (No).

Set the ControlSource property of the hidden text box so it references the
primary key column of the first (sessions) sub form. To reference a property
of a subform you do so by means of the subform control's Form property.
Subform control here means the control in the main form's Controls collection
which houses the subform, not the underlying form object (though both can
have the same name). So the text box's ControlSource would be something like
this.

=[sfcSessions].Form.[SessionID]

where sfcSessions is the name of the first subform control.

For the second (students enrolled) subform set the subform control's
LinkMasterFields property to the name of the hidden text box:

txtSessionIDHidden

and set its LinkChildFields property to the relevant foreign key column in
the subform's underlying table or query, e.g.

SessionID

As you navigate through the first subform the second should show the
students enrolled for the session currently selected in the first subform.

Ken Sheridan
Stafford, England

MollyDog8 said:
I am building an attendance database for classes. The classes contain
sessions and students may or may not attend all sessions.

I've built a form that contains two subforms. The main form contains only
the class id. The first subform displays the sessions set up for that class
id.
So far, this subform seems to work just fine.

The problem is with the second subform. When a user clicks a session in the
first subform, I want the second subform to display the list of all students
enrolled in the class along with a Yes/No checkbox that can be clicked to
indicate that the student attended the selected session. If the user clicks
another session in the first subform, the list in the second subform again
displays all the students enrolled in the class and enables the user to click
whether the student attended the selected session.

For the life of me I cannot get this thing to work. Any ideas? It is
driving me bananas and I don't think it can be as hard as I'm making it.

Thanks very much!
 
M

MollyDog8

Hi all:

Thanks for your help. As a result of all this good advice, I've ended up
using the session as the main form and dumping the attendance table into it
as a subform. It works pretty dang well - not gorgeous or fancy-schmancy,
but serviceable. At this point, it is all we can hope for!
 

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