Filtering on a subreport

C

cprav

Okay, here is what I have going:

It is a database with information on various organizations.
The main report has "Organization Name" and "County" fields.
Because each organization may have several offices, there is a
subform/report for Location. Included is a Community field. To show which
area of the county the office/location is in.

Next, to make this way user friendly, I have created a dialouge box where
the user chooses from a list of counties which populates the community list.
This is working fine. The user then clicks a button to view all the
organizations in the chosen community.

To save myself from create several extra reports (which are quite complex
with several subforms) I have linked the button to a macro which runs the
Report with a filter. Other such buttons I have set up (based on the main
report) have worked fine, but of course, this is a bit tricker as it's a
subform. But surely there is a way?

My LinkMaster/Child properties seem to be correct.

I have most recently tried a query based solely on the subreport which works
- I brings up the organizations in a given community, but I just can't make
that final step to get the full form.

I am inexperiences in coding etc., so detailed instructions would be most
welcome. Let me know if I should include more informaton!

Thanks!
 
A

Allen Browne

If I understand you correctly, you have an Organization table. One
organization can be in many locations, so you also have an
OrganziationLocation table, with a many to one relation to Organization.
Then you have a main report bound to the Organization table, with a
subreport bound ot the OrganziationLocation table. You want to filter the
main report based on a location in the OrganziationLocation table.

If that's the idea, you can use a subquery in the WhereCondition of
OpenReport. The code for the Click event procedure of your button would look
something like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT LocationID FROM OrganziationLocation " & _
"WHERE ((OrganziationLocation.OrganizationID = Organization.OrganizationID)
" & _
"AND (OrganziationLocation.Location = ""New York"")))"
DoCmd.OpenReport "Report1", acViewPreview, , strWhere

If subqueries are new, see:
How to Create and Use Subqueries
at:
http://support.microsoft.com/?id=209066
 
C

cprav

Thanks Allen

The information you provided was really great, however it must be a bit over
my head because I worked on it on and off all day today and just wasn't able
to make it work. :( I even did a bit more reseach as I knew now what to
look for. I understand the concept and how it should work, but just can make
it work, after trying several combinations and experiments.

The actual subquery in my OnClick properties currently looks like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = CommunityInventory.OrgID) " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

DoCmd.OpenReport "CommunityInventory", acViewPreview, strWhere

So this is all pretty much the same as you had EXCEPT where you had "new
york" I've put this ChooseOrganization.choosecom which is a combo box in my
dialouge box where the user chooses the community from a list instead of
typing it (to stop typos and also so the user can see what communities are
available. I think this is probably where my problem is, but not sure.

The code works in the sense that it is opening the right report, but it is
showing all of the organizations in the database, not just the ones that I
selected in my dialouge box (which is still open through the procedure of
course)

I tried another method where I created a query with jus the Locations table
info in it (with, among other fields, the Community, and the only similar
field - OrgID), with a filter in Community field pointing to the dialouge
box. The query works. I added it to my code in various places, including
having the query open at the start of the procedure and close after opening
the report so the information could be pulled from the query. The processes
all worked, but it still did not filter the report.

finally, I noticed you had two commas next to each other in the OpenReport
command. I played around with this - adding it, adding a filter there (just
adding the ChooseOrganization.choosecom or the name of the query that I
created), leaving it out and the procedure did not work at all unless I left
it out.


Sorry for being such a bother, but I'd appreciate anyhelp you can give me in
figureing out what I did wrong!

I love access and everything you can do, but just get stuck on some things
once in a while.
 
A

Allen Browne

You need to concatenate the value into the string.

Probably something like this:

strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = " & [choosecom] & ") " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

