Solved

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

Posted on 2014-10-08
8
185 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 33

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mimic UNC drive 10 46
Empty Record being created 4 42
Runtime Error -2147467259 (80004005) 7 40
Getting "Invalid Operation" Error when opening a Recordset 10 32
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

685 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