473,836 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting Subform RecordSource in VBA

13 New Member

I'm trying to utilize VBA to set a subform's recordsource on a command button click. My form is used to search by a query and display the results in the subform. If I set the subform source in the property sheet it works properly but loads the full query on opening the form. I want the subform to be blank until I click the search button. Here is my button's code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnSearch_Click()
  2. On Error GoTo btnSearch_Click_Err
  4.     Forms![frmQuickSearchMedia]![Media Search subform].Form.RecordSource = "Media Search"
  5.     Forms![frmQuickSearchMedia]![Media Search subform].Form.Requery
  9. btnSearch_Click_Exit:
  10.     Exit Sub
  12. btnSearch_Click_Err:
  13.     MsgBox Error$
  14.     Resume btnSearch_Click_Exit
  16. End Sub
When I click the button I get an error stating:
The expression you entered refers to an object that is closed or doesn't exist.

Is this an error in my button code or a consequence of my form to query interaction?

Thank you

Dec 19 '12
16 30391
13 New Member
That is Precisely what happens, If I take the RecordSource off the subform then nothing shows up. As for the screenshots, well.. it was worth a shot. I can explain them though.

The first was just a pic of the mainform showing no RecordSource. The Second was a picture of the SubForm Control(unbound ) showing no SourceObject. The third was of the SubForm showing the RecordSource. I have the form working so I just left the RecordSource in. Why I get nothing when it's removed, ..The world may never know.. I know that I have no clue.

I do appreciate all of the assistance and insight that you and everyone else have given.
Dec 21 '12 #11
13 New Member
Thank you for your insights. I will have to give this a try. Removing the RecordSource didn't work properly so this may be the ticket. Have you ever seen the incident of removing the RecordSource but setting it in code only to have it not show?
Dec 21 '12 #12
32,584 Recognized Expert Moderator MVP
Have you ever seen the incident of removing the RecordSource but setting it in code only to have it not show?
I'd ask the same question. From the fact that Smiley has a solution that he uses, one could guess that he must have had a similar problem.
Dec 21 '12 #13
2,322 Recognized Expert Moderator Top Contributor
Lets try to make sure our expectations are correct.

If I have ANY BOUND form, subform or parent form, and remove the recordsource, after its opened, I would expect the following to occur:
A form with labels showing but all textboxes saying name?
If allow additions was set to false, you might instead see an empty form (no labels and no textboxes)

If you set a faulty recordsource (i.e. one which returns no records) and allow additions is set to false, you might also see this type of behavior (Empty form, no labels, no controls)

Now I have not worked really with removing the recordsource, more with replacing the recordsource. Moreover my replacements have been dealing with items from the same table, for example:
Original recordsource "SELECT * FROM tbl_Comments WHERE ID_Report=212"
New Recordsource "SELECT * FROM tbl_Comments WHERE ID_Report=917"

You can see this kind of replacement if you watch the last 3 minutes of my treeview tutorial part 2. This link should take you roughly to the correct spot in the video though you may need to watch the entire video to make sense of it.
Dec 21 '12 #14
2,322 Recognized Expert Moderator Top Contributor
Could you post a before SQL and an after SQL so we might get an idea of the type of recordsource replacement you are performing?
Dec 21 '12 #15
32,584 Recognized Expert Moderator MVP
The question is really do you have experience of a bound form with no RecordSource set when it's opened failing to show anything when a RecordSource value is added later? This is behaviour I wouldn't expect to see, but the OP has reported as happening to them.

Your example of :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tbl_Comments WHERE ID_Report=212
where you change only the WHERE clause, is a situation where simply changing the filter used to open the form would surely be a more straightforward approach. If it's a subform, then the .Filter property could be set/changed instead.
Dec 21 '12 #16
13 New Member
The SQL originally in my SubForm:
Expand|Select|Wrap|Line Numbers
  1. SELECT [Media Search].D_Format, [Media Search].D_Title, [Media Search].D_Content, [Media Search].D_DateBorrowed, [Media Search].D_Returned, [Media Search].ClientLast, [Media Search].ClientFirst FROM [Media Search]; 
That is based off of a Query. The query SQL was this:

Expand|Select|Wrap|Line Numbers
  1. SELECT [Disc Specifics].D_Format, [Disc Specifics].D_Title, [Disc Specifics].D_Content, [Disc Loan Records].D_DateBorrowed, [Disc Loan Records].D_Returned, [Client Listing].ClientLast, [Client Listing].ClientFirst
  2. FROM [Disc Specifics] INNER JOIN ([Client Listing] INNER JOIN [Disc Loan Records] ON [Client Listing].ClientID = [Disc Loan Records].ClientID) ON [Disc Specifics].D_ID = [Disc Loan Records].D_ID
  3. WHERE ((([Disc Specifics].D_Format) Like "*" & [Forms]![frmQuickSearchMedia]![txtFormat] & "*") AND (([Disc Specifics].D_Title) Like "*" & [Forms]![frmQuickSearchMedia]![txtTitle] & "*") AND (([Disc Specifics].D_Content) Like "*" & [Forms]![frmQuickSearchMedia]![txtContent] & "*"));
