473,416 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Recordset in subform based on field in parent form

Lyn
Hi,
I am working on a genealogy form. The only table (so far) lists everybody
in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the
table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform
(sfmSiblings) in which I wish to list all the brothers and sisters of the
person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field
values (excluding of course the person displayed in the main form). I am
trying to use the values in the controls on the main form where the father
ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I hardcoded
test values for the father ID and mother ID. This works fine. Then I added
this form as a subform in the Tab Control of the main form. I viewed the
SQL for the query and updated it by substituting the values of the controls
on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...

I obviously have this syntax wrong, because when I run the main form, for
each record I get msgboxes prompting for the values frmMainForm!IDFath and
frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I
make the query in the subform use criteria displayed in controls on the main
form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but
related table from the record in the main form. In my case, there is only
one table and I want to display related records (via the parent IDs) from
that same table. Am I not allowed to do this? Or will I need some sort of
additional crossreference table?

Thanks in advance for any assistance.
--
Cheers,
Lyn.
Nov 13 '05 #1
25 10143
Lyn, just put the following in the criteria line of the query:

[Forms]![frmMainForm]![IDFath] Or [Forms]![frmMainForm]![IDMoth]

Hope it helps!
--
Reggie

www.smittysinet.com
----------
"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...
Hi,
I am working on a genealogy form. The only table (so far) lists everybody
in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the
table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform
(sfmSiblings) in which I wish to list all the brothers and sisters of the
person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field
values (excluding of course the person displayed in the main form). I am
trying to use the values in the controls on the main form where the father
ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I hardcoded test values for the father ID and mother ID. This works fine. Then I added this form as a subform in the Tab Control of the main form. I viewed the
SQL for the query and updated it by substituting the values of the controls on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...

I obviously have this syntax wrong, because when I run the main form, for
each record I get msgboxes prompting for the values frmMainForm!IDFath and
frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I
make the query in the subform use criteria displayed in controls on the main form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but
related table from the record in the main form. In my case, there is only
one table and I want to display related records (via the parent IDs) from
that same table. Am I not allowed to do this? Or will I need some sort of additional crossreference table?

Thanks in advance for any assistance.
--
Cheers,
Lyn.

Nov 13 '05 #2
Hi Lyn

You could try:
WHERE (Person.IDFather = Nz(Forms!frmMainForm!IDFath,0))
OR (Person.IDMother = Nz(Forms!frmMainForm!IDMoth,0))

The Nz() should cope with the case where the main form is at a new record.
However, you will probably get an initial error on load, because Access
loads the subform before the main form.

On your broader question, you can join a table to itself, by dragging
multiple copies of the table into the Relationships window. There is an
example of this with animal pedigrees at:
http://allenbrowne.com/ser-06.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...

I am working on a genealogy form. The only table (so far) lists everybody
in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the
table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform
(sfmSiblings) in which I wish to list all the brothers and sisters of the
person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field
values (excluding of course the person displayed in the main form). I am
trying to use the values in the controls on the main form where the father
ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I hardcoded test values for the father ID and mother ID. This works fine. Then I added this form as a subform in the Tab Control of the main form. I viewed the
SQL for the query and updated it by substituting the values of the controls on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...

I obviously have this syntax wrong, because when I run the main form, for
each record I get msgboxes prompting for the values frmMainForm!IDFath and
frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I
make the query in the subform use criteria displayed in controls on the main form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but
related table from the record in the main form. In my case, there is only
one table and I want to display related records (via the parent IDs) from
that same table. Am I not allowed to do this? Or will I need some sort of additional crossreference table?

Thanks in advance for any assistance.
--
Cheers,
Lyn.

Nov 13 '05 #3
Lyn wrote:
Hi,
I am working on a genealogy form. The only table (so far) lists everybody
in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the
table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform
(sfmSiblings) in which I wish to list all the brothers and sisters of the
person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field
values (excluding of course the person displayed in the main form). I am
trying to use the values in the controls on the main form where the father
ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I hardcoded
test values for the father ID and mother ID. This works fine. Then I added
this form as a subform in the Tab Control of the main form. I viewed the
SQL for the query and updated it by substituting the values of the controls
on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...
If you make the record source for your subform something like
this it should work:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

I'm assuming that the autonumber ID is called IDperson. That last
line is needed unless you also want to include the person on the
main form in the list of siblings.

I obviously have this syntax wrong, because when I run the main form, for
each record I get msgboxes prompting for the values frmMainForm!IDFath and
frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I
make the query in the subform use criteria displayed in controls on the main
form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but
related table from the record in the main form. In my case, there is only
one table and I want to display related records (via the parent IDs) from
that same table. Am I not allowed to do this? Or will I need some sort of
additional crossreference table?


this shouldn't be any problem.

Nov 13 '05 #4
Lyn
Allen,
Thanks for your suggestions. The animal pedigree tip looks very similar to
what I am doing. I will give this a try.

BTW, I live in Sydney.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40**********************@freenews.iinet.net.a u...
Hi Lyn

You could try:
WHERE (Person.IDFather = Nz(Forms!frmMainForm!IDFath,0))
OR (Person.IDMother = Nz(Forms!frmMainForm!IDMoth,0))

The Nz() should cope with the case where the main form is at a new record.
However, you will probably get an initial error on load, because Access
loads the subform before the main form.

On your broader question, you can join a table to itself, by dragging
multiple copies of the table into the Relationships window. There is an
example of this with animal pedigrees at:
http://allenbrowne.com/ser-06.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...

I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform (sfmSiblings) in which I wish to list all the brothers and sisters of the person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field values (excluding of course the person displayed in the main form). I am trying to use the values in the controls on the main form where the father ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I

hardcoded
test values for the father ID and mother ID. This works fine. Then I

added
this form as a subform in the Tab Control of the main form. I viewed the SQL for the query and updated it by substituting the values of the

controls
on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...

I obviously have this syntax wrong, because when I run the main form, for each record I get msgboxes prompting for the values frmMainForm!IDFath and frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I make the query in the subform use criteria displayed in controls on the

main
form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but related table from the record in the main form. In my case, there is only one table and I want to display related records (via the parent IDs) from that same table. Am I not allowed to do this? Or will I need some sort

of
additional crossreference table?

Thanks in advance for any assistance.
--
Cheers,
Lyn.


Nov 13 '05 #5
Lyn
Thanks Reggie. From all the responses, it looks as though the main thing I
was missing was the "Forms!" prefix. I have gotten too used to the "Me!"
shortcut which of course does not work here.

--
Cheers,
Lyn.
"Reggie" <no**********@smittysinet.com> wrote in message
news:GP********************@comcast.com...
Lyn, just put the following in the criteria line of the query:

[Forms]![frmMainForm]![IDFath] Or [Forms]![frmMainForm]![IDMoth]

Hope it helps!
--
Reggie

www.smittysinet.com
----------
"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...
Hi,
I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform (sfmSiblings) in which I wish to list all the brothers and sisters of the person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field values (excluding of course the person displayed in the main form). I am trying to use the values in the controls on the main form where the father ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I

hardcoded
test values for the father ID and mother ID. This works fine. Then I

added
this form as a subform in the Tab Control of the main form. I viewed the SQL for the query and updated it by substituting the values of the

controls
on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...

I obviously have this syntax wrong, because when I run the main form, for each record I get msgboxes prompting for the values frmMainForm!IDFath and frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I make the query in the subform use criteria displayed in controls on the

main
form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but related table from the record in the main form. In my case, there is only one table and I want to display related records (via the parent IDs) from that same table. Am I not allowed to do this? Or will I need some sort

of
additional crossreference table?

Thanks in advance for any assistance.
--
Cheers,
Lyn.


Nov 13 '05 #6
Lyn
Hi Helen. You are right, my autonum ID *is* called "IDPerson"! It seems
that the main problem was that I omitted the "Forms!" prefix in the main
form reference.

Thanks to all for your prompt responses.
--
Cheers,
Lyn.

"Helen Wheels" <he**********@yahoo.com.au> wrote in message
news:40**************@yahoo.com.au...
Lyn wrote:
Hi,
I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform (sfmSiblings) in which I wish to list all the brothers and sisters of the person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field values (excluding of course the person displayed in the main form). I am trying to use the values in the controls on the main form where the father ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I hardcoded test values for the father ID and mother ID. This works fine. Then I added this form as a subform in the Tab Control of the main form. I viewed the SQL for the query and updated it by substituting the values of the controls on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...


If you make the record source for your subform something like
this it should work:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

I'm assuming that the autonumber ID is called IDperson. That last
line is needed unless you also want to include the person on the
main form in the list of siblings.

I obviously have this syntax wrong, because when I run the main form, for each record I get msgboxes prompting for the values frmMainForm!IDFath and frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I make the query in the subform use criteria displayed in controls on the main form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but related table from the record in the main form. In my case, there is only one table and I want to display related records (via the parent IDs) from that same table. Am I not allowed to do this? Or will I need some sort of additional crossreference table?


this shouldn't be any problem.

Nov 13 '05 #7
Lyn
Thanks again to everyone who responded to my original question. I think
Allen was right, using a control value in the main form as a criterion does
not work because the subform loads before the main form.

The Person record must be available before the main form or subform opens.
So I have tried using multiple copies of the table (Person) in the query
(with alias names) with different types of joins. Firstly, just the one
alias (Person_Sib) joined by the IDFather field in each table (I have left
out IDMother for simplicity). The Person table in the query is intended to
be the record in the main form, and the Person_Sib alias is intended to find
all the records with the same IDFather. However, in this case, Person_Sib
just seems to track Person -- ie, Person_Sib returns only the same (single)
record as Person.

So I have tried a different tack. I inserted a second alias of Person
(Person_Father) between the two original tables in the query. In this case,
Person.IDFather is joined to Person_Father.IDPerson (the autonum record ID)
which is joined to Person_Sib.IDFather. At this point I got confused over
what sort of joins to use. Using INNER joins for both (which I am sure is
wrong), Person_Sib still seems to track the same record as Person.

