Solved

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

Posted on 2014-10-08
8
183 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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