Solved

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

Posted on 2014-10-08
8
175 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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 32

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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 49

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 49

Expert Comment

by:Gustav Brock
ID: 40368190
You are welcome!

/gustav
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now