changing record source of subform

  • Thread starter Thread starter Rod
  • Start date Start date
R

Rod

From the code behind a form I want to be able to change the record source
for a subform

something like this.


SQLstr="Select ......"

Me.MySubform.Form.RecordSource=SQLstr
Me.MySubform.Form.RecordSource.Requery

but it don't work.

I am very sure the SQL is fine because I took it from the subform property.


any help much appreciated.
 
When you say, "it don't work", can you be more specific? Does it fail with
an error? Return no records? Return wrong records?

Presumably this is executed from the main form's module. What event? Click
of a button?

Is there any chance the subform could be dirty at the time when this
executes? (It would not be from user entry, but could be programmatically
dirty.)

You "took it from the subform property", so the assignment does work. Did
you also read the LinkMasterFields and LinkChildFields properties of the
subform control? When you assign a RecordSource, Access assigns whatever it
feels like to these properties.
 
Allen Browne said:
When you say, "it don't work", can you be more specific? Does it fail with
an error? Return no records? Return wrong records?

The subform has a Select without Where clause for the record source.
I am trying to set it to a Select with a Where clause. This is not
happening, the original Select is being used.
Presumably this is executed from the main form's module. What event? Click
of a button?


The sub form is on a tab control. The event is changing tabs. I know the
event is fired.
Is there any chance the subform could be dirty at the time when this
executes? (It would not be from user entry, but could be programmatically
dirty.)

I'm not smart enough to answer this question.

You "took it from the subform property", so the assignment does work. Did
you also read the LinkMasterFields and LinkChildFields properties of the
subform control? When you assign a RecordSource, Access assigns whatever it
feels like to these properties.


OK, I have now added
Me.MySubform.LinkChildFields = "Venue ID"
Me.MySubform.LinkMasterFields = "Venue ID"

with no change. The right linking is taking place.
 
Rod, it might help if you post the complete event procedure for the Change
event of the tab control.

We can then suggest some Debug.Print statements, or breakpoints so you can
verify what is executing and what is failing.
 
Private Sub NotesTab_Change()

Dim TabValue As Long, SQLs As String

TabValue = Me.NotesTab.Value 'not used but will replace the "1" in SQL
below


SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE ((([Call Log].[Staff
ID])=" & 1 & ") And (([Call Log].Hide)=False)) ORDER BY [Call Log].[Date
Time] DESC;"

Me.Call_Log_Subform_in_Venue_Form.Form.RecordSource = SQLs
Me.Call_Log_Subform_in_Venue_Form.LinkChildFields = "Venue ID"
Me.Call_Log_Subform_in_Venue_Form.LinkMasterFields = "Venue ID"

Me.Call_Log_Subform_in_Venue_Form.Requery

End Sub


if I break on the end sub and
debug.? Me......RecordSource
I get the correct SQLs
If I cut and paste that sql into a query it gives the records I expect.

It is as if the requery does not work.
 
Okay, let's explicity save anything that's doing on on the main form, and
check that it is not at a new record (which would generate orphans in the
subform).

Then after assigning the RecordSource to the subform, get Access to tell us
the results:

Private Sub NotesTab_Change()
Dim TabValue As Long, SQLs As String

If Me.Dirty Then
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Not changed"
Else
TabValue = Me.NotesTab.Value
SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE (([Call
Log].[Staff ID]=" & TabValue & ") And ([Call Log].Hide = False)) ORDER BY
[Call Log].[Date Time] DESC;"

With Me.Call_Log_Subform_in_Venue_Form
.Form.RecordSource = SQLs
Debug.Print "For StaffID " & TabValue & " at " & Now()
Debug.Print "RecordSource: " & .Form.RecordSource
Debug.Print "LinkChildFields: " & .LinkChildFields
Debug.Print "LinkMasterFields: " & .LinkMasterFields
With .Form.RecordsetClone
If .RecordCount > 0 Then
.MoveLast
End If
Debug.Print .RecordCount & " record(s)"
End With
Debug.Print
End With
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.

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

Rod said:
Private Sub NotesTab_Change()

Dim TabValue As Long, SQLs As String

TabValue = Me.NotesTab.Value 'not used but will replace the "1" in SQL
below


SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE ((([Call Log].[Staff
ID])=" & 1 & ") And (([Call Log].Hide)=False)) ORDER BY [Call Log].[Date
Time] DESC;"

Me.Call_Log_Subform_in_Venue_Form.Form.RecordSource = SQLs
Me.Call_Log_Subform_in_Venue_Form.LinkChildFields = "Venue ID"
Me.Call_Log_Subform_in_Venue_Form.LinkMasterFields = "Venue ID"

Me.Call_Log_Subform_in_Venue_Form.Requery

End Sub


