I have several years of experience working with VBA in MS Office applications (for automation and ETL processes), but until recently they did not have to deal with forms in MS Access. I am laying the design for some simple forms of data mining for the database that I developed, and I hung up on what seems like a simple task.
Purpose: I need a datasheet sub format to display records returned from a dynamically generated SQL statement from controls in the main form.
In my main form, I have a button, when the user clicks on it, the button will compile the information specified by the user in other user controls into an SQL query, and then run this query so that the subformation displays the result of the record.
No matter what I do, I cannot get this to work. I keep getting (most of the time anyway) the Microsoft startup time visual error "2467: The expression you entered refers to an object that is closed or does not exist." This is the error I get with the code below. I canβt understand if I need to somehow initiate the subform as soon as any code is run. I tried some other code options that also didnβt work with other code forums, but I seem to have found several message streams, including some that suggest that the code below should work.
The attached image shows what the basic basic form looks like. I designated a button that the user would click (btnDisplaySWData) to compile SQL, which gets the creation from controls that have not yet been enabled, but this is not a problem. I just hard code the SQL statement, as shown in the code snippet, trying to understand this problem. As already mentioned, I want the records to be displayed in a subformat called dataDisplaySubform. "JUNK" is a table in the Access database in which I can legitimately query the SQL code below, which I use only for testing, until I find out. All code in the given data form (with the name frmDataExtract) consists of what is in the code window below. 
Option Compare Database Option Explicit Public Sub btnDisplaySWData_Click() Dim pSQL As String pSQL = "SELECT JUNK.agency_ID, JUNK.agency_desc FROM JUNK" Me.dataDisplaySubform.Form.RecordSource = pSQL End Sub
The form is called dataDisplaySubform, as shown in the following screenshot of the properties with the selected subform.

Here's what the general layout of the form looks like

I looked at several sites on the forum, and also tried all the options for Stack Overflow search terms to find potential solutions to my problem, but no one worked even when the original stream was marked as resolved by the person who posted it. I spent too much time, about 2 working days, trying to understand what I was doing wrong, and still could not.
I appreciate anyone who can help me in the right direction, it drives me crazy.
thanks, --TB
SOLUTION SOLUTIONS TURKISHGOL
Well, I think I figured it out myself, although HansUp helped me go down the path with a mention of the Source object of a subordinate form that has nothing attached to it. In my case, assigning a Source object in the form was not the right solution that was offered by HansUp. Instead, the saved request seems to make it do what I want.
Not sure if there is a better way to do this, but it looks like you need to set up a dummy, almost a placeholder request, so that you can set the original subform object in VBA for it. The placeholder request is as follows:
SELECT * FROM JUNK WHERE JUNK.agency_ID ="_";
The above Access query is saved as the name "TESTQUERY". It does not display anything, but satisfies the need to assign a Source Object, essentially creating an instance of the subform when viewing the main form as a form. Thus, with the saved placeholder query, you can reassign the RecordSource for any SQL string using the user interface controls in the main form, for example:
Public Sub btnDisplaySWData_Click() Dim pSQL As String pSQL = "SELECT JUNK.agency_ID, JUNK.agency_desc FROM JUNK" Me.dataDisplaySubform.SourceObject = "Query.TESTQUERY" Me.dataDisplaySubform.Form.RecordSource = pSQL Me.dataDisplaySubform.Requery End Sub
which, when the form is in the process of production, the hard-coded SQL statement shown, stored in the pSQL string variable, will be combined through user input of controls in the main form.
So now, when btnDisplaySWData is clicked, it does what I am trying to do and displays the records. 