The SQL in my Button Click Code is:

Expand|Select|Wrap|Line Numbers
  1. "SELECT [Disc Specifics].D_Format, [Disc Specifics].D_Title, [Disc Specifics].D_Content, [Disc Loan Records].D_DateBorrowed, [Disc Loan Records].D_Returned, [Client Listing].ClientLast, [Client Listing].ClientFirst FROM [Disc Specifics] INNER JOIN ([Client Listing] INNER JOIN [Disc Loan Records] ON [Client Listing].ClientID = [Disc Loan Records].ClientID) ON [Disc Specifics].D_ID = [Disc Loan Records].D_ID WHERE ((([Disc Specifics].D_Format) Like " * " & [Forms]![frmQuickSearchMedia]![txtFormat] & " * ") AND (([Disc Specifics].D_Title) Like " * " & [Forms]![frmQuickSearchMedia]![txtTitle] & " * ") AND (([Disc Specifics].D_Content) Like " * " & [Forms]![frmQuickSearchMedia]![txtContent] & " * "));"
The original query SQL and the button click SQL are the exact same statements.
Dec 22 '12 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Lauren Quantrell | last post by:
I'm using SQL Server backend on an Access 2K front end. I populate a subform: Forms!myForm.myChild.Form.RecordSource = "myStoredProcedureName" On that form is a control where the controlsource is "= Count()" that shows the user the number of records. What I'm doing now is populating the subform, using the RecordsetClone.RecordCount of the subform to determine if any records were returned, and if not, opening a message box telling the...
by: John M | last post by:
Hi, I want to set the recordsource of a report on the basis of the selection from a combo box. If the report is not yet open, I am told it is not open or not found. If it is open I'm told that I can't alter the recordsource it the report is 'already printing'!! I'm lost ... but then I'm not a programmer ..... Thanks in advance
by: Peter Bailey | last post by:
I have a subform that I dont want to have a recordsource initially as it is locking the table that is created dynamically. I want to connect to the table after the data has been written by another routine. I had hoped I could do mytable.recordsource = myquery
by: robert.waters | last post by:
Hello, Say that there is a database for client information; there is a main form that displays the client's name, and this form has one or more subforms that display different types of information related to that client. The main Form has RecordSource="SELECT clientid, clientname FROM client_table;", and each subform has both Link Child and Master Fields="clientid".
by: Widge | last post by:
I'm having a nightmare. I've used this technique before and can't understand why I'm getting an error using it now. Basically I have a form, on that is a sub form datasheet. What I am eventually aiming to do is have a button set the Record Source of the subform. Now the code is as follows: Private Sub Command6_Click() SetFilter
by: fuze | last post by:
I currently have a form with a couple text boxes, a button, and an unbound subform. Pressing the button will assemble a SQL string using whatever is in the textboxes and assign the SQL string to the recordsource of the subform. Unfortunately, doing so results in the run-time error 2467 - "the expression you entered refers to an object that is closed or does not exist." below is a simple version of my code Private Sub btn1_Click() ...
by: tnjarrett | last post by:
Hello, I have a MS Access continuous subform that was using a query as the recordsource. I changed it to use a recordset instead because when the query was used, the changes to the subform values directly changed the tables under the query. I wanted the changes to be 'in-memory' so that I could check them before applying them to the tables. When I changed the subform to use the recordset, it is now 'read-only' and I can't change anything...
by: stonward | last post by:
I'm trying to change the recordsource of a subform using a simple button click: by default, the subform's source is one query, when a button is clicked it changes to another query (the same query, but this time also showing stock items with less than 1 in stock...). Having tried all manner of methods, then having read previous posts, i see the problem may be related to link criteria? Trouble is, i have no link criteria...my subform is not...
by: thelonelyghost | last post by:
Just searched google and this website but I couldn't come up with an answer to this. Basic Information Software: Microsoft Access 2000 OS: Windows XP Professional SP3 Problem: Error 3008 (locked table, ) when trying to modify recordsource of subform. Private Sub btnSearch_Click() 'Changes recordsource of subform. subform name: frmSubform
by: reginaldmerritt | last post by:
Has anyone had this problem before, my search on-line would suggest it not common but I'm sure you fine folk will be able to help. This is the code I have used to change the recordsource on a subform. I have similar code to order the data using different queries. Private Sub OrderByStartDate_Click() Forms.FRMClaims.ClaimsDisplaySubForm.Form.RecordSource = "QYClaimsByStartDate" End Sub
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.