if I break on the end sub and
debug.? Me......RecordSource
I get the correct SQLs
If I cut and paste that sql into a query it gives the records I expect.

It is as if the requery does not work.


Allen Browne said:
Rod, it might help if you post the complete event procedure for the
Change
event of the tab control.

We can then suggest some Debug.Print statements, or breakpoints so you
can
verify what is executing and what is failing.
 
Here is the debug info:

For StaffID 5 at 17/05/2006 08:59:43
RecordSource: SELECT [Call Log].* FROM [Call Log] WHERE (([Call Log].[Staff
ID]=5) And ([Call Log].Hide = False)) ORDER BY [Call Log].[Date Time] DESC;
LinkChildFields: Venue ID
LinkMasterFields: Venue ID
2 record(s)

Two records is correct for this Venue ID.
But this is not what I am seeing on the subform.
I see whatever I set the recordsource to be in the saved version of the
subform
If I delete the recordsource in the saved version of the subform then I just
get #Name? in the fields. (as you would expect for a form without a
recordsource).

PS
(very much appreciate the effort you are making, I have to go out for much
of today for a meeting so please do not take lack of response on my part for
lack of interest!)



Allen Browne said:
Okay, let's explicity save anything that's doing on on the main form, and
check that it is not at a new record (which would generate orphans in the
subform).

Then after assigning the RecordSource to the subform, get Access to tell us
the results:

Private Sub NotesTab_Change()
Dim TabValue As Long, SQLs As String

If Me.Dirty Then
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Not changed"
Else
TabValue = Me.NotesTab.Value
SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE (([Call
Log].[Staff ID]=" & TabValue & ") And ([Call Log].Hide = False)) ORDER BY
[Call Log].[Date Time] DESC;"

With Me.Call_Log_Subform_in_Venue_Form
.Form.RecordSource = SQLs
Debug.Print "For StaffID " & TabValue & " at " & Now()
Debug.Print "RecordSource: " & .Form.RecordSource
Debug.Print "LinkChildFields: " & .LinkChildFields
Debug.Print "LinkMasterFields: " & .LinkMasterFields
With .Form.RecordsetClone
If .RecordCount > 0 Then
.MoveLast
End If
Debug.Print .RecordCount & " record(s)"
End With
Debug.Print
End With
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.

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

Rod said:
Private Sub NotesTab_Change()

Dim TabValue As Long, SQLs As String

TabValue = Me.NotesTab.Value 'not used but will replace the "1" in SQL
below


SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE ((([Call Log].[Staff
ID])=" & 1 & ") And (([Call Log].Hide)=False)) ORDER BY [Call Log].[Date
Time] DESC;"

Me.Call_Log_Subform_in_Venue_Form.Form.RecordSource = SQLs
Me.Call_Log_Subform_in_Venue_Form.LinkChildFields = "Venue ID"
Me.Call_Log_Subform_in_Venue_Form.LinkMasterFields = "Venue ID"

Me.Call_Log_Subform_in_Venue_Form.Requery

End Sub


if I break on the end sub and
debug.? Me......RecordSource
I get the correct SQLs
If I cut and paste that sql into a query it gives the records I expect.

It is as if the requery does not work.


Allen Browne said:
Rod, it might help if you post the complete event procedure for the
Change
event of the tab control.

We can then suggest some Debug.Print statements, or breakpoints so you
can
verify what is executing and what is failing.


When you say, "it don't work", can you be more specific? Does it fail
with
an error? Return no records? Return wrong records?

The subform has a Select without Where clause for the record source.
I am trying to set it to a Select with a Where clause. This is not
happening, the original Select is being used.


Presumably this is executed from the main form's module. What event?
Click
of a button?


The sub form is on a tab control. The event is changing tabs. I know
the
event is fired.


Is there any chance the subform could be dirty at the time when this
executes? (It would not be from user entry, but could be programmatically
dirty.)

I'm not smart enough to answer this question.



You "took it from the subform property", so the assignment does
work.
Did
you also read the LinkMasterFields and LinkChildFields properties of the
subform control? When you assign a RecordSource, Access assigns whatever
it
feels like to these properties.


OK, I have now added
Me.MySubform.LinkChildFields = "Venue ID"
Me.MySubform.LinkMasterFields = "Venue ID"

with no change. The right linking is taking place.


From the code behind a form I want to be able to change the record
source
for a subform

something like this.


SQLstr="Select ......"

Me.MySubform.Form.RecordSource=SQLstr
Me.MySubform.Form.RecordSource.Requery

but it don't work.

I am very sure the SQL is fine because I took it from the subform
property.
 
Rod, this is a really long shot, but is there any chance that the subform
control named "Call_Log_Subform_in_Venue_Form" is not the one you think it
is? You do realize that the Name of the subform control can be different
from the name of the form loaded into it (its SourceObject)?

