Solved

Setting RecordSource in a subform based on a field in the subform

Posted on 2014-10-08
8
186 Views
Last Modified: 2014-10-08
Hello,

I have a form (name = frmCalendar) that has seven subforms (one for each day of the week with the names frmCalendar_Day1, frmCalendar_Day2, etc.). Each subform has a control on it called ColumnDate. The parent form has a variety of buttons that will change the ColumnDate on each of the subforms.

I'd like each subform to display just the records that match its ColumnDate and for this to be applied immediately whenever the ColumnDate is changed.

So, for instance, the recordsource of the first subform might be something like this:

SELECT * FROM tblOrders WHERE OrderDate=ColumnDate

(I realize that's not the right syntax; I've just supplied it to give the idea of what I'm trying to accomplish.)

If the ColumnDate of each subform is changed in the parent form, how do I make the recordsource of each subform change automatically?

I'm not at liberty to change the overall design of the form - it needs to remain one main form with seven subforms. Given that restriction, what's the best way to accomplish this?

Thanks in advance.

James
0
Comment
Question by:jrmcanada2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 40367886
Use the query wizard. There use the Generate.. (the wand button) to create a condition wich points to your control on the parent form.
0
 
LVL 57
ID: 40368043
The simplest setup:

1. Base each subform on a query if it is not already.
2. In each of those queries, add a criteria on the date field of:

=Forms![frmCalendar]![<name of control or field from parent form with the date>]

3. In the AfterUpdate event of the controls on the parent form with the date, requery the subform:

  Me![<subformcontrolName>].Requery

This is only one way to do this.  There are many.

Jim.
0
 
LVL 84
ID: 40368046
Just Refresh the Subforms after the user changes the Date in the main form:

Me.SubformControl1.Form.Refresh
Me.SubformControl2.Form.Refresh

I tend to do it like this:

Me.SubformControl1.Form.Recordsource = Me.SubformControl1.Form.Recordsource

Not sure why ... I recall running into issues with the refresh method, and adopted the Recordsource method.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jrmcanada2
ID: 40368130
Thanks for the responses. Thanks to your suggestions, I've been able to get it working but I think I'm being overly complex so I'll ask for a bit more clarification.

There is a date field (StartDate) on the parent form (frmCalendar) that holds the date for the first column.

Then I have seven subforms (frmCalendar_Day1, frmCalendar_Day2, etc.) that each have a field called ColumnDate. On frmCalendar_Day1, ColumnDate should be the same as StartDate on the parent form. On frmCalendar_Day2, ColumnDate should be StartDate+1, etc.

What I've done now is to put seven date fields on the parent field (one corresponding to each of the columns). I've also created seven different queries with each one pointing to one of those date fields in the query criteria.

Is this the best way to do it or is there a way I can do it with just one underlying query?

Thanks again.

James
0
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40368134
The easiest method is to use the properties of the subformcontrol:

LinkChildFields and LinkMasterFields

http://msdn.microsoft.com/en-us/library/office/ff822089(v=office.15).aspx

The LinkChildFields will be the date for the subform, and the LinkMasterFields the control on the parent form.

No code or anything else is needed. Whenever the masterfield changes value, the subform i updated automatically.

/gustav
0
 
LVL 57
ID: 40368174
<<There is a date field (StartDate) on the parent form (frmCalendar) that holds the date for the first column.>>

If you have only one date field, then each of the queries could use an offset without needing a separate date field for each on the parent form:

=Forms![frmCalendar]![<name of control or field from parent form with the date>] + 1
=Forms![frmCalendar]![<name of control or field from parent form with the date>] + 2
=Forms![frmCalendar]![<name of control or field from parent form with the date>] + 3

 etc.

That's one way to simplify it.

and as gustav said, you could use the link master and child fields.   I suggested the other method though as you have a little more control over things.

You mentioned you had a variety of buttons on the main form to control the subforms and it wasn't clear if you had one date vs 7 on the main form.

The master/child links works great when it's a straight parent/child relationship, but usually when you cobble an interface together for something like this (a calendar) or you have an odd situation (like you want to control transacting or change record sources on the fly), you come out ahead if you avoid them.

But as I mentioned in my comment, there's certainly more than one way to do this.

Another would have been to have only one subform and actually change it's recordsource as you moved through the days, but that would assume only one day visible at a time, and it wasn't clear if that was the case or not.

Nothing wrong though with anything that's been suggested.

Jim.
0
 

Author Closing Comment

by:jrmcanada2
ID: 40368185
This is excellent. As you said, it requires no code. It also only requires the one main query and one subform (that I use seven times). So now when I have to make a change to the layout of a column, I only need to change it in one place rather than duplicating my changes in seven subforms.

Thanks everyone!
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40368190
You are welcome!

/gustav
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question