Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically changing recordsource of a subform

Status
Not open for further replies.

bsagal

Technical User
Jul 17, 2003
35
CA
Hello,

I have the following code i am using to change the recordsource of a subform at run time:

Dim recordsource
Set db = CurrentDb
Set recordsource = db.OpenRecordset(??????)
Me![employee].Form.recordsource = ??????

I want to use the query named "Employee" as my recordsource, but what is the syntax for using a query?

Thanks!
 
Hi bsagal,

In the On Load event of your Main form:

Me!YourSubformName.Form.RecordSource = "Employee"

Bill
 
Bill

I want to do something similar without a subform, but tie the record source to a toggle button.

The default record source will be a SQL statement that I have. Then, if the user clicks the button I want to requery the SQL to set the filter to that user's info only.

Can this be done?

Thanks!

Jim DeGeorge [wavey]
 
Bill

I tried the following code behind the toggle button's OnClick property:

If Me.tglFilter = 0 Then
Me.RecordSource = "SELECT tblGaps.* FROM tblGaps WHERE tblGaps.DateClosed Is Null " _
& "AND tblGaps.Resolution Is Null AND tblGaps.Conversion=[Forms]![frmSignOn]![ctlConversion] " _
& "ORDER BY tblGaps.[Gap#];"
Else
Me.RecordSource = "SELECT tblGaps.* FROM tblGaps WHERE tblGaps.DateClosed Is Null " _
& "AND tblGaps.Resolution Is Null AND tblGaps.Conversion=[Forms]![frmSignOn]![ctlConversion] " _
& "AND tblGaps.NFS_Owner=[Forms]![frmSignOn]![SignOn] " _
& "ORDER BY tblGaps.[Gap#];"
End If

Me.Requery

The form's OnLoad property has this code:

Me.RecordSource = "SELECT tblGaps.* FROM tblGaps WHERE tblGaps.DateClosed Is Null " _
& "AND tblGaps.Resolution Is Null AND tblGaps.Conversion=[Forms]![frmSignOn]![ctlConversion] " _
& "ORDER BY tblGaps.[Gap#];"

It works great.

If you can think of a better way to do this, I'm open for ideas. Thanks!

Jim DeGeorge [wavey]
 
Hi jdegeorge,

I can't improve on your method other than to shorten your queries:

Select * instead of SELECT tblGaps.*

Have removed the table name from the query as you are only querying 1 table.

To refer to a control on a Form/Report use !

This site shows how to refer to Form/Report controls and properties:
If Me!tglFilter = 0 Then
Me.RecordSource = "SELECT * FROM tblGaps WHERE DateClosed Is Null " _
& "AND Resolution Is Null AND Conversion=[Forms]![frmSignOn]![ctlConversion] " _
& "ORDER BY [Gap#];"
Else
Me.RecordSource = "SELECT * FROM tblGaps WHERE DateClosed Is Null " _
& "AND Resolution Is Null AND Conversion=[Forms]![frmSignOn]![ctlConversion] " _
& "AND NFS_Owner=[Forms]![frmSignOn]![SignOn] " _
& "ORDER BY [Gap#];"
End If

Hope this helps.

Bill
 
Bill

I'll change the "." to "!", but it seems to be working as "." for now.

Didn't know you could leave out the table name in the SQL field references if there was only 1 table. Thanks!

Jim DeGeorge [wavey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top