If you have the names, right, perhaps Access has them wrong. Uncheck the
boxes under:
Tools | Options | General | Name AutoCorrect
Then compact the database:
Tools | Database Utilities | Compact
Does that make any difference?

If neither of these is the case, there must be something else that is
interferring--another event, piece of code, or side-effect of something
else.

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

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

Rod said:
Here is the debug info:

For StaffID 5 at 17/05/2006 08:59:43
RecordSource: SELECT [Call Log].* FROM [Call Log] WHERE (([Call
Log].[Staff
ID]=5) And ([Call Log].Hide = False)) ORDER BY [Call Log].[Date Time]
DESC;
LinkChildFields: Venue ID
LinkMasterFields: Venue ID
2 record(s)

Two records is correct for this Venue ID.
But this is not what I am seeing on the subform.
I see whatever I set the recordsource to be in the saved version of the
subform
If I delete the recordsource in the saved version of the subform then I
just
get #Name? in the fields. (as you would expect for a form without a
recordsource).

PS
(very much appreciate the effort you are making, I have to go out for much
of today for a meeting so please do not take lack of response on my part
for
lack of interest!)



Allen Browne said:
Okay, let's explicity save anything that's doing on on the main form, and
check that it is not at a new record (which would generate orphans in the
subform).

Then after assigning the RecordSource to the subform, get Access to tell us
the results:

Private Sub NotesTab_Change()
Dim TabValue As Long, SQLs As String

If Me.Dirty Then
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Not changed"
Else
TabValue = Me.NotesTab.Value
SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE (([Call
Log].[Staff ID]=" & TabValue & ") And ([Call Log].Hide = False)) ORDER BY
[Call Log].[Date Time] DESC;"

With Me.Call_Log_Subform_in_Venue_Form
.Form.RecordSource = SQLs
Debug.Print "For StaffID " & TabValue & " at " & Now()
Debug.Print "RecordSource: " & .Form.RecordSource
Debug.Print "LinkChildFields: " & .LinkChildFields
Debug.Print "LinkMasterFields: " & .LinkMasterFields
With .Form.RecordsetClone
If .RecordCount > 0 Then
.MoveLast
End If
Debug.Print .RecordCount & " record(s)"
End With
Debug.Print
End With
End If
End Sub
Rod said:
Private Sub NotesTab_Change()

Dim TabValue As Long, SQLs As String

TabValue = Me.NotesTab.Value 'not used but will replace the "1" in SQL
below


SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE ((([Call Log].[Staff
ID])=" & 1 & ") And (([Call Log].Hide)=False)) ORDER BY [Call
Log].[Date
Time] DESC;"

Me.Call_Log_Subform_in_Venue_Form.Form.RecordSource = SQLs
Me.Call_Log_Subform_in_Venue_Form.LinkChildFields = "Venue ID"
Me.Call_Log_Subform_in_Venue_Form.LinkMasterFields = "Venue ID"

Me.Call_Log_Subform_in_Venue_Form.Requery

End Sub


if I break on the end sub and
debug.? Me......RecordSource
I get the correct SQLs
If I cut and paste that sql into a query it gives the records I expect.

It is as if the requery does not work.


Rod, it might help if you post the complete event procedure for the
Change
event of the tab control.

We can then suggest some Debug.Print statements, or breakpoints so you
can
verify what is executing and what is failing.


When you say, "it don't work", can you be more specific? Does it fail
with
an error? Return no records? Return wrong records?

The subform has a Select without Where clause for the record source.
I am trying to set it to a Select with a Where clause. This is not
happening, the original Select is being used.


Presumably this is executed from the main form's module. What
event?
Click
of a button?


The sub form is on a tab control. The event is changing tabs. I know
the
event is fired.


Is there any chance the subform could be dirty at the time when
this
executes? (It would not be from user entry, but could be
programmatically
dirty.)

I'm not smart enough to answer this question.



You "took it from the subform property", so the assignment does work.
Did
you also read the LinkMasterFields and LinkChildFields properties
of
the
subform control? When you assign a RecordSource, Access assigns
whatever
it
feels like to these properties.


OK, I have now added
Me.MySubform.LinkChildFields = "Venue ID"
Me.MySubform.LinkMasterFields = "Venue ID"

with no change. The right linking is taking place.


From the code behind a form I want to be able to change the
record
source
for a subform

something like this.


SQLstr="Select ......"

Me.MySubform.Form.RecordSource=SQLstr
Me.MySubform.Form.RecordSource.Requery

but it don't work.

I am very sure the SQL is fine because I took it from the subform
property.
 
Turning off the autocorrect and compacting has solved it.
Thanks a million for your time and perseverance.

Rod

