[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

How open form using query rather than table

I have a form which has it's record source as a table.

But I want to be able to open the form using a query sometimes instead.  Can this be done?

I'm trying to do it with:

DoCmd.OpenForm stDocName, acNormal, , , acFormEdit, acWindowNormal

But don't know how to make it use the query.

Or do I need to use open arguments?  There is two criteria:

ID = [Forms]![frmSelectNameAndDate]![cboName]
and
DateWorked = [Forms]![frmSelectNameAndDate]![txtDate]

--Steve
0
SteveL13
Asked:
SteveL13
  • 3
  • 3
  • 2
1 Solution
 
COACHMAN99Commented:
set the recordsource to the query instead of the table. add params to the query if necessary
0
 
Dale FyeCommented:
Assuming that the query has the same fields as the table, you can use the Open event to change the forms RecordSource property:

me.RecordSource = "qryYourQueryName"
0
 
SteveL13Author Commented:
But sometimes I want the record source to be the table and other times the query.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
COACHMAN99Commented:
populate a global variable and set the form recordsource on load event.
e.g.
on load
 if var then me.recordsource = query else me.recordsource = table.
0
 
Dale FyeCommented:
Then consider using the OpenArgs argument of the OpenForm method to pass the form what you want to use as the recordsource if not the table.  Something like:

DoCmd.OpenForm stDocName, acNormal, , , acFormEdit, acWindowNormal, "qryYourQuery"

Then use the Open event of the form to change the recordsouce appropriately:

Private sub Form_Open

     If IsNull(me.OpenArgs) = False then me.RecordSource = me.OpenArgs

End Sub
0
 
SteveL13Author Commented:
So this goes in the onopen event of the form:

If IsNull(me.OpenArgs) = False then me.RecordSource = me.OpenArgs

But where does this go?

DoCmd.OpenForm stDocName, acNormal, , , acFormEdit, acWindowNormal, "qryYourQuery"
0
 
Dale FyeCommented:
"But where does this go?"

That is how you open the form, usually from the click event of a command button on another form, or something like that.
0
 
COACHMAN99Commented:
in calling form
populate a global variable
DoCmd.OpenForm stDocName, acNormal, , , acFormEdit, acWindowNormal


in loading form
e.g.
on load
 if var then me.recordsource = query else me.recordsource = table.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now