The Person record is the subject of the main form. I need the Person_Sib
alias to list ALL records with the same IDFather (and eventually "OR same
IDMother"). I will of course eventually filter out the Person_Sib record
that matches the Person in the main form (who cannot be his/her own
sibling). How can I do this in the Access query? Or do I have to resort to
VBA to create the sibling recordset? What am I doing wrong? (I hope that
this makes sense.)

Thanks for any further assistance.
--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40**********************@freenews.iinet.net.a u...
Hi Lyn

You could try:
WHERE (Person.IDFather = Nz(Forms!frmMainForm!IDFath,0))
OR (Person.IDMother = Nz(Forms!frmMainForm!IDMoth,0))

The Nz() should cope with the case where the main form is at a new record.
However, you will probably get an initial error on load, because Access
loads the subform before the main form.

On your broader question, you can join a table to itself, by dragging
multiple copies of the table into the Relationships window. There is an
example of this with animal pedigrees at:
http://allenbrowne.com/ser-06.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...

I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform (sfmSiblings) in which I wish to list all the brothers and sisters of the person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field values (excluding of course the person displayed in the main form). I am trying to use the values in the controls on the main form where the father ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I

hardcoded
test values for the father ID and mother ID. This works fine. Then I

added
this form as a subform in the Tab Control of the main form. I viewed the SQL for the query and updated it by substituting the values of the

controls
on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...

I obviously have this syntax wrong, because when I run the main form, for each record I get msgboxes prompting for the values frmMainForm!IDFath and frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how can I make the query in the subform use criteria displayed in controls on the

main
form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but related table from the record in the main form. In my case, there is only one table and I want to display related records (via the parent IDs) from that same table. Am I not allowed to do this? Or will I need some sort

of
additional crossreference table?

Thanks in advance for any assistance.
--
Cheers,
Lyn.


Nov 13 '05 #8
Could you set the subform so it intially loads no-one:
SELECT * FROM MyTable WHERE (False);

Then use the Current event of the main form to assign its RecordSource.
Something like this:
Dim strWhere As String

If Me.NewRecord Then
strWhere = "(False);"
Else
strWhere = "(Person_Father.IDPerson = " & Me.IDPerson & ") OR
(Person_Mother.IDPerson = " & Me.IDPerson & ");"
Me.[Child1].Form.RecordSource = "SELECT * FROM SomeTable WHERE " &
strWhere
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...
Thanks again to everyone who responded to my original question. I think
Allen was right, using a control value in the main form as a criterion does not work because the subform loads before the main form.

The Person record must be available before the main form or subform opens.
So I have tried using multiple copies of the table (Person) in the query
(with alias names) with different types of joins. Firstly, just the one
alias (Person_Sib) joined by the IDFather field in each table (I have left
out IDMother for simplicity). The Person table in the query is intended to be the record in the main form, and the Person_Sib alias is intended to find all the records with the same IDFather. However, in this case, Person_Sib
just seems to track Person -- ie, Person_Sib returns only the same (single) record as Person.

So I have tried a different tack. I inserted a second alias of Person
(Person_Father) between the two original tables in the query. In this case, Person.IDFather is joined to Person_Father.IDPerson (the autonum record ID) which is joined to Person_Sib.IDFather. At this point I got confused over
what sort of joins to use. Using INNER joins for both (which I am sure is
wrong), Person_Sib still seems to track the same record as Person.

The Person record is the subject of the main form. I need the Person_Sib
alias to list ALL records with the same IDFather (and eventually "OR same
IDMother"). I will of course eventually filter out the Person_Sib record
that matches the Person in the main form (who cannot be his/her own
sibling). How can I do this in the Access query? Or do I have to resort to VBA to create the sibling recordset? What am I doing wrong? (I hope that
this makes sense.)

Thanks for any further assistance.
--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40**********************@freenews.iinet.net.a u...
Hi Lyn

You could try:
WHERE (Person.IDFather = Nz(Forms!frmMainForm!IDFath,0))
OR (Person.IDMother = Nz(Forms!frmMainForm!IDMoth,0))

The Nz() should cope with the case where the main form is at a new record.
However, you will probably get an initial error on load, because Access
loads the subform before the main form.

On your broader question, you can join a table to itself, by dragging
multiple copies of the table into the Relationships window. There is an
example of this with animal pedigrees at:
http://allenbrowne.com/ser-06.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...

I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID.

The parent form (frmMainForm) displays the data in each record, which
includes the ID of the father and the mother (who also have records in the table). One record per form.

I have a Tab Control in the form, and in one of the tabs I have a subform (sfmSiblings) in which I wish to list all the brothers and sisters of the person currently being displayed in the main form. The basis of the
selection is all records which have the same father ID or mother ID field values (excluding of course the person displayed in the main form). I am trying to use the values in the controls on the main form where the father ID and mother ID are displayed in the subform query.

I created the subform originally as a form with a query in which I hardcoded
test values for the father ID and mother ID. This works fine. Then I

added
this form as a subform in the Tab Control of the main form. I viewed the SQL for the query and updated it by substituting the values of the

controls
on the main form for the hardcoding. That is, instead of using the
following format in the SELECT statement:

WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...

I substituted:

WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
frmMainForm!IDMoth ...

I obviously have this syntax wrong, because when I run the main form, for each record I get msgboxes prompting for the values frmMainForm!IDFath and frmMainForm!IDMoth. If I respond with the correct values, the subform
displays the correct data.

I hope that this makes some sense. Essentially my question is: how
can I make the query in the subform use criteria displayed in controls on
the main
form?

Another aspect of this is that all the books and tutorials I have seen
assume that the subform will be used to display data from a different but related table from the record in the main form. In my case, there is only one table and I want to display related records (via the parent IDs) from that same table. Am I not allowed to do this? Or will I need some

sort of
additional crossreference table?

Thanks in advance for any assistance.
--
Cheers,
Lyn.

Nov 13 '05 #9
Lyn
Allen,
I have tried your suggestion and variations of it, but no luck. After a lot
of trial and error, I have resorted to using VBA code similar to what you
provided in your previous. I have simplified the requirements to the bare
minimum, just to get something that works, then I can build it up again.

I now have a form containing a subform (sfmSibs) in a tab control. I have
removed any query from the subform's Record Source.

In the Current event for the main form, which I already use to populate some
unbound controls on the main form, I have added the following code:

Dim mySQL As String

If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
mySQL = "SELECT Person.SurnameBirth, Person.FirstName, " & _
"Person.DOB, Person.Sex, " & _
"Person.IDPerson FROM Person " & _
"WHERE Person.IDFather = " & Me![IDFather] & ";"
Me.[sfmSibs].Form.RecordSource = mySQL
End If

When I run this, if Me![IDFather] is null or zero, of course it works (blank
form as expected). However, any record with a non-zero Me![IDFather] gives
this error:

Run-time error '2465':
Microsoft Office Access can't find the field "|"
referred to in your expression.

Not very helpful, as I don't have "|" anywhere in the expression. (Once
when I mistyped a word, this error message displayed the word in error, but
not this time.)

When I click Debug, the following line is highlighted:

Me.[sfmSibs].Form.RecordSource = mySQL

I presume that the problem is somewhere in the mySQL string. I have tried
it with and without the terminating semicolon ";" (VBA often seems to insert
that for you), but no effect.

Any ideas how to troubleshoot this when the field in error is quoted as "|"
?!?!

A related question: instead of doing this in the Current event of the main
form, could I do it in the On Click event of the tab control Page? So that
the subform is not populated unless the relevant tab is clicked. Just
trying to learn as much as I can from this.

Thanks again for your help.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Could you set the subform so it intially loads no-one:
SELECT * FROM MyTable WHERE (False);

Then use the Current event of the main form to assign its RecordSource.
Something like this:
Dim strWhere As String

If Me.NewRecord Then
strWhere = "(False);"
Else
strWhere = "(Person_Father.IDPerson = " & Me.IDPerson & ") OR
(Person_Mother.IDPerson = " & Me.IDPerson & ");"
Me.[Child1].Form.RecordSource = "SELECT * FROM SomeTable WHERE " &
strWhere
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...
Thanks again to everyone who responded to my original question. I think
Allen was right, using a control value in the main form as a criterion does
not work because the subform loads before the main form.

The Person record must be available before the main form or subform opens.
So I have tried using multiple copies of the table (Person) in the query
(with alias names) with different types of joins. Firstly, just the one
alias (Person_Sib) joined by the IDFather field in each table (I have left out IDMother for simplicity). The Person table in the query is intended

to
be the record in the main form, and the Person_Sib alias is intended to

find
all the records with the same IDFather. However, in this case, Person_Sib just seems to track Person -- ie, Person_Sib returns only the same

(single)
record as Person.

So I have tried a different tack. I inserted a second alias of Person
(Person_Father) between the two original tables in the query. In this

case,
Person.IDFather is joined to Person_Father.IDPerson (the autonum record

ID)
which is joined to Person_Sib.IDFather. At this point I got confused over what sort of joins to use. Using INNER joins for both (which I am sure is wrong), Person_Sib still seems to track the same record as Person.

The Person record is the subject of the main form. I need the Person_Sib alias to list ALL records with the same IDFather (and eventually "OR same IDMother"). I will of course eventually filter out the Person_Sib record that matches the Person in the main form (who cannot be his/her own
sibling). How can I do this in the Access query? Or do I have to resort to
VBA to create the sibling recordset? What am I doing wrong? (I hope

that this makes sense.)

Thanks for any further assistance.
--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40**********************@freenews.iinet.net.a u...
Hi Lyn

You could try:
WHERE (Person.IDFather = Nz(Forms!frmMainForm!IDFath,0))
OR (Person.IDMother = Nz(Forms!frmMainForm!IDMoth,0))

The Nz() should cope with the case where the main form is at a new record. However, you will probably get an initial error on load, because Access loads the subform before the main form.

On your broader question, you can join a table to itself, by dragging
multiple copies of the table into the Relationships window. There is an example of this with animal pedigrees at:
http://allenbrowne.com/ser-06.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...
>
> I am working on a genealogy form. The only table (so far) lists

everybody
> in the family, one record per person. Each record has an autonum ID. >
> The parent form (frmMainForm) displays the data in each record, which > includes the ID of the father and the mother (who also have records in the
> table). One record per form.
>
> I have a Tab Control in the form, and in one of the tabs I have a

subform
> (sfmSiblings) in which I wish to list all the brothers and sisters
of
the
> person currently being displayed in the main form. The basis of the
> selection is all records which have the same father ID or mother ID

field
> values (excluding of course the person displayed in the main form).
I am
> trying to use the values in the controls on the main form where the

father
> ID and mother ID are displayed in the subform query.
>
> I created the subform originally as a form with a query in which I
hardcoded
> test values for the father ID and mother ID. This works fine. Then
I added
> this form as a subform in the Tab Control of the main form. I viewed the
> SQL for the query and updated it by substituting the values of the
controls
> on the main form for the hardcoding. That is, instead of using the
> following format in the SELECT statement:
>
> WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...
>
> I substituted:
>
> WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother =
> frmMainForm!IDMoth ...
>
> I obviously have this syntax wrong, because when I run the main
form, for
> each record I get msgboxes prompting for the values
frmMainForm!IDFath and
> frmMainForm!IDMoth. If I respond with the correct values, the
subform > displays the correct data.
>
> I hope that this makes some sense. Essentially my question is: how

can
I
> make the query in the subform use criteria displayed in controls on

the main
> form?
>
> Another aspect of this is that all the books and tutorials I have seen > assume that the subform will be used to display data from a
different but
> related table from the record in the main form. In my case, there
is only
> one table and I want to display related records (via the parent IDs)

from
> that same table. Am I not allowed to do this? Or will I need some

sort of
> additional crossreference table?
>
> Thanks in advance for any assistance.
> --
> Cheers,
> Lyn.


Nov 13 '05 #10
Hi Lyn.

To debug this, ask Access to print mySQL out to the immediate window:
mySQL = "SELECT ...
Debug.Print mySQL
Me.[sfmSibs].Form...

Now try it. Open the Immediate window (Ctrl+G). Access will have printed the
faulty SQL statement there. Copy it to clipboard. Then create a new query,
switch it to SQL View (View menu), and paste in the faulty SQL string. This
should help you to see what's wrong, and you will see how to fix the
assigment to mySQL.

The pipe character is a place holder for the field name. It appears that you
are getting the error message without the correct name being inserted into
the place holder. Ultimately it means that Access can't make sense of the
SQL statement. For example, if IDFather is actually a Text field (not a
Number field), then the SQL statement needs exta quote marks:
"WHERE Person.IDFather = """ & Me![IDFather] & """;"

Another cause of the error is Name AutoCorrect incorrectly tracking the
field name. Uncheck the boxes under Tools | Options | General | Name
AutoCorrect, and then compact the database (Tools | Database Utilities).

You're on the right track, so it is worth continuing to debug this. Do
include the trailing semicolon in the sql statement.

It would be possible to use the Change event of the tab control (not the
Click event of the page) rather than the Current event of the main form. The
problem with that approach is that it leaves the wrong data showing in the
subform when you change record in the main form, until you move to a
different tab and move back to the one you want again.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen,
I have tried your suggestion and variations of it, but no luck. After a lot of trial and error, I have resorted to using VBA code similar to what you
provided in your previous. I have simplified the requirements to the bare
minimum, just to get something that works, then I can build it up again.

I now have a form containing a subform (sfmSibs) in a tab control. I have
removed any query from the subform's Record Source.

In the Current event for the main form, which I already use to populate some unbound controls on the main form, I have added the following code:

Dim mySQL As String

If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
mySQL = "SELECT Person.SurnameBirth, Person.FirstName, " & _
"Person.DOB, Person.Sex, " & _
"Person.IDPerson FROM Person " & _
"WHERE Person.IDFather = " & Me![IDFather] & ";"
Me.[sfmSibs].Form.RecordSource = mySQL
End If

When I run this, if Me![IDFather] is null or zero, of course it works (blank form as expected). However, any record with a non-zero Me![IDFather] gives this error:

Run-time error '2465':
Microsoft Office Access can't find the field "|"
referred to in your expression.

Not very helpful, as I don't have "|" anywhere in the expression. (Once
when I mistyped a word, this error message displayed the word in error, but not this time.)

When I click Debug, the following line is highlighted:

Me.[sfmSibs].Form.RecordSource = mySQL

I presume that the problem is somewhere in the mySQL string. I have tried
it with and without the terminating semicolon ";" (VBA often seems to insert that for you), but no effect.

Any ideas how to troubleshoot this when the field in error is quoted as "|" ?!?!

A related question: instead of doing this in the Current event of the main
form, could I do it in the On Click event of the tab control Page? So that the subform is not populated unless the relevant tab is clicked. Just
trying to learn as much as I can from this.

Thanks again for your help.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Could you set the subform so it intially loads no-one:
SELECT * FROM MyTable WHERE (False);

Then use the Current event of the main form to assign its RecordSource.
Something like this:
Dim strWhere As String

If Me.NewRecord Then
strWhere = "(False);"
Else
strWhere = "(Person_Father.IDPerson = " & Me.IDPerson & ") OR
(Person_Mother.IDPerson = " & Me.IDPerson & ");"
Me.[Child1].Form.RecordSource = "SELECT * FROM SomeTable WHERE " &
strWhere
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:c9**********@lust.ihug.co.nz...
Thanks again to everyone who responded to my original question. I think Allen was right, using a control value in the main form as a criterion does
not work because the subform loads before the main form.

The Person record must be available before the main form or subform opens. So I have tried using multiple copies of the table (Person) in the query (with alias names) with different types of joins. Firstly, just the one alias (Person_Sib) joined by the IDFather field in each table (I have left out IDMother for simplicity). The Person table in the query is intended to
be the record in the main form, and the Person_Sib alias is intended
to
find
all the records with the same IDFather. However, in this case, Person_Sib just seems to track Person -- ie, Person_Sib returns only the same

(single)
record as Person.

So I have tried a different tack. I inserted a second alias of Person
(Person_Father) between the two original tables in the query. In this

case,
Person.IDFather is joined to Person_Father.IDPerson (the autonum
record ID)
which is joined to Person_Sib.IDFather. At this point I got confused over what sort of joins to use. Using INNER joins for both (which I am
sure is wrong), Person_Sib still seems to track the same record as Person.

The Person record is the subject of the main form. I need the Person_Sib alias to list ALL records with the same IDFather (and eventually "OR same IDMother"). I will of course eventually filter out the Person_Sib record that matches the Person in the main form (who cannot be his/her own
sibling). How can I do this in the Access query? Or do I have to resort
to
VBA to create the sibling recordset? What am I doing wrong? (I hope
that this makes sense.)

Thanks for any further assistance.
--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40**********************@freenews.iinet.net.a u...
> Hi Lyn
>
> You could try:
> WHERE (Person.IDFather = Nz(Forms!frmMainForm!IDFath,0))
> OR (Person.IDMother = Nz(Forms!frmMainForm!IDMoth,0))
>
> The Nz() should cope with the case where the main form is at a new

record.
> However, you will probably get an initial error on load, because Access > loads the subform before the main form.
>
> On your broader question, you can join a table to itself, by
dragging > multiple copies of the table into the Relationships window. There is

an > example of this with animal pedigrees at:
> http://allenbrowne.com/ser-06.html
>
> "Lyn" <lh******@ihug.com.au> wrote in message
> news:c9**********@lust.ihug.co.nz...
> >
> > I am working on a genealogy form. The only table (so far) lists
everybody
> > in the family, one record per person. Each record has an autonum ID. > >
> > The parent form (frmMainForm) displays the data in each record, which > > includes the ID of the father and the mother (who also have records in
the
> > table). One record per form.
> >
> > I have a Tab Control in the form, and in one of the tabs I have a
subform
> > (sfmSiblings) in which I wish to list all the brothers and sisters of the
> > person currently being displayed in the main form. The basis of
the > > selection is all records which have the same father ID or mother ID field
> > values (excluding of course the person displayed in the main form).
I am
> > trying to use the values in the controls on the main form where
the father
> > ID and mother ID are displayed in the subform query.
> >
> > I created the subform originally as a form with a query in which I
> hardcoded
> > test values for the father ID and mother ID. This works fine. Then
I > added
> > this form as a subform in the Tab Control of the main form. I viewed the
> > SQL for the query and updated it by substituting the values of the
> controls
> > on the main form for the hardcoding. That is, instead of using
the > > following format in the SELECT statement:
> >
> > WHERE Person.IDFather = 5 OR Person.IDMother = 6 ...
> >
> > I substituted:
> >
> > WHERE Person.IDFather = frmMainForm!IDFath OR Person.IDMother = > > frmMainForm!IDMoth ...
> >
> > I obviously have this syntax wrong, because when I run the main

form, for
> > each record I get msgboxes prompting for the values frmMainForm!IDFath and
> > frmMainForm!IDMoth. If I respond with the correct values, the subform > > displays the correct data.
> >
> > I hope that this makes some sense. Essentially my question is: how can
I
> > make the query in the subform use criteria displayed in controls
on
the
> main
> > form?
> >
> > Another aspect of this is that all the books and tutorials I have seen > > assume that the subform will be used to display data from a different but
> > related table from the record in the main form. In my case, there is only
> > one table and I want to display related records (via the parent

IDs) from
> > that same table. Am I not allowed to do this? Or will I need

some sort
> of
> > additional crossreference table?
> >
> > Thanks in advance for any assistance.
> > --
> > Cheers,
> > Lyn.

Nov 13 '05 #11
Lyn
Thanks again, Allen. This has given me some good debugging tools.

I did as you suggested, all steps, but without any improvement.

However, I did discover the main problem. Rather than continually update
the same subform and same query, I had created new versions of each. In the
end I had about eight versions of the subform and a similar number of
queries. Updating the query version of a form or subform is easy -- you
just update the Record Source property. But I could not find any analogue
of this for updating the subform version in the Form properties. I
suspected that I might have had the wrong subform version configured in the
main form. The only way I could find to fix this was to delete the current
subform and to recreate it from the Toolbox so that I could get into the
Subform Wizard. Is there any other way to invoke the Subform Wizard?

Anyway, after deleting and recreating the subform in the tab control page of
the main form, the error went away.

I now get data in the subform, but it is always only the same (single)
Person record as in the main form. That is, if I have a number of brothers
and sisters, all with the same IDFather, only my own record is displayed in
the subform. Which I eventually want to suppress, leaving only brothers and
sisters. It seems that instead of the subforms Record Source being the
whole of the Person table, it is being restricted to the one record already
selected by the main form. I had expected the VBA effectively to create a
new recordset (I have successfully used such recordsets in this manner
elsewhere in the main form VBA). How can I make the VBA for the subform
create a new Record Source based on the WHOLE of the Person table, rather
than the single record already selected by the main form?

I feel that I am getting closer to the solution, there is just one piece of
the puzzle still missing.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Hi Lyn.

To debug this, ask Access to print mySQL out to the immediate window:
mySQL = "SELECT ...
Debug.Print mySQL
Me.[sfmSibs].Form...

Now try it. Open the Immediate window (Ctrl+G). Access will have printed the faulty SQL statement there. Copy it to clipboard. Then create a new query,
switch it to SQL View (View menu), and paste in the faulty SQL string. This should help you to see what's wrong, and you will see how to fix the
assigment to mySQL.

The pipe character is a place holder for the field name. It appears that you are getting the error message without the correct name being inserted into
the place holder. Ultimately it means that Access can't make sense of the
SQL statement. For example, if IDFather is actually a Text field (not a
Number field), then the SQL statement needs exta quote marks:
"WHERE Person.IDFather = """ & Me![IDFather] & """;"

Another cause of the error is Name AutoCorrect incorrectly tracking the
field name. Uncheck the boxes under Tools | Options | General | Name
AutoCorrect, and then compact the database (Tools | Database Utilities).

You're on the right track, so it is worth continuing to debug this. Do
include the trailing semicolon in the sql statement.

It would be possible to use the Change event of the tab control (not the
Click event of the page) rather than the Current event of the main form. The problem with that approach is that it leaves the wrong data showing in the
subform when you change record in the main form, until you move to a
different tab and move back to the one you want again.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen,
I have tried your suggestion and variations of it, but no luck. After a

lot
of trial and error, I have resorted to using VBA code similar to what you provided in your previous. I have simplified the requirements to the bare minimum, just to get something that works, then I can build it up again.

I now have a form containing a subform (sfmSibs) in a tab control. I have removed any query from the subform's Record Source.

In the Current event for the main form, which I already use to populate

some
unbound controls on the main form, I have added the following code:

Dim mySQL As String

If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
mySQL = "SELECT Person.SurnameBirth, Person.FirstName, " & _
"Person.DOB, Person.Sex, " & _
"Person.IDPerson FROM Person " & _
"WHERE Person.IDFather = " & Me![IDFather] & ";"
Me.[sfmSibs].Form.RecordSource = mySQL
End If

When I run this, if Me![IDFather] is null or zero, of course it works

(blank
form as expected). However, any record with a non-zero Me![IDFather]

gives
this error:

Run-time error '2465':
Microsoft Office Access can't find the field "|"
referred to in your expression.

Not very helpful, as I don't have "|" anywhere in the expression. (Once
when I mistyped a word, this error message displayed the word in error,

but
not this time.)

When I click Debug, the following line is highlighted:

Me.[sfmSibs].Form.RecordSource = mySQL

I presume that the problem is somewhere in the mySQL string. I have tried it with and without the terminating semicolon ";" (VBA often seems to

insert
that for you), but no effect.

Any ideas how to troubleshoot this when the field in error is quoted as

"|"
?!?!

A related question: instead of doing this in the Current event of the main form, could I do it in the On Click event of the tab control Page? So

that
the subform is not populated unless the relevant tab is clicked. Just
trying to learn as much as I can from this.

Thanks again for your help.

--
Cheers,
Lyn.

Nov 13 '05 #12
Good: you are making progress with debugging.

To change which form gets loaded into your subform control without running
the wizard, you could changethe SourceObject of the subform control.

To help you get the desired SQL statement, create a new query and enter some
dummy values for the person fields. If you have multiple copies of the
Person table, use an alias (properties box) to help assign a meaningful
name. When the query is working the way you need, switch it to SQL View
(View menu), and you have a sample of the SQL string you need to create.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Thanks again, Allen. This has given me some good debugging tools.

I did as you suggested, all steps, but without any improvement.

However, I did discover the main problem. Rather than continually update
the same subform and same query, I had created new versions of each. In the end I had about eight versions of the subform and a similar number of
queries. Updating the query version of a form or subform is easy -- you
just update the Record Source property. But I could not find any analogue
of this for updating the subform version in the Form properties. I
suspected that I might have had the wrong subform version configured in the main form. The only way I could find to fix this was to delete the current subform and to recreate it from the Toolbox so that I could get into the
Subform Wizard. Is there any other way to invoke the Subform Wizard?

Anyway, after deleting and recreating the subform in the tab control page of the main form, the error went away.

I now get data in the subform, but it is always only the same (single)
Person record as in the main form. That is, if I have a number of brothers and sisters, all with the same IDFather, only my own record is displayed in the subform. Which I eventually want to suppress, leaving only brothers and sisters. It seems that instead of the subforms Record Source being the
whole of the Person table, it is being restricted to the one record already selected by the main form. I had expected the VBA effectively to create a
new recordset (I have successfully used such recordsets in this manner
elsewhere in the main form VBA). How can I make the VBA for the subform
create a new Record Source based on the WHOLE of the Person table, rather
than the single record already selected by the main form?

I feel that I am getting closer to the solution, there is just one piece of the puzzle still missing.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Hi Lyn.

To debug this, ask Access to print mySQL out to the immediate window:
mySQL = "SELECT ...
Debug.Print mySQL
Me.[sfmSibs].Form...

Now try it. Open the Immediate window (Ctrl+G). Access will have printed

the
faulty SQL statement there. Copy it to clipboard. Then create a new query,
switch it to SQL View (View menu), and paste in the faulty SQL string.

This
should help you to see what's wrong, and you will see how to fix the
assigment to mySQL.

The pipe character is a place holder for the field name. It appears that

you
are getting the error message without the correct name being inserted into the place holder. Ultimately it means that Access can't make sense of the SQL statement. For example, if IDFather is actually a Text field (not a
Number field), then the SQL statement needs exta quote marks:
"WHERE Person.IDFather = """ & Me![IDFather] & """;"

Another cause of the error is Name AutoCorrect incorrectly tracking the
field name. Uncheck the boxes under Tools | Options | General | Name
AutoCorrect, and then compact the database (Tools | Database Utilities).

You're on the right track, so it is worth continuing to debug this. Do
include the trailing semicolon in the sql statement.

It would be possible to use the Change event of the tab control (not the
Click event of the page) rather than the Current event of the main form.

The
problem with that approach is that it leaves the wrong data showing in the subform when you change record in the main form, until you move to a
different tab and move back to the one you want again.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen,
I have tried your suggestion and variations of it, but no luck. After a
lot
of trial and error, I have resorted to using VBA code similar to what you provided in your previous. I have simplified the requirements to the bare minimum, just to get something that works, then I can build it up
again.
I now have a form containing a subform (sfmSibs) in a tab control. I

have removed any query from the subform's Record Source.

In the Current event for the main form, which I already use to populate some
unbound controls on the main form, I have added the following code:

Dim mySQL As String

If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
mySQL = "SELECT Person.SurnameBirth, Person.FirstName, " &
_ "Person.DOB, Person.Sex, " & _
"Person.IDPerson FROM Person " & _
"WHERE Person.IDFather = " & Me![IDFather] & ";"
Me.[sfmSibs].Form.RecordSource = mySQL
End If

When I run this, if Me![IDFather] is null or zero, of course it works

(blank
form as expected). However, any record with a non-zero Me![IDFather]

gives
this error:

Run-time error '2465':
Microsoft Office Access can't find the field "|"
referred to in your expression.

Not very helpful, as I don't have "|" anywhere in the expression. (Once when I mistyped a word, this error message displayed the word in
error, but
not this time.)

When I click Debug, the following line is highlighted:

Me.[sfmSibs].Form.RecordSource = mySQL

I presume that the problem is somewhere in the mySQL string. I have tried it with and without the terminating semicolon ";" (VBA often seems to

insert
that for you), but no effect.

Any ideas how to troubleshoot this when the field in error is quoted
as "|"
?!?!

A related question: instead of doing this in the Current event of the

main form, could I do it in the On Click event of the tab control Page? So

that
the subform is not populated unless the relevant tab is clicked. Just
trying to learn as much as I can from this.

Thanks again for your help.

--
Cheers,
Lyn.

Nov 13 '05 #13
Lyn
Allen,
Thanks again for the help and encouragement. And patience!

I did not fully understand your previous suggestions. I have had queries
and subforms with the IDFather value hardcoded working from the beginning.
The problem has been in getting the query and subform integrated into the
main form, using a value in the main form in place of the hardcoded value,
and using the full table to select from instead of just the current record
in the main form. I tried creating an alias of the table in the SQL by
using the syntax:

SELECT * FROM table AS alias WHERE alias.xxx = ...

But this gave me the same result as not using the alias. That is, it just
seems to give the record already selected in the main form a new table name.
What is needed is the alias to reflect a new copy of the whole table. Maybe
I am not applying the alias properly, although this was all I could find in
the Help file.

I could not find a SourceObject property in the subform control. Only
RecordSource, which points to the dummy query used when first opening the
form.

However, I do have this bit of code working, using the Immediate Pane as a
pseudo subform:

Dim Conn As ADODB.Connection
Set Conn = CurrentProject.Connection
Dim RS As New ADODB.Recordset

If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
mySQL = "SELECT Person.Surname, Person.FirstName, Person.DOB,
Person.Sex, " & _
"Person.IDPerson FROM Person " & _
"WHERE Person.IDFather = " & Me![IDFather] & _
" AND Person.IDPerson <> " & Me![IDPerson] & ";"
RS.Open mySQL, Conn, adOpenStatic
If RS.RecordCount > 0 Then
RS.MoveFirst
While Not RS.EOF
Debug.Print "Name: " & RS!Surname & " " & RS!FirstName
RS.MoveNext
Wend
End If
RS.Close
End If

This displays all the person's siblings in the debug pane -- ie, all records
in the Person table with the same IDFather value, excluding the record
currently displayed in the main form.

All I have to do now is find out how to display the same results in the
subform instead of the debug window. A simple task! (You would think?) So
what am I still doing wrong?

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Good: you are making progress with debugging.

To change which form gets loaded into your subform control without running
the wizard, you could changethe SourceObject of the subform control.

To help you get the desired SQL statement, create a new query and enter some dummy values for the person fields. If you have multiple copies of the
Person table, use an alias (properties box) to help assign a meaningful
name. When the query is working the way you need, switch it to SQL View
(View menu), and you have a sample of the SQL string you need to create.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Thanks again, Allen. This has given me some good debugging tools.

I did as you suggested, all steps, but without any improvement.

However, I did discover the main problem. Rather than continually update
the same subform and same query, I had created new versions of each. In the
end I had about eight versions of the subform and a similar number of
queries. Updating the query version of a form or subform is easy -- you
just update the Record Source property. But I could not find any analogue of this for updating the subform version in the Form properties. I
suspected that I might have had the wrong subform version configured in

the
main form. The only way I could find to fix this was to delete the

current
subform and to recreate it from the Toolbox so that I could get into the
Subform Wizard. Is there any other way to invoke the Subform Wizard?

Anyway, after deleting and recreating the subform in the tab control page of
the main form, the error went away.

I now get data in the subform, but it is always only the same (single)
Person record as in the main form. That is, if I have a number of brothers
and sisters, all with the same IDFather, only my own record is displayed

in
the subform. Which I eventually want to suppress, leaving only brothers

and
sisters. It seems that instead of the subforms Record Source being the
whole of the Person table, it is being restricted to the one record

already
selected by the main form. I had expected the VBA effectively to create

a new recordset (I have successfully used such recordsets in this manner
elsewhere in the main form VBA). How can I make the VBA for the subform
create a new Record Source based on the WHOLE of the Person table, rather than the single record already selected by the main form?

I feel that I am getting closer to the solution, there is just one piece

of
the puzzle still missing.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Hi Lyn.

To debug this, ask Access to print mySQL out to the immediate window:
mySQL = "SELECT ...
Debug.Print mySQL
Me.[sfmSibs].Form...

Now try it. Open the Immediate window (Ctrl+G). Access will have printed
the
faulty SQL statement there. Copy it to clipboard. Then create a new query, switch it to SQL View (View menu), and paste in the faulty SQL string.

This
should help you to see what's wrong, and you will see how to fix the
assigment to mySQL.

The pipe character is a place holder for the field name. It appears
that you
are getting the error message without the correct name being inserted into the place holder. Ultimately it means that Access can't make sense of the SQL statement. For example, if IDFather is actually a Text field (not
a Number field), then the SQL statement needs exta quote marks:
"WHERE Person.IDFather = """ & Me![IDFather] & """;"

Another cause of the error is Name AutoCorrect incorrectly tracking the field name. Uncheck the boxes under Tools | Options | General | Name
AutoCorrect, and then compact the database (Tools | Database Utilities).
You're on the right track, so it is worth continuing to debug this. Do
include the trailing semicolon in the sql statement.

It would be possible to use the Change event of the tab control (not the Click event of the page) rather than the Current event of the main form. The
problem with that approach is that it leaves the wrong data showing in the subform when you change record in the main form, until you move to a
different tab and move back to the one you want again.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
> Allen,
> I have tried your suggestion and variations of it, but no luck.
After a lot
> of trial and error, I have resorted to using VBA code similar to
what you
> provided in your previous. I have simplified the requirements to
the bare
> minimum, just to get something that works, then I can build it up again. >
> I now have a form containing a subform (sfmSibs) in a tab control.
I have
> removed any query from the subform's Record Source.
>
> In the Current event for the main form, which I already use to populate some
> unbound controls on the main form, I have added the following code:
>
> Dim mySQL As String
>
> If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
> mySQL = "SELECT Person.SurnameBirth, Person.FirstName, "
&
_ > "Person.DOB, Person.Sex, " & _
> "Person.IDPerson FROM Person " & _
> "WHERE Person.IDFather = " & Me![IDFather] & ";"
> Me.[sfmSibs].Form.RecordSource = mySQL
> End If
>
> When I run this, if Me![IDFather] is null or zero, of course it
works (blank
> form as expected). However, any record with a non-zero Me![IDFather] gives
> this error:
>
> Run-time error '2465':
> Microsoft Office Access can't find the field "|"
> referred to in your expression.
>
> Not very helpful, as I don't have "|" anywhere in the expression.

(Once > when I mistyped a word, this error message displayed the word in error, but
> not this time.)
>
> When I click Debug, the following line is highlighted:
>
> Me.[sfmSibs].Form.RecordSource = mySQL
>
> I presume that the problem is somewhere in the mySQL string. I have

tried
> it with and without the terminating semicolon ";" (VBA often seems to insert
> that for you), but no effect.
>
> Any ideas how to troubleshoot this when the field in error is quoted as "|"
> ?!?!
>
> A related question: instead of doing this in the Current event of the main
> form, could I do it in the On Click event of the tab control Page?

So that
> the subform is not populated unless the relevant tab is clicked. Just > trying to learn as much as I can from this.
>
> Thanks again for your help.
>
> --
> Cheers,
> Lyn.


Nov 13 '05 #14
Assuming that IDFather and IDPerson are text boxes in the main form, that
statement *should* work if you assign it to the RecordSource of the subform
in the Current event of the main form.

If you found a RecordSource property but not a SourceObject property, you
were looking at the form in the subform control, not the subform control
itself. Open the main form in design view. Right-click the edge of the
subform control, and choose Properties. SourceObject should be the first
item on the Data tab.

Hope that helps.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen,
Thanks again for the help and encouragement. And patience!

I did not fully understand your previous suggestions. I have had queries
and subforms with the IDFather value hardcoded working from the beginning.
The problem has been in getting the query and subform integrated into the
main form, using a value in the main form in place of the hardcoded value,
and using the full table to select from instead of just the current record
in the main form. I tried creating an alias of the table in the SQL by
using the syntax:

SELECT * FROM table AS alias WHERE alias.xxx = ...

But this gave me the same result as not using the alias. That is, it just
seems to give the record already selected in the main form a new table name. What is needed is the alias to reflect a new copy of the whole table. Maybe I am not applying the alias properly, although this was all I could find in the Help file.

I could not find a SourceObject property in the subform control. Only
RecordSource, which points to the dummy query used when first opening the
form.

However, I do have this bit of code working, using the Immediate Pane as a
pseudo subform:

Dim Conn As ADODB.Connection
Set Conn = CurrentProject.Connection
Dim RS As New ADODB.Recordset

If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
mySQL = "SELECT Person.Surname, Person.FirstName, Person.DOB,
Person.Sex, " & _
"Person.IDPerson FROM Person " & _
"WHERE Person.IDFather = " & Me![IDFather] & _
" AND Person.IDPerson <> " & Me![IDPerson] & ";"
RS.Open mySQL, Conn, adOpenStatic
If RS.RecordCount > 0 Then
RS.MoveFirst
While Not RS.EOF
Debug.Print "Name: " & RS!Surname & " " & RS!FirstName
RS.MoveNext
Wend
End If
RS.Close
End If

This displays all the person's siblings in the debug pane -- ie, all records in the Person table with the same IDFather value, excluding the record
currently displayed in the main form.

All I have to do now is find out how to display the same results in the
subform instead of the debug window. A simple task! (You would think?) So what am I still doing wrong?

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Good: you are making progress with debugging.

To change which form gets loaded into your subform control without running
the wizard, you could changethe SourceObject of the subform control.

To help you get the desired SQL statement, create a new query and enter some
dummy values for the person fields. If you have multiple copies of the
Person table, use an alias (properties box) to help assign a meaningful
name. When the query is working the way you need, switch it to SQL View
(View menu), and you have a sample of the SQL string you need to create.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Thanks again, Allen. This has given me some good debugging tools.

I did as you suggested, all steps, but without any improvement.

However, I did discover the main problem. Rather than continually update the same subform and same query, I had created new versions of each. In the
end I had about eight versions of the subform and a similar number of
queries. Updating the query version of a form or subform is easy --
you just update the Record Source property. But I could not find any analogue of this for updating the subform version in the Form properties. I
suspected that I might have had the wrong subform version configured in the
main form. The only way I could find to fix this was to delete the

current
subform and to recreate it from the Toolbox so that I could get into
the Subform Wizard. Is there any other way to invoke the Subform Wizard?

Anyway, after deleting and recreating the subform in the tab control page
of
the main form, the error went away.

I now get data in the subform, but it is always only the same (single)
Person record as in the main form. That is, if I have a number of

brothers
and sisters, all with the same IDFather, only my own record is displayed in
the subform. Which I eventually want to suppress, leaving only
brothers and
sisters. It seems that instead of the subforms Record Source being
the whole of the Person table, it is being restricted to the one record

already
selected by the main form. I had expected the VBA effectively to create a new recordset (I have successfully used such recordsets in this manner
elsewhere in the main form VBA). How can I make the VBA for the
subform create a new Record Source based on the WHOLE of the Person table,
rather than the single record already selected by the main form?

I feel that I am getting closer to the solution, there is just one piece of
the puzzle still missing.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
> Hi Lyn.
>
> To debug this, ask Access to print mySQL out to the immediate
window: > mySQL = "SELECT ...
> Debug.Print mySQL
> Me.[sfmSibs].Form...
>
> Now try it. Open the Immediate window (Ctrl+G). Access will have printed the
> faulty SQL statement there. Copy it to clipboard. Then create a new

query,
> switch it to SQL View (View menu), and paste in the faulty SQL string. This
> should help you to see what's wrong, and you will see how to fix the
> assigment to mySQL.
>
> The pipe character is a place holder for the field name. It appears that you
> are getting the error message without the correct name being inserted into
> the place holder. Ultimately it means that Access can't make sense
of
the
> SQL statement. For example, if IDFather is actually a Text field
(not a > Number field), then the SQL statement needs exta quote marks:
> "WHERE Person.IDFather = """ & Me![IDFather] & """;"
>
> Another cause of the error is Name AutoCorrect incorrectly tracking the > field name. Uncheck the boxes under Tools | Options | General | Name
> AutoCorrect, and then compact the database (Tools | Database Utilities). >
> You're on the right track, so it is worth continuing to debug this.
Do > include the trailing semicolon in the sql statement.
>
> It would be possible to use the Change event of the tab control (not
the > Click event of the page) rather than the Current event of the main form. The
> problem with that approach is that it leaves the wrong data showing in the
> subform when you change record in the main form, until you move to a
> different tab and move back to the one you want again.
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia.
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
> "Lyn" <lh******@ihug.com.au> wrote in message
> news:ca**********@lust.ihug.co.nz...
> > Allen,
> > I have tried your suggestion and variations of it, but no luck. After
a
> lot
> > of trial and error, I have resorted to using VBA code similar to what you
> > provided in your previous. I have simplified the requirements to the bare
> > minimum, just to get something that works, then I can build it up

again.
> >
> > I now have a form containing a subform (sfmSibs) in a tab control. I have
> > removed any query from the subform's Record Source.
> >
> > In the Current event for the main form, which I already use to

populate
> some
> > unbound controls on the main form, I have added the following
code: > >
> > Dim mySQL As String
> >
> > If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
> > mySQL = "SELECT Person.SurnameBirth, Person.FirstName, " &
_
> > "Person.DOB, Person.Sex, " & _
> > "Person.IDPerson FROM Person " & _
> > "WHERE Person.IDFather = " & Me![IDFather] &

";" > > Me.[sfmSibs].Form.RecordSource = mySQL
> > End If
> >
> > When I run this, if Me![IDFather] is null or zero, of course it

works > (blank
> > form as expected). However, any record with a non-zero Me![IDFather] > gives
> > this error:
> >
> > Run-time error '2465':
> > Microsoft Office Access can't find the field "|"
> > referred to in your expression.
> >
> > Not very helpful, as I don't have "|" anywhere in the expression.

(Once
> > when I mistyped a word, this error message displayed the word in

error,
> but
> > not this time.)
> >
> > When I click Debug, the following line is highlighted:
> >
> > Me.[sfmSibs].Form.RecordSource = mySQL
> >
> > I presume that the problem is somewhere in the mySQL string. I have tried
> > it with and without the terminating semicolon ";" (VBA often seems to > insert
> > that for you), but no effect.
> >
> > Any ideas how to troubleshoot this when the field in error is
quoted as
> "|"
> > ?!?!
> >
> > A related question: instead of doing this in the Current event of

the main
> > form, could I do it in the On Click event of the tab control Page? So > that
> > the subform is not populated unless the relevant tab is clicked. Just > > trying to learn as much as I can from this.
> >
> > Thanks again for your help.
> >
> > --
> > Cheers,
> > Lyn.

Nov 13 '05 #15
Lyn
Allen, thanks for the prompt response. With your guidance, I did find
Source Object -- thank you.

Re assigning "that statement" to the subform Record Source in the main form
Current event, did you mean the code I had defining the recordset, but using
"Me.[sfmSibs].Form.RecordSource = mySQL" instead of the "RS.Open ..."? Or
did you mean use it with the SQL statement "SELECT * FROM table AS alias
WHERE alias.xxx = ..."?

I am pretty sure that I tried the former before, but I am so confused with
all the things I tried that I am prepared to try it again.

--
Cheers,
Lyn.
"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Assuming that IDFather and IDPerson are text boxes in the main form, that
statement *should* work if you assign it to the RecordSource of the subform in the Current event of the main form.

If you found a RecordSource property but not a SourceObject property, you
were looking at the form in the subform control, not the subform control
itself. Open the main form in design view. Right-click the edge of the
subform control, and choose Properties. SourceObject should be the first
item on the Data tab.

Hope that helps.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen,
Thanks again for the help and encouragement. And patience!

I did not fully understand your previous suggestions. I have had queries and subforms with the IDFather value hardcoded working from the beginning. The problem has been in getting the query and subform integrated into the main form, using a value in the main form in place of the hardcoded value, and using the full table to select from instead of just the current record in the main form. I tried creating an alias of the table in the SQL by
using the syntax:

SELECT * FROM table AS alias WHERE alias.xxx = ...

But this gave me the same result as not using the alias. That is, it just seems to give the record already selected in the main form a new table

name.
What is needed is the alias to reflect a new copy of the whole table.

Maybe
I am not applying the alias properly, although this was all I could find

in
the Help file.

I could not find a SourceObject property in the subform control. Only
RecordSource, which points to the dummy query used when first opening the form.

However, I do have this bit of code working, using the Immediate Pane as a pseudo subform:

Dim Conn As ADODB.Connection
Set Conn = CurrentProject.Connection
Dim RS As New ADODB.Recordset

If Not IsNull(Me![IDFather]) And Me![IDFather] <> 0 Then
mySQL = "SELECT Person.Surname, Person.FirstName, Person.DOB,
Person.Sex, " & _
"Person.IDPerson FROM Person " & _
"WHERE Person.IDFather = " & Me![IDFather] & _
" AND Person.IDPerson <> " & Me![IDPerson] & ";"
RS.Open mySQL, Conn, adOpenStatic
If RS.RecordCount > 0 Then
RS.MoveFirst
While Not RS.EOF
Debug.Print "Name: " & RS!Surname & " " & RS!FirstName
RS.MoveNext
Wend
End If
RS.Close
End If

This displays all the person's siblings in the debug pane -- ie, all

records
in the Person table with the same IDFather value, excluding the record
currently displayed in the main form.

All I have to do now is find out how to display the same results in the
subform instead of the debug window. A simple task! (You would think?)

So
what am I still doing wrong?

--
Cheers,
Lyn.

Nov 13 '05 #16
Yes. Now you have a working query statement, you should be able to do this:
Me.[sfmSibs].Form.RecordSource = mySQL
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen, thanks for the prompt response. With your guidance, I did find
Source Object -- thank you.

Re assigning "that statement" to the subform Record Source in the main form Current event, did you mean the code I had defining the recordset, but using "Me.[sfmSibs].Form.RecordSource = mySQL" instead of the "RS.Open ..."? Or
did you mean use it with the SQL statement "SELECT * FROM table AS alias
WHERE alias.xxx = ..."?

I am pretty sure that I tried the former before, but I am so confused with
all the things I tried that I am prepared to try it again.

--
Cheers,
Lyn.

Nov 13 '05 #17
Lyn
Allen, still no go. Still selects only the current record from the main
form.

I'd like to recap and summarise the situation as I understand it now. This
is a very simplified description of the form and associated code for ease of
understanding.

1) I have a form (main form) which displays one record of a family member
from the table Person. Each such record has a unique (autonum) ID field --
IDPerson. Each record also has a non-unique field -- IDFather.

2) I have a subform (sfmSibs) in the main form in which I wish to display
all records in the Person table which have the same IDFather value as that
of the current record in the main form.

3) Consider the following SQL which will be used in both points 4) and 5)
below:

Dim mySQL As String
mySQL = "SELECT * FROM Table WHERE Person.IDFather = " & Me![IDFather] &
";"

"Me![IDFather]" is a control on the main form where the current record's
IDFather value is displayed.

4) The following code creates a recordset which includes ALL of the Person
records with the current value of Me![IDFather]:

Dim RS As New ADODB.Recordset
Dim Conn As ADODB.Connection
Set Conn = CurrentProject.Connection

RS.Open mySQL, Conn, adOpenStatic

From this recordset, I can access all of the records with the same IDFather,
but cannot (as yet) find a way to display the recordset in the subform.

5) The following alternative code will display in the subform:

Me.[sfmSibs].Form.RecordSource = mySQL

However, mySQL in this case selects ONLY from the single Person record which
is already displayed in the main form (or so it seems). That is, the
subform never contains more than one row which is the same as the main form
record, regardless of how many records contain the same IDFather value.
What I need to do is somehow combine points 4) and 5). That is, effectively
get the subform to display a recordset compiled from the entire Person
table, not just the current main form record. I have tried combining these
steps, by specifying RS in various ways as the subform record source, but
with no success.

I can't help thinking that part of the recordset process (point 4) is to
specify a connection to the database (ie: the "Conn" argument in the RS.Open
statement). Maybe this is what causes the recordset to start with a fresh
(full) copy of the table. The code in point 5) stands on its own, and since
the main form has already selected a single Person record, the code seems to
take that as its source rather than starting with a fresh copy of the whole
table.

At least that is how it looks to me. Is there something missing from the
point 5) code? What do I need to do to get the SQL to select from the WHOLE
of the Person table for the subform?

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Yes. Now you have a working query statement, you should be able to do this: Me.[sfmSibs].Form.RecordSource = mySQL
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen, thanks for the prompt response. With your guidance, I did find
Source Object -- thank you.

Re assigning "that statement" to the subform Record Source in the main

form
Current event, did you mean the code I had defining the recordset, but

using
"Me.[sfmSibs].Form.RecordSource = mySQL" instead of the "RS.Open ..."? Or did you mean use it with the SQL statement "SELECT * FROM table AS alias
WHERE alias.xxx = ..."?

I am pretty sure that I tried the former before, but I am so confused with all the things I tried that I am prepared to try it again.

--
Cheers,
Lyn.


Nov 13 '05 #18
Lyn, I'm guessing that Access is automatically setting the
LinkMasterFields/LinkChildFields properties of the subform control. You
don't want that, because you are programmatically assigning the records.

Open the main form in design view.
Right-click the edge of the subform control, and choose Properties.
On the Data tab of the Properties box, locate the LinkMasterFields control,
and delete the IDPerson entry. Delete the entry for LinkChildFields as well.

I'm going to be away for the weekend, but will check back on Tuesday to see
if this solved your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen, still no go. Still selects only the current record from the main
form.

I'd like to recap and summarise the situation as I understand it now. This is a very simplified description of the form and associated code for ease of understanding.

1) I have a form (main form) which displays one record of a family member
from the table Person. Each such record has a unique (autonum) ID field -- IDPerson. Each record also has a non-unique field -- IDFather.

2) I have a subform (sfmSibs) in the main form in which I wish to display
all records in the Person table which have the same IDFather value as that
of the current record in the main form.

3) Consider the following SQL which will be used in both points 4) and 5)
below:

Dim mySQL As String
mySQL = "SELECT * FROM Table WHERE Person.IDFather = " & Me![IDFather] & ";"

"Me![IDFather]" is a control on the main form where the current record's
IDFather value is displayed.

4) The following code creates a recordset which includes ALL of the Person
records with the current value of Me![IDFather]:

Dim RS As New ADODB.Recordset
Dim Conn As ADODB.Connection
Set Conn = CurrentProject.Connection

RS.Open mySQL, Conn, adOpenStatic

From this recordset, I can access all of the records with the same IDFather, but cannot (as yet) find a way to display the recordset in the subform.

5) The following alternative code will display in the subform:

Me.[sfmSibs].Form.RecordSource = mySQL

However, mySQL in this case selects ONLY from the single Person record which is already displayed in the main form (or so it seems). That is, the
subform never contains more than one row which is the same as the main form record, regardless of how many records contain the same IDFather value.
What I need to do is somehow combine points 4) and 5). That is, effectively get the subform to display a recordset compiled from the entire Person
table, not just the current main form record. I have tried combining these steps, by specifying RS in various ways as the subform record source, but
with no success.

I can't help thinking that part of the recordset process (point 4) is to
specify a connection to the database (ie: the "Conn" argument in the RS.Open statement). Maybe this is what causes the recordset to start with a fresh
(full) copy of the table. The code in point 5) stands on its own, and since the main form has already selected a single Person record, the code seems to take that as its source rather than starting with a fresh copy of the whole table.

At least that is how it looks to me. Is there something missing from the
point 5) code? What do I need to do to get the SQL to select from the WHOLE of the Person table for the subform?

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Yes. Now you have a working query statement, you should be able to do

this:
Me.[sfmSibs].Form.RecordSource = mySQL
"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen, thanks for the prompt response. With your guidance, I did find
Source Object -- thank you.

Re assigning "that statement" to the subform Record Source in the main

form
Current event, did you mean the code I had defining the recordset, but

using
"Me.[sfmSibs].Form.RecordSource = mySQL" instead of the "RS.Open ..."? Or did you mean use it with the SQL statement "SELECT * FROM table AS alias WHERE alias.xxx = ..."?

I am pretty sure that I tried the former before, but I am so confused with all the things I tried that I am prepared to try it again.

--
Cheers,
Lyn.

Nov 13 '05 #19
Lyn
Allen, I hope you enjoyed your break.

I have had no luck with the VBA, but Helen has emailed me a solution which
uses neither VBA nor a query (thanks again Helen). It just inserts an SQL
Select statement directly into the RecordSource of the subform. And it
works fine. The statement is:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

While I hadn't thought of inserting the SQL directly into RecordSource, I
did think that I had tried this as a Query. However, I have tried so many
different things trying to resolve this that I can't be sure. In the end,
it was certainly simple enough.

I can't help thinking that the solution via VBA is going to be simple too.
Having come this far, I would still like to find the VBA solution, if only
as a learning exercise. So if you still have the patience, I would like to
continue.

One other thing. Rather than displaying the IDFather and IDMother values in
the Continuous Feeds subform, I would like to translate each into a text
field inserted into the row (eg, [FirstName] & " " & [Surname]). Using
Helen's solution, I have created a text control in the subform row for
Father. In the RecordSource for the control, I have firstly inserted a
simple SQL statement (which didn't work -- gave me "#Name?"), and then put
the same SQL into a Query (qryFindFatherName) and specified the query as the
RecordSource for the control (with the same result). The query was:

SELECT * FROM Person WHERE Person.IDPerson=Forms!sfmSibs!IDFather;

This query works well enough when run directly as a query (I get prompted to
input the "Forms!sfmSibs!IDFather"), but not when run from the main form. I
have also tried variations such as "[Forms].[sfmSibs]![IDFather]" in the
query with the same result.

In the RecordSource expression builder, I have:
"=qryFindFatherName!FirstName & " " & qryFindFatherName!Surname.

I think that the problem is that I am not correctly specifying the control
for IDFather in the subform row for the WHERE statement in the query. What
do you think? Helen, please feel free to comment also.

Thanks again.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Lyn, I'm guessing that Access is automatically setting the
LinkMasterFields/LinkChildFields properties of the subform control. You
don't want that, because you are programmatically assigning the records.

Open the main form in design view.
Right-click the edge of the subform control, and choose Properties.
On the Data tab of the Properties box, locate the LinkMasterFields control, and delete the IDPerson entry. Delete the entry for LinkChildFields as well.
I'm going to be away for the weekend, but will check back on Tuesday to see if this solved your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Nov 13 '05 #20
Hi Lyn

Just back, and have had a chance to try what you are doing. The problem you
are seeing is a bug in Access.

Although you have saved the form with *nothing* in the
LinkMasterFields/LinkChildFields of your subform, Access is WRONGLY
assigning values to these properties when you assign the RecordSource of the
subform. The result is that the only subform record that can match your main
form record is itself! This is completely the wrong result for what you want
(i.e. all the *other* siblings except the person themself).

Even if you clear the automatic assignment to these properties, Access will
create the assignment again as soon as you move record in the main form.

There is a sample of this bug at this address:
http://allenbrowne.com/BadSubform.zip
I think you will find that this is exactly the behaviour you have been
struggling with.

You can work around the behaviour by explicitly reassigning an empty string
to the subform's LinkMasterFields and LinkChildFields properties in the main
form's Current event, after you assign the RecordSource to the subform. It's
an extremely inelegant workaround, but it does appear to let you have the
sibblings in your subform.

Looks like another Access bug that needs to be written up.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen, I hope you enjoyed your break.

I have had no luck with the VBA, but Helen has emailed me a solution which
uses neither VBA nor a query (thanks again Helen). It just inserts an SQL
Select statement directly into the RecordSource of the subform. And it
works fine. The statement is:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

While I hadn't thought of inserting the SQL directly into RecordSource, I
did think that I had tried this as a Query. However, I have tried so many
different things trying to resolve this that I can't be sure. In the end,
it was certainly simple enough.

I can't help thinking that the solution via VBA is going to be simple too.
Having come this far, I would still like to find the VBA solution, if only
as a learning exercise. So if you still have the patience, I would like to continue.

One other thing. Rather than displaying the IDFather and IDMother values in the Continuous Feeds subform, I would like to translate each into a text
field inserted into the row (eg, [FirstName] & " " & [Surname]). Using
Helen's solution, I have created a text control in the subform row for
Father. In the RecordSource for the control, I have firstly inserted a
simple SQL statement (which didn't work -- gave me "#Name?"), and then put
the same SQL into a Query (qryFindFatherName) and specified the query as the RecordSource for the control (with the same result). The query was:

SELECT * FROM Person WHERE Person.IDPerson=Forms!sfmSibs!IDFather;

This query works well enough when run directly as a query (I get prompted to input the "Forms!sfmSibs!IDFather"), but not when run from the main form. I have also tried variations such as "[Forms].[sfmSibs]![IDFather]" in the
query with the same result.

In the RecordSource expression builder, I have:
"=qryFindFatherName!FirstName & " " & qryFindFatherName!Surname.

I think that the problem is that I am not correctly specifying the control
for IDFather in the subform row for the WHERE statement in the query. What do you think? Helen, please feel free to comment also.

Thanks again.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Lyn, I'm guessing that Access is automatically setting the
LinkMasterFields/LinkChildFields properties of the subform control. You
don't want that, because you are programmatically assigning the records.

Open the main form in design view.
Right-click the edge of the subform control, and choose Properties.
On the Data tab of the Properties box, locate the LinkMasterFields

control,
and delete the IDPerson entry. Delete the entry for LinkChildFields as

well.

I'm going to be away for the weekend, but will check back on Tuesday to

see
if this solved your issue.

Nov 13 '05 #21
Getting there - more below!

Lyn wrote:
Allen, I hope you enjoyed your break.

I have had no luck with the VBA, but Helen has emailed me a solution which
uses neither VBA nor a query (thanks again Helen). It just inserts an SQL
Select statement directly into the RecordSource of the subform. And it
works fine. The statement is:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

While I hadn't thought of inserting the SQL directly into RecordSource, I
did think that I had tried this as a Query. However, I have tried so many
different things trying to resolve this that I can't be sure. In the end,
it was certainly simple enough.

I can't help thinking that the solution via VBA is going to be simple too.
Having come this far, I would still like to find the VBA solution, if only
as a learning exercise. So if you still have the patience, I would like to
continue.
Yes, I'd agree that setting the subform's record source using VBA
code running on the main form's Current event is a better way to
go. Although the quick and dirty solution I sent you earlier
works, it's not very easily re-usable. What are the odds you'll
want to display grandchildren/nieces & nephews etc later? If you
do, you can re-use the same subform but set a different
recordsource for it from the main form.

So, combining some bits of mine and some bits of Allen's
responses, try putting the following in your main form's
OnCurrent event (watch out for possible line wrapping in your
mail reader and make sure I got the name of your subform right!):

'CODE STARTS HERE:
Dim mySQL As String

'Create the SQL to select people with either the same mother or
'the same father as the current record in the main form.
'We deal with null/unknown parent IDs by checking whether theyre
'equal to an ID number that no person will ever have, i.e. 0

mySQL = "SELECT * FROM Person " & _
"WHERE ((Person.IDFather=" & Nz(Me![IDFather], 0) & ") " & _
"OR (Person.IDMother=" & Nz(Me![IDMother], 0) & ")) " & _
"AND (Not (Person.IDperson=" & Me![IDperson] & "));"

'Assign that SQL string as the record source of the subform
Me![sfmSibs].Form.RecordSource = mySQL

'Deal with Access's rude habit of filling in the link
'master/child fields with values you didn't ask for:
Me![sfmSibs].LinkChildFields = ""
Me![sfmSibs].LinkMasterFields = ""

'CODE ENDS HERE


One other thing. Rather than displaying the IDFather and IDMother values in
the Continuous Feeds subform, I would like to translate each into a text
field inserted into the row (eg, [FirstName] & " " & [Surname]). Using
Helen's solution, I have created a text control in the subform row for
Father. In the RecordSource for the control, I have firstly inserted a
simple SQL statement (which didn't work -- gave me "#Name?"), and then put
the same SQL into a Query (qryFindFatherName) and specified the query as the
RecordSource for the control (with the same result). The query was:


I'd probably join the person table to itself in the subform
record source, once to get the mother's details and a second time
for the father's. The SQL for the subform's recordsource would
then be something like:

mySQL = "SELECT Person.*, " & _
"father.name AS fathername, mother.name AS mothername " & _
"FROM (Person LEFT JOIN person AS father " & _
"ON Person.IDfather = father.idPerson) " & _
"LEFT JOIN person AS mother " & _
"ON Person.IDmother = mother.idPerson " & _
"WHERE ((Person.IDFather=" & Nz(Me![IDFather], 0) & ") " & _
"OR (Person.IDMother=" & Nz(Me![IDMother], 0) & ")) " & _
"AND (Not (Person.IDperson=" & Me![IDperson] & "));"
I'll bet I got at least one typo somewhere in the above, but
hopefully this gets you a bit further along the track. Good luck!
Helen

Nov 13 '05 #22
Lyn
Brilliant, Allen!!!

You have hit the nail right on the head. I have added statements to the VBA
to blank out these fields after the new record has loaded (via the Current
event), and it works perfectly.

As a newbie to Access, I would never have found that!

Now, to finish off, have you any ideas about my other query?
One other thing. Rather than displaying the IDFather and IDMother
values in the Continuous Feeds subform, I would like to translate
each into a text field inserted into the row
(eg, [FirstName] & " " & [Surname]). Using Helen's solution,
I have created a text control in the subform row for Father.
In the RecordSource for the control, I have firstly inserted a
simple SQL statement (which didn't work -- gave me
"#Name?"), and then put the same SQL into a Query
(qryFindFatherName) and specified the query as the
RecordSource for the control (with the same result).
The query was:

SELECT * FROM Person WHERE Person.IDPerson=Forms!sfmSibs!IDFather;

This query works well enough when run directly as a query (I get prompted
to input the "Forms!sfmSibs!IDFather"), but not when run from the
main form. I have also tried variations such as "[Forms].[sfmSibs]![IDFather]" in the query with the same result.

In the RecordSource expression builder, I have:
"=qryFindFatherName!FirstName & " " & qryFindFatherName!Surname.

I think that the problem is that I am not correctly specifying the control
for IDFather in the subform row for the WHERE statement in the query.
What do you think?
A VBA solution would also be helpful as I also want to translate the Sex
value (Male/Female) to Relationship
(Brother/Half-Brother/Sister/Half-Sister) in the Siblings subform. Once I
know how to access the values returned in each row, translating Sex to
Relationship will be fairly simple VBA.

Many thanks again!
--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@per-qv1-newsreader-01.iinet.net.au... Hi Lyn

Just back, and have had a chance to try what you are doing. The problem you are seeing is a bug in Access.

Although you have saved the form with *nothing* in the
LinkMasterFields/LinkChildFields of your subform, Access is WRONGLY
assigning values to these properties when you assign the RecordSource of the subform. The result is that the only subform record that can match your main form record is itself! This is completely the wrong result for what you want (i.e. all the *other* siblings except the person themself).

Even if you clear the automatic assignment to these properties, Access will create the assignment again as soon as you move record in the main form.

There is a sample of this bug at this address:
http://allenbrowne.com/BadSubform.zip
I think you will find that this is exactly the behaviour you have been
struggling with.

You can work around the behaviour by explicitly reassigning an empty string to the subform's LinkMasterFields and LinkChildFields properties in the main form's Current event, after you assign the RecordSource to the subform. It's an extremely inelegant workaround, but it does appear to let you have the
sibblings in your subform.

Looks like another Access bug that needs to be written up.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen, I hope you enjoyed your break.

I have had no luck with the VBA, but Helen has emailed me a solution which uses neither VBA nor a query (thanks again Helen). It just inserts an SQL Select statement directly into the RecordSource of the subform. And it
works fine. The statement is:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

While I hadn't thought of inserting the SQL directly into RecordSource, I did think that I had tried this as a Query. However, I have tried so many different things trying to resolve this that I can't be sure. In the end, it was certainly simple enough.

I can't help thinking that the solution via VBA is going to be simple too. Having come this far, I would still like to find the VBA solution, if only as a learning exercise. So if you still have the patience, I would like to
continue.

One other thing. Rather than displaying the IDFather and IDMother values in
the Continuous Feeds subform, I would like to translate each into a text
field inserted into the row (eg, [FirstName] & " " & [Surname]). Using
Helen's solution, I have created a text control in the subform row for
Father. In the RecordSource for the control, I have firstly inserted a
simple SQL statement (which didn't work -- gave me "#Name?"), and then
put the same SQL into a Query (qryFindFatherName) and specified the query as

the
RecordSource for the control (with the same result). The query was:

SELECT * FROM Person WHERE Person.IDPerson=Forms!sfmSibs!IDFather;

This query works well enough when run directly as a query (I get prompted to
input the "Forms!sfmSibs!IDFather"), but not when run from the main
form. I
have also tried variations such as "[Forms].[sfmSibs]![IDFather]" in the
query with the same result.

In the RecordSource expression builder, I have:
"=qryFindFatherName!FirstName & " " & qryFindFatherName!Surname.

I think that the problem is that I am not correctly specifying the

control for IDFather in the subform row for the WHERE statement in the query.

What
do you think? Helen, please feel free to comment also.

Thanks again.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
Lyn, I'm guessing that Access is automatically setting the
LinkMasterFields/LinkChildFields properties of the subform control. You don't want that, because you are programmatically assigning the records.
Open the main form in design view.
Right-click the edge of the subform control, and choose Properties.
On the Data tab of the Properties box, locate the LinkMasterFields

control,
and delete the IDPerson entry. Delete the entry for LinkChildFields as

well.

I'm going to be away for the weekend, but will check back on Tuesday
to see
if this solved your issue.


Nov 13 '05 #23
Hi Lyn

You should be able to use a query statement like this one:

SELECT tblPerson.*,
Trim([Father].[FirstName] & " " & [Father].[Surname]) AS FatherName,
Trim([Mother].[FirstName] & " " & [Mother].[Surname]) AS MotherName
FROM (tblPerson LEFT JOIN tblPerson AS Father ON tblPerson.IDFather =
Father.IDPerson)
LEFT JOIN tblPerson AS Mother ON tblPerson.IDMother = Mother.IDPerson;

You can paste that into SQL View of a query (View menu in query design), to
see how it works, and modify it to suit.

Essentially, the query contains 3 copies of the same table. Two copies are
aliased so they are meaningful. (Set an Alias in the Properties box - view
menu).

We use outer joins in case the parents are unknown. (Double-click the join
lines to set the join type.) Provided you are not trying to enter the
parents names, you should find that this query is updatable, so suitable for
your form.

I think Helen is suggesting something similar.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Brilliant, Allen!!!

You have hit the nail right on the head. I have added statements to the VBA to blank out these fields after the new record has loaded (via the Current
event), and it works perfectly.

As a newbie to Access, I would never have found that!

Now, to finish off, have you any ideas about my other query?
One other thing. Rather than displaying the IDFather and IDMother
values in the Continuous Feeds subform, I would like to translate
each into a text field inserted into the row
(eg, [FirstName] & " " & [Surname]). Using Helen's solution,
I have created a text control in the subform row for Father.
In the RecordSource for the control, I have firstly inserted a
simple SQL statement (which didn't work -- gave me
"#Name?"), and then put the same SQL into a Query
(qryFindFatherName) and specified the query as the
RecordSource for the control (with the same result).
The query was:

SELECT * FROM Person WHERE Person.IDPerson=Forms!sfmSibs!IDFather;

This query works well enough when run directly as a query (I get prompted
to input the "Forms!sfmSibs!IDFather"), but not when run from the
main form. I have also tried variations such as "[Forms].[sfmSibs]![IDFather]"
in the query with the same result.

In the RecordSource expression builder, I have:
"=qryFindFatherName!FirstName & " " & qryFindFatherName!Surname.

I think that the problem is that I am not correctly specifying the control for IDFather in the subform row for the WHERE statement in the query.
What do you think?


A VBA solution would also be helpful as I also want to translate the Sex
value (Male/Female) to Relationship
(Brother/Half-Brother/Sister/Half-Sister) in the Siblings subform. Once I
know how to access the values returned in each row, translating Sex to
Relationship will be fairly simple VBA.

Many thanks again!
--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@per-qv1-newsreader-01.iinet.net.au...
Hi Lyn

Just back, and have had a chance to try what you are doing. The problem

you
are seeing is a bug in Access.

Although you have saved the form with *nothing* in the
LinkMasterFields/LinkChildFields of your subform, Access is WRONGLY
assigning values to these properties when you assign the RecordSource of

the
subform. The result is that the only subform record that can match your

main
form record is itself! This is completely the wrong result for what you

want
(i.e. all the *other* siblings except the person themself).

Even if you clear the automatic assignment to these properties, Access

will
create the assignment again as soon as you move record in the main form.

There is a sample of this bug at this address:
http://allenbrowne.com/BadSubform.zip
I think you will find that this is exactly the behaviour you have been
struggling with.

You can work around the behaviour by explicitly reassigning an empty

string
to the subform's LinkMasterFields and LinkChildFields properties in the

main
form's Current event, after you assign the RecordSource to the subform.

It's
an extremely inelegant workaround, but it does appear to let you have the sibblings in your subform.

Looks like another Access bug that needs to be written up.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Lyn" <lh******@ihug.com.au> wrote in message
news:ca**********@lust.ihug.co.nz...
Allen, I hope you enjoyed your break.

I have had no luck with the VBA, but Helen has emailed me a solution which uses neither VBA nor a query (thanks again Helen). It just inserts an SQL Select statement directly into the RecordSource of the subform. And it works fine. The statement is:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

While I hadn't thought of inserting the SQL directly into RecordSource, I
did think that I had tried this as a Query. However, I have tried so many different things trying to resolve this that I can't be sure. In the end, it was certainly simple enough.

I can't help thinking that the solution via VBA is going to be simple too. Having come this far, I would still like to find the VBA solution, if only as a learning exercise. So if you still have the patience, I would
like to
continue.

One other thing. Rather than displaying the IDFather and IDMother values
in
the Continuous Feeds subform, I would like to translate each into a

text field inserted into the row (eg, [FirstName] & " " & [Surname]). Using Helen's solution, I have created a text control in the subform row for
Father. In the RecordSource for the control, I have firstly inserted a simple SQL statement (which didn't work -- gave me "#Name?"), and then

put the same SQL into a Query (qryFindFatherName) and specified the query as the
RecordSource for the control (with the same result). The query was:

SELECT * FROM Person WHERE Person.IDPerson=Forms!sfmSibs!IDFather;

This query works well enough when run directly as a query (I get prompted
to
input the "Forms!sfmSibs!IDFather"), but not when run from the main

form.
I
have also tried variations such as "[Forms].[sfmSibs]![IDFather]" in

the query with the same result.

In the RecordSource expression builder, I have:
"=qryFindFatherName!FirstName & " " & qryFindFatherName!Surname.

I think that the problem is that I am not correctly specifying the

control for IDFather in the subform row for the WHERE statement in the query.

What
do you think? Helen, please feel free to comment also.

Thanks again.

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@freenews.iinet.net. au...
> Lyn, I'm guessing that Access is automatically setting the
> LinkMasterFields/LinkChildFields properties of the subform control. You > don't want that, because you are programmatically assigning the records. >
> Open the main form in design view.
> Right-click the edge of the subform control, and choose Properties.
> On the Data tab of the Properties box, locate the LinkMasterFields
control,
> and delete the IDPerson entry. Delete the entry for LinkChildFields as well.
>
> I'm going to be away for the weekend, but will check back on Tuesday to see
> if this solved your issue.

Nov 13 '05 #24
Lyn
Helen, apologies but I did not see your latest post before I replied to
Allen. I have in fact got the VBA side of the Siblings query (and a similar
Children query) working with code very similar to what you have suggested.

Regarding my new query, it seems that you and Allen are both on the same
page, so I will work on that now and let you both know how it turns out.

Thanks again to you both.
--
Cheers,
Lyn.

"Helen Wheels" <he**********@yahoo.com.au> wrote in message
news:40**************@yahoo.com.au...
Getting there - more below!

Lyn wrote:
Allen, I hope you enjoyed your break.

I have had no luck with the VBA, but Helen has emailed me a solution which uses neither VBA nor a query (thanks again Helen). It just inserts an SQL Select statement directly into the RecordSource of the subform. And it
works fine. The statement is:

SELECT * FROM Person WHERE
((Person.IDFather=[Forms]![frmMainForm]![IDFather]) OR
(Person.IDMother=[Forms]![frmMainForm]![IDMother]))
AND (Not (Person.IDperson=[Forms]![frmMainForm]![IDperson]));

While I hadn't thought of inserting the SQL directly into RecordSource, I did think that I had tried this as a Query. However, I have tried so many different things trying to resolve this that I can't be sure. In the end, it was certainly simple enough.

I can't help thinking that the solution via VBA is going to be simple too. Having come this far, I would still like to find the VBA solution, if only as a learning exercise. So if you still have the patience, I would like to continue.


Yes, I'd agree that setting the subform's record source using VBA
code running on the main form's Current event is a better way to
go. Although the quick and dirty solution I sent you earlier
works, it's not very easily re-usable. What are the odds you'll
want to display grandchildren/nieces & nephews etc later? If you
do, you can re-use the same subform but set a different
recordsource for it from the main form.

So, combining some bits of mine and some bits of Allen's
responses, try putting the following in your main form's
OnCurrent event (watch out for possible line wrapping in your
mail reader and make sure I got the name of your subform right!):

'CODE STARTS HERE:
Dim mySQL As String

'Create the SQL to select people with either the same mother or
'the same father as the current record in the main form.
'We deal with null/unknown parent IDs by checking whether theyre
'equal to an ID number that no person will ever have, i.e. 0

mySQL = "SELECT * FROM Person " & _
"WHERE ((Person.IDFather=" & Nz(Me![IDFather], 0) & ") " & _
"OR (Person.IDMother=" & Nz(Me![IDMother], 0) & ")) " & _
"AND (Not (Person.IDperson=" & Me![IDperson] & "));"

'Assign that SQL string as the record source of the subform
Me![sfmSibs].Form.RecordSource = mySQL

'Deal with Access's rude habit of filling in the link
'master/child fields with values you didn't ask for:
Me![sfmSibs].LinkChildFields = ""
Me![sfmSibs].LinkMasterFields = ""

'CODE ENDS HERE


One other thing. Rather than displaying the IDFather and IDMother values in the Continuous Feeds subform, I would like to translate each into a text
field inserted into the row (eg, [FirstName] & " " & [Surname]). Using
Helen's solution, I have created a text control in the subform row for
Father. In the RecordSource for the control, I have firstly inserted a
simple SQL statement (which didn't work -- gave me "#Name?"), and then put the same SQL into a Query (qryFindFatherName) and specified the query as the RecordSource for the control (with the same result). The query was:


I'd probably join the person table to itself in the subform
record source, once to get the mother's details and a second time
for the father's. The SQL for the subform's recordsource would
then be something like:

mySQL = "SELECT Person.*, " & _
"father.name AS fathername, mother.name AS mothername " & _
"FROM (Person LEFT JOIN person AS father " & _
"ON Person.IDfather = father.idPerson) " & _
"LEFT JOIN person AS mother " & _
"ON Person.IDmother = mother.idPerson " & _
"WHERE ((Person.IDFather=" & Nz(Me![IDFather], 0) & ") " & _
"OR (Person.IDMother=" & Nz(Me![IDMother], 0) & ")) " & _
"AND (Not (Person.IDperson=" & Me![IDperson] & "));"
I'll bet I got at least one typo somewhere in the above, but
hopefully this gets you a bit further along the track. Good luck!
Helen

Nov 13 '05 #25
Lyn
Allen, Helen,
Thanks to you both, I now have the Siblings subform working as desired.

I really appreciate your patience and perseverance in seeing this through
with me.

Best regards.
--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:40***********************@per-qv1-newsreader-01.iinet.net.au...
Hi Lyn

You should be able to use a query statement like this one:

SELECT tblPerson.*,
Trim([Father].[FirstName] & " " & [Father].[Surname]) AS FatherName,
Trim([Mother].[FirstName] & " " & [Mother].[Surname]) AS MotherName
FROM (tblPerson LEFT JOIN tblPerson AS Father ON tblPerson.IDFather =
Father.IDPerson)
LEFT JOIN tblPerson AS Mother ON tblPerson.IDMother = Mother.IDPerson;

You can paste that into SQL View of a query (View menu in query design), to see how it works, and modify it to suit.

Essentially, the query contains 3 copies of the same table. Two copies are
aliased so they are meaningful. (Set an Alias in the Properties box - view
menu).

We use outer joins in case the parents are unknown. (Double-click the join
lines to set the join type.) Provided you are not trying to enter the
parents names, you should find that this query is updatable, so suitable for your form.

I think Helen is suggesting something similar.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Nov 13 '05 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bruno Luis | last post by:
Hello I'm using A97 and i'm having some trouble with creating a recordset with data from two different databases. I connect to a remote database and make this data the default source for my...
2
by: Galina | last post by:
Hello I have an application in MS Access 2000. I have a form, which includes a subform. The subform is based on a table, but locked for any editing. There is Edit button. When clicked, it starts a...
0
by: CSDunn | last post by:
Hello, In Access ADP's that connect to SQL Server databases, any time I have a situation where I have a combo box in a main form that looks up a record in a subform, the subform record source has...
4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
3
by: shumaker | last post by:
This code from the subform works for getting the value of a field of the main form named "WorkSheet": MsgBox Form_WorkSheet.Recordset.Fields("Clerk").Value Each record in the mainform datasheet...
6
by: GSteven | last post by:
(as formerly posted to microsoft.public.access.forms with no result) I've created a continuous form which is based on a straightforward table (ex - customers - 100 records). On the form there is...
6
by: CPAccess | last post by:
How do I maintain referential integrity between a main form and a subform, each based upon different (but joined with integrity enforced) table? Here's the situation: I have two tables:...
14
by: ml_sauls | last post by:
I've built a system to enter and manage purchase orders. This is in use by >10 clients. Some use it in Access 97, most are in A2k. About half use it through a Citrix implementation. It is...
7
by: dscarbor | last post by:
I have a simple form with 4 fields, and a subform that retrieves records that are potential matches based on a query. The user will use the ID from the subform record and enter it into one of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.