If OrgID is a Text field (not a Number field, you will need extra quotes:
"WHERE ((Locations.OrgID = """ & [choosecom] & """) " & _
 
C

cprav

I'm beginning to think I'm going to have to just give up!

I have used your code, and when that didn't work I did some experimenting
again, but eventually went back to your code. The procedure works, but
again, I get all records.

my OrgID is an autonumber field so that the user doesn't have to add that.
Organization Name is none my primary key in case there are two organizations
with the same name. I'm not sure that would happen, but you never know...

What could I be doing wrong? The code below is not based on any queries
(except the query in my choosecom combo box code (and the combo box works
fine - the query includes Locations.community and community.county which is
filtered to the previous Choose County combo box), so it can't be a problem
with any underlying queries.

Sorry for all this. I've never had so much trouble making something in
Access work before!

Allen Browne said:
You need to concatenate the value into the string.

Probably something like this:

strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = " & [choosecom] & ") " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

If OrgID is a Text field (not a Number field, you will need extra quotes:
"WHERE ((Locations.OrgID = """ & [choosecom] & """) " & _

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

cprav said:
Thanks Allen

The information you provided was really great, however it must be a bit
over
my head because I worked on it on and off all day today and just wasn't
able
to make it work. :( I even did a bit more reseach as I knew now what to
look for. I understand the concept and how it should work, but just can
make
it work, after trying several combinations and experiments.

The actual subquery in my OnClick properties currently looks like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = CommunityInventory.OrgID) " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

DoCmd.OpenReport "CommunityInventory", acViewPreview, strWhere

So this is all pretty much the same as you had EXCEPT where you had "new
york" I've put this ChooseOrganization.choosecom which is a combo box in
my
dialouge box where the user chooses the community from a list instead of
typing it (to stop typos and also so the user can see what communities are
available. I think this is probably where my problem is, but not sure.

The code works in the sense that it is opening the right report, but it is
showing all of the organizations in the database, not just the ones that I
selected in my dialouge box (which is still open through the procedure of
course)

I tried another method where I created a query with jus the Locations
table
info in it (with, among other fields, the Community, and the only similar
field - OrgID), with a filter in Community field pointing to the dialouge
box. The query works. I added it to my code in various places, including
having the query open at the start of the procedure and close after
opening
the report so the information could be pulled from the query. The
processes
all worked, but it still did not filter the report.

finally, I noticed you had two commas next to each other in the OpenReport
command. I played around with this - adding it, adding a filter there
(just
adding the ChooseOrganization.choosecom or the name of the query that I
created), leaving it out and the procedure did not work at all unless I
left
it out.


Sorry for being such a bother, but I'd appreciate anyhelp you can give me
in
figureing out what I did wrong!

I love access and everything you can do, but just get stuck on some things
once in a while.
 
A

Allen Browne

After you assign the strWhere string, insert this line:
Debug.Print strWhere

Then when it doesn't work correctly, open the Immediate Window (Ctrl+G) and
see what's printed there. Does that make sense?

Note that the WhereCondition won't be applied if the report is already open.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

cprav said:
I'm beginning to think I'm going to have to just give up!

I have used your code, and when that didn't work I did some experimenting
again, but eventually went back to your code. The procedure works, but
again, I get all records.

my OrgID is an autonumber field so that the user doesn't have to add that.
Organization Name is none my primary key in case there are two
organizations
with the same name. I'm not sure that would happen, but you never know...

What could I be doing wrong? The code below is not based on any queries
(except the query in my choosecom combo box code (and the combo box works
fine - the query includes Locations.community and community.county which
is
filtered to the previous Choose County combo box), so it can't be a
problem
with any underlying queries.

Sorry for all this. I've never had so much trouble making something in
Access work before!

Allen Browne said:
You need to concatenate the value into the string.

Probably something like this:

strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = " & [choosecom] & ") " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

If OrgID is a Text field (not a Number field, you will need extra quotes:
"WHERE ((Locations.OrgID = """ & [choosecom] & """) " & _

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

cprav said:
Thanks Allen

The information you provided was really great, however it must be a bit
over
my head because I worked on it on and off all day today and just wasn't
able
to make it work. :( I even did a bit more reseach as I knew now what
to
look for. I understand the concept and how it should work, but just
can
make
it work, after trying several combinations and experiments.

The actual subquery in my OnClick properties currently looks like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = CommunityInventory.OrgID) " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

DoCmd.OpenReport "CommunityInventory", acViewPreview, strWhere

So this is all pretty much the same as you had EXCEPT where you had
"new
york" I've put this ChooseOrganization.choosecom which is a combo box
in
my
dialouge box where the user chooses the community from a list instead
of
typing it (to stop typos and also so the user can see what communities
are
available. I think this is probably where my problem is, but not sure.

The code works in the sense that it is opening the right report, but it
is
showing all of the organizations in the database, not just the ones
that I
selected in my dialouge box (which is still open through the procedure
of
course)

I tried another method where I created a query with jus the Locations
table
info in it (with, among other fields, the Community, and the only
similar
field - OrgID), with a filter in Community field pointing to the
dialouge
box. The query works. I added it to my code in various places,
including
having the query open at the start of the procedure and close after
opening
the report so the information could be pulled from the query. The
processes
all worked, but it still did not filter the report.

finally, I noticed you had two commas next to each other in the
OpenReport
command. I played around with this - adding it, adding a filter there
(just
adding the ChooseOrganization.choosecom or the name of the query that I
created), leaving it out and the procedure did not work at all unless I
left
it out.


Sorry for being such a bother, but I'd appreciate anyhelp you can give
me
in
figureing out what I did wrong!

I love access and everything you can do, but just get stuck on some
things
once in a while.
:

If I understand you correctly, you have an Organization table. One
organization can be in many locations, so you also have an
OrganziationLocation table, with a many to one relation to
Organization.
Then you have a main report bound to the Organization table, with a
subreport bound ot the OrganziationLocation table. You want to filter
the
main report based on a location in the OrganziationLocation table.

If that's the idea, you can use a subquery in the WhereCondition of
OpenReport. The code for the Click event procedure of your button
would
look
something like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT LocationID FROM OrganziationLocation " & _
"WHERE ((OrganziationLocation.OrganizationID =
Organization.OrganizationID)
" & _
"AND (OrganziationLocation.Location = ""New York"")))"
DoCmd.OpenReport "Report1", acViewPreview, , strWhere

If subqueries are new, see:
How to Create and Use Subqueries
at:
http://support.microsoft.com/?id=209066

Okay, here is what I have going:

It is a database with information on various organizations.
The main report has "Organization Name" and "County" fields.
Because each organization may have several offices, there is a
subform/report for Location. Included is a Community field. To
show
which
area of the county the office/location is in.

Next, to make this way user friendly, I have created a dialouge box
where
the user chooses from a list of counties which populates the
community
list.
This is working fine. The user then clicks a button to view all the
organizations in the chosen community.

To save myself from create several extra reports (which are quite
complex
with several subforms) I have linked the button to a macro which
runs
the
Report with a filter. Other such buttons I have set up (based on
the
main
report) have worked fine, but of course, this is a bit tricker as
it's
a
subform. But surely there is a way?

My LinkMaster/Child properties seem to be correct.

I have most recently tried a query based solely on the subreport
which
works
- I brings up the organizations in a given community, but I just
can't
make
that final step to get the full form.

I am inexperiences in coding etc., so detailed instructions would be
most
welcome. Let me know if I should include more informaton!

Thanks!
 
C

cprav

That's a neat trick. I will have to remember that! This is what it said:

EXISTS (SELECT Community FROM Locations WHERE ((Locations.OrgID =
Bridgetown) AND (Locations.Community = ChooseOrganization.choosecom)))

I'm happy to see that it is taking the input from the combo box.
I played around with changing Locations.OrgID to Locations.Community because
it seemed that that was the field where it should be looking for Bridgetown,
and at this point I will try anything. :)

Any idea?

I really appreciate all your help and very quick replies!

Allen Browne said:
After you assign the strWhere string, insert this line:
Debug.Print strWhere

Then when it doesn't work correctly, open the Immediate Window (Ctrl+G) and
see what's printed there. Does that make sense?

Note that the WhereCondition won't be applied if the report is already open.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

cprav said:
I'm beginning to think I'm going to have to just give up!

I have used your code, and when that didn't work I did some experimenting
again, but eventually went back to your code. The procedure works, but
again, I get all records.

my OrgID is an autonumber field so that the user doesn't have to add that.
Organization Name is none my primary key in case there are two
organizations
with the same name. I'm not sure that would happen, but you never know...

What could I be doing wrong? The code below is not based on any queries
(except the query in my choosecom combo box code (and the combo box works
fine - the query includes Locations.community and community.county which
is
filtered to the previous Choose County combo box), so it can't be a
problem
with any underlying queries.

Sorry for all this. I've never had so much trouble making something in
Access work before!

Allen Browne said:
You need to concatenate the value into the string.

Probably something like this:

strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = " & [choosecom] & ") " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

If OrgID is a Text field (not a Number field, you will need extra quotes:
"WHERE ((Locations.OrgID = """ & [choosecom] & """) " & _

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Thanks Allen

The information you provided was really great, however it must be a bit
over
my head because I worked on it on and off all day today and just wasn't
able
to make it work. :( I even did a bit more reseach as I knew now what
to
look for. I understand the concept and how it should work, but just
can
make
it work, after trying several combinations and experiments.

The actual subquery in my OnClick properties currently looks like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = CommunityInventory.OrgID) " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

DoCmd.OpenReport "CommunityInventory", acViewPreview, strWhere

So this is all pretty much the same as you had EXCEPT where you had
"new
york" I've put this ChooseOrganization.choosecom which is a combo box
in
my
dialouge box where the user chooses the community from a list instead
of
typing it (to stop typos and also so the user can see what communities
are
available. I think this is probably where my problem is, but not sure.

The code works in the sense that it is opening the right report, but it
is
showing all of the organizations in the database, not just the ones
that I
selected in my dialouge box (which is still open through the procedure
of
course)

I tried another method where I created a query with jus the Locations
table
info in it (with, among other fields, the Community, and the only
similar
field - OrgID), with a filter in Community field pointing to the
dialouge
box. The query works. I added it to my code in various places,
including
having the query open at the start of the procedure and close after
opening
the report so the information could be pulled from the query. The
processes
all worked, but it still did not filter the report.

finally, I noticed you had two commas next to each other in the
OpenReport
command. I played around with this - adding it, adding a filter there
(just
adding the ChooseOrganization.choosecom or the name of the query that I
created), leaving it out and the procedure did not work at all unless I
left
it out.


Sorry for being such a bother, but I'd appreciate anyhelp you can give
me
in
figureing out what I did wrong!

I love access and everything you can do, but just get stuck on some
things
once in a while.
:

If I understand you correctly, you have an Organization table. One
organization can be in many locations, so you also have an
OrganziationLocation table, with a many to one relation to
Organization.
Then you have a main report bound to the Organization table, with a
subreport bound ot the OrganziationLocation table. You want to filter
the
main report based on a location in the OrganziationLocation table.

If that's the idea, you can use a subquery in the WhereCondition of
OpenReport. The code for the Click event procedure of your button
would
look
something like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT LocationID FROM OrganziationLocation " & _
"WHERE ((OrganziationLocation.OrganizationID =
Organization.OrganizationID)
" & _
"AND (OrganziationLocation.Location = ""New York"")))"
DoCmd.OpenReport "Report1", acViewPreview, , strWhere

If subqueries are new, see:
How to Create and Use Subqueries
at:
http://support.microsoft.com/?id=209066

Okay, here is what I have going:

It is a database with information on various organizations.
The main report has "Organization Name" and "County" fields.
Because each organization may have several offices, there is a
subform/report for Location. Included is a Community field. To
show
which
area of the county the office/location is in.

Next, to make this way user friendly, I have created a dialouge box
where
the user chooses from a list of counties which populates the
community
list.
This is working fine. The user then clicks a button to view all the
organizations in the chosen community.

To save myself from create several extra reports (which are quite
complex
with several subforms) I have linked the button to a macro which
runs
the
Report with a filter. Other such buttons I have set up (based on
the
main
report) have worked fine, but of course, this is a bit tricker as
it's
a
subform. But surely there is a way?

My LinkMaster/Child properties seem to be correct.

I have most recently tried a query based solely on the subreport
which
works
- I brings up the organizations in a given community, but I just
can't
make
that final step to get the full form.

I am inexperiences in coding etc., so detailed instructions would be
most
welcome. Let me know if I should include more informaton!

Thanks!
 
A

Allen Browne

That's not correct. It should have a number concatenated in the string, and
quotes around the text, e.g.:

EXISTS (SELECT Community FROM Locations WHERE ((Locations.OrgID =
"Bridgetown") AND (Locations.Community = 99)))

You need to work on the way you build the string until it generates this
kind of result.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

cprav said:
That's a neat trick. I will have to remember that! This is what it said:

EXISTS (SELECT Community FROM Locations WHERE ((Locations.OrgID =
Bridgetown) AND (Locations.Community = ChooseOrganization.choosecom)))

I'm happy to see that it is taking the input from the combo box.
I played around with changing Locations.OrgID to Locations.Community
because
it seemed that that was the field where it should be looking for
Bridgetown,
and at this point I will try anything. :)

Any idea?

I really appreciate all your help and very quick replies!

Allen Browne said:
After you assign the strWhere string, insert this line:
Debug.Print strWhere

Then when it doesn't work correctly, open the Immediate Window (Ctrl+G)
and
see what's printed there. Does that make sense?

Note that the WhereCondition won't be applied if the report is already
open.

cprav said:
I'm beginning to think I'm going to have to just give up!

I have used your code, and when that didn't work I did some
experimenting
again, but eventually went back to your code. The procedure works, but
again, I get all records.

my OrgID is an autonumber field so that the user doesn't have to add
that.
Organization Name is none my primary key in case there are two
organizations
with the same name. I'm not sure that would happen, but you never
know...

What could I be doing wrong? The code below is not based on any
queries
(except the query in my choosecom combo box code (and the combo box
works
fine - the query includes Locations.community and community.county
which
is
filtered to the previous Choose County combo box), so it can't be a
problem
with any underlying queries.

Sorry for all this. I've never had so much trouble making something in
Access work before!

:

You need to concatenate the value into the string.

Probably something like this:

strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = " & [choosecom] & ") " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

If OrgID is a Text field (not a Number field, you will need extra
quotes:
"WHERE ((Locations.OrgID = """ & [choosecom] & """) " & _

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Thanks Allen

The information you provided was really great, however it must be a
bit
over
my head because I worked on it on and off all day today and just
wasn't
able
to make it work. :( I even did a bit more reseach as I knew now
what
to
look for. I understand the concept and how it should work, but just
can
make
it work, after trying several combinations and experiments.

The actual subquery in my OnClick properties currently looks like
this:

Dim strWhere As String
strWhere = "EXISTS (SELECT Community FROM Locations " & _
"WHERE ((Locations.OrgID = CommunityInventory.OrgID) " & _
"AND (Locations.Community = ChooseOrganization.choosecom)))"

DoCmd.OpenReport "CommunityInventory", acViewPreview, strWhere

So this is all pretty much the same as you had EXCEPT where you had
"new
york" I've put this ChooseOrganization.choosecom which is a combo
box
in
my
dialouge box where the user chooses the community from a list
instead
of
typing it (to stop typos and also so the user can see what
communities
are
available. I think this is probably where my problem is, but not
sure.

The code works in the sense that it is opening the right report, but
it
is
showing all of the organizations in the database, not just the ones
that I
selected in my dialouge box (which is still open through the
procedure
of
course)

I tried another method where I created a query with jus the
Locations
table
info in it (with, among other fields, the Community, and the only
similar
field - OrgID), with a filter in Community field pointing to the
dialouge
box. The query works. I added it to my code in various places,
including
having the query open at the start of the procedure and close after
opening
the report so the information could be pulled from the query. The
processes
all worked, but it still did not filter the report.

finally, I noticed you had two commas next to each other in the
OpenReport
command. I played around with this - adding it, adding a filter
there
(just
adding the ChooseOrganization.choosecom or the name of the query
that I
created), leaving it out and the procedure did not work at all
unless I
left
it out.


Sorry for being such a bother, but I'd appreciate anyhelp you can
give
me
in
figureing out what I did wrong!

I love access and everything you can do, but just get stuck on some
things
once in a while.
:

If I understand you correctly, you have an Organization table. One
organization can be in many locations, so you also have an
OrganziationLocation table, with a many to one relation to
Organization.
Then you have a main report bound to the Organization table, with a
subreport bound ot the OrganziationLocation table. You want to
filter
the
main report based on a location in the OrganziationLocation table.

If that's the idea, you can use a subquery in the WhereCondition of
OpenReport. The code for the Click event procedure of your button
would
look
something like this:

Dim strWhere As String
strWhere = "EXISTS (SELECT LocationID FROM OrganziationLocation " &
_
"WHERE ((OrganziationLocation.OrganizationID =
Organization.OrganizationID)
" & _
"AND (OrganziationLocation.Location = ""New York"")))"
DoCmd.OpenReport "Report1", acViewPreview, , strWhere

If subqueries are new, see:
How to Create and Use Subqueries
at:
http://support.microsoft.com/?id=209066

Okay, here is what I have going:

It is a database with information on various organizations.
The main report has "Organization Name" and "County" fields.
Because each organization may have several offices, there is a
subform/report for Location. Included is a Community field. To
show
which
area of the county the office/location is in.

Next, to make this way user friendly, I have created a dialouge
box
where
the user chooses from a list of counties which populates the
community
list.
This is working fine. The user then clicks a button to view all
the
organizations in the chosen community.

To save myself from create several extra reports (which are quite
complex
with several subforms) I have linked the button to a macro which
runs
the
Report with a filter. Other such buttons I have set up (based on
the
main
report) have worked fine, but of course, this is a bit tricker as
it's
a
subform. But surely there is a way?

My LinkMaster/Child properties seem to be correct.

I have most recently tried a query based solely on the subreport
which
works
- I brings up the organizations in a given community, but I just
can't
make
that final step to get the full form.

I am inexperiences in coding etc., so detailed instructions would
be
most
welcome. Let me know if I should include more informaton!
 

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