Solved

How open form using query rather than table

Posted on 2014-01-21
8
331 Views
Last Modified: 2014-01-22
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
Comment
Question by:SteveL13
  • 3
  • 3
  • 2
8 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
Comment Utility
set the recordsource to the query instead of the table. add params to the query if necessary
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
 

Author Comment

by:SteveL13
Comment Utility
But sometimes I want the record source to be the table and other times the query.
0
 
LVL 7

Expert Comment

by:COACHMAN99
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
 

Author Comment

by:SteveL13
Comment Utility
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
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
"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
 
LVL 7

Accepted Solution

by:
COACHMAN99 earned 500 total points
Comment Utility
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now