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
SteveL13Asked:
Who is Participating?
 
COACHMAN99Connect With a Mentor Commented:
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
 
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
SteveL13Author Commented:
But sometimes I want the record source to be the table and other times the query.
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.