Allen Browne said:
Rod, this is a really long shot, but is there any chance that the subform
control named "Call_Log_Subform_in_Venue_Form" is not the one you think it
is? You do realize that the Name of the subform control can be different
from the name of the form loaded into it (its SourceObject)?

If you have the names, right, perhaps Access has them wrong. Uncheck the
boxes under:
Tools | Options | General | Name AutoCorrect
Then compact the database:
Tools | Database Utilities | Compact
Does that make any difference?

If neither of these is the case, there must be something else that is
interferring--another event, piece of code, or side-effect of something
else.

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

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

Rod said:
Here is the debug info:

For StaffID 5 at 17/05/2006 08:59:43
RecordSource: SELECT [Call Log].* FROM [Call Log] WHERE (([Call
Log].[Staff
ID]=5) And ([Call Log].Hide = False)) ORDER BY [Call Log].[Date Time]
DESC;
LinkChildFields: Venue ID
LinkMasterFields: Venue ID
2 record(s)

Two records is correct for this Venue ID.
But this is not what I am seeing on the subform.
I see whatever I set the recordsource to be in the saved version of the
subform
If I delete the recordsource in the saved version of the subform then I
just
get #Name? in the fields. (as you would expect for a form without a
recordsource).

PS
(very much appreciate the effort you are making, I have to go out for much
of today for a meeting so please do not take lack of response on my part
for
lack of interest!)



Allen Browne said:
Okay, let's explicity save anything that's doing on on the main form, and
check that it is not at a new record (which would generate orphans in the
subform).

Then after assigning the RecordSource to the subform, get Access to
tell
us
the results:

Private Sub NotesTab_Change()
Dim TabValue As Long, SQLs As String

If Me.Dirty Then
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Not changed"
Else
TabValue = Me.NotesTab.Value
SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE (([Call
Log].[Staff ID]=" & TabValue & ") And ([Call Log].Hide = False)) ORDER BY
[Call Log].[Date Time] DESC;"

With Me.Call_Log_Subform_in_Venue_Form
.Form.RecordSource = SQLs
Debug.Print "For StaffID " & TabValue & " at " & Now()
Debug.Print "RecordSource: " & .Form.RecordSource
Debug.Print "LinkChildFields: " & .LinkChildFields
Debug.Print "LinkMasterFields: " & .LinkMasterFields
With .Form.RecordsetClone
If .RecordCount > 0 Then
.MoveLast
End If
Debug.Print .RecordCount & " record(s)"
End With
Debug.Print
End With
End If
End Sub
Private Sub NotesTab_Change()

Dim TabValue As Long, SQLs As String

TabValue = Me.NotesTab.Value 'not used but will replace the "1" in SQL
below


SQLs = "SELECT [Call Log].* FROM [Call Log] WHERE ((([Call Log].[Staff
ID])=" & 1 & ") And (([Call Log].Hide)=False)) ORDER BY [Call
Log].[Date
Time] DESC;"

Me.Call_Log_Subform_in_Venue_Form.Form.RecordSource = SQLs
Me.Call_Log_Subform_in_Venue_Form.LinkChildFields = "Venue ID"
Me.Call_Log_Subform_in_Venue_Form.LinkMasterFields = "Venue ID"

Me.Call_Log_Subform_in_Venue_Form.Requery

End Sub


if I break on the end sub and
debug.? Me......RecordSource
I get the correct SQLs
If I cut and paste that sql into a query it gives the records I expect.

It is as if the requery does not work.


Rod, it might help if you post the complete event procedure for the
Change
event of the tab control.

We can then suggest some Debug.Print statements, or breakpoints so you
can
verify what is executing and what is failing.


When you say, "it don't work", can you be more specific? Does it fail
with
an error? Return no records? Return wrong records?

The subform has a Select without Where clause for the record source.
I am trying to set it to a Select with a Where clause. This is not
happening, the original Select is being used.


Presumably this is executed from the main form's module. What
event?
Click
of a button?


The sub form is on a tab control. The event is changing tabs. I know
the
event is fired.


Is there any chance the subform could be dirty at the time when
this
executes? (It would not be from user entry, but could be
programmatically
dirty.)

I'm not smart enough to answer this question.



You "took it from the subform property", so the assignment does work.
Did
you also read the LinkMasterFields and LinkChildFields properties
of
the
subform control? When you assign a RecordSource, Access assigns
whatever
it
feels like to these properties.


OK, I have now added
Me.MySubform.LinkChildFields = "Venue ID"
Me.MySubform.LinkMasterFields = "Venue ID"

with no change. The right linking is taking place.


From the code behind a form I want to be able to change the
record
source
for a subform

something like this.


SQLstr="Select ......"

Me.MySubform.Form.RecordSource=SQLstr
Me.MySubform.Form.RecordSource.Requery

but it don't work.

I am very sure the SQL is fine because I took it from the subform
property.
 
Back
Top