473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Setting Subform RecordSource in VBA

RLindahl
Greetings,

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
  3.  
  4.     Forms![frmQuickSearchMedia]![Media Search subform].Form.RecordSource = "Media Search"
  5.     Forms![frmQuickSearchMedia]![Media Search subform].Form.Requery
  6.  
  7.  
  8.  
  9. btnSearch_Click_Exit:
  10.     Exit Sub
  11.  
  12. btnSearch_Click_Err:
  13.     MsgBox Error$
  14.     Resume btnSearch_Click_Exit
  15.  
  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

RL
Dec 19 '12 #1

✓ answered by ADezii

You need to Requery the SubForm Control itself, and not the actual Form that is its Record Source. Try:
Expand|Select|Wrap|Line Numbers
  1. With Forms![frmQuickSearchMedia]![Media Search subform]
  2.   .Form.RecordSource = "Media Search"
  3.      .Requery
  4. End With

16 30246
ADezii
8,834 Expert 8TB
You need to Requery the SubForm Control itself, and not the actual Form that is its Record Source. Try:
Expand|Select|Wrap|Line Numbers
  1. With Forms![frmQuickSearchMedia]![Media Search subform]
  2.   .Form.RecordSource = "Media Search"
  3.      .Requery
  4. End With
Dec 19 '12 #2
NeoPa
32,556 Expert Mod 16PB
ADezii's response is (pretty close to) perfect. It handles the error message you quote as well as explaining why it should work.

Your question has two aspects though. The other is avoiding the recordset loading until actually specified by the Button click. This is easier than it might appear. Simply design (Save) the form with no RecordSource value.

I suspect though, that with more judicious use of existing form properties and calling parameters, you could do this without resorting to such a cumbersome approach. Filter properties can be passed to a form as part of its open call, for instance. I'll leave that thought with you. It might result in a separate question.
Dec 20 '12 #3
@ADezii
Thank you. This worked out. Is there a trick to knowing when it is the control and when it is the form that needs to be requeried? I've just delved into utilizing VBA and Access. Your time and assistance is most appreciated.
Dec 20 '12 #4
@NeoPa
Thank you for your reply. You are correct that I was trying to avoid the recordset loading until the button click. Neither the main form or subform have a RecordSource value. I guess I'm not exactly sure I follow what you mean by just removing the RecordSource, But it sounds as though it is what I did.

I'm a beginner with VBA and Access and it shows. I figured that this solution was a bit much. I'll have to see about reworking it. Thanks again, your time and insight are greatly appreciated.
Dec 20 '12 #5
NeoPa
32,556 Expert Mod 16PB
Thank you for your very polite replies.

I'm curious to understand why you would have a set of data already loaded if the .RecordSource property is already blank. I would expect there to be no data loaded if .RecordSource is blank.
Dec 20 '12 #6
My apologies I misspoke. The mainform's RecordSource is blank, the subform's SourceObject is blank, but the subform RecordSource is not. I had to recheck. Your previous statement about recordSource makes sense now. However, If I remove the RecordSource from the subform then nothing shows when I click the button.

I must have something backwards or I'm still missing something.

I've attached screenshots of the forms' data properties. I do not know if they will be of any use. This is all still quite new concepts to me (VBA to control forms and controls, etc)

I utilized the code from the original answer in the button.

Your time and assistance are greatly appreciated.



Attached Images
File Type: jpg ScreenHunter_01 Dec. 20 11.12.jpg (23.3 KB, 17662 views)
File Type: jpg ScreenHunter_02 Dec. 20 11.12.jpg (22.8 KB, 17613 views)
File Type: jpg ScreenHunter_03 Dec. 20 11.13.jpg (24.1 KB, 17551 views)
Dec 20 '12 #7
TheSmileyCoder
2,322 Expert Mod 2GB
What I often do if I don't want the subform to load data during open is to set the recordsource to:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM MyTableName WHERE FALSE
This will load no records at startup, and then you can simply change the recordsource when you are ready.
Dec 20 '12 #8
NeoPa
32,556 Expert Mod 16PB
I'm afraid there's nothing there we can read. The resolution is too low. I can see that one of them has Record Source set to blank, but no names and little else at all really :-( It's really better to post words where possible. Pictures are rarely much use for anything but pictures. Even when discernable they cannot be copied easily so generally little use. I appreciate the attempt mind - just making the point from experience.

Let me try to understand you better here. Are you saying that when you save the sub form without a Record Source then it doesn't work even after running your code behind the Command Button? The code includes a Requery call, so I find that strange.
Dec 20 '12 #9
NeoPa
32,556 Expert Mod 16PB
Smiley:
This will load no records at startup, and then you can simply change the recordsource when you are ready.
Are you saying that you've had similar experiences trying to work with a subform that has no Record Source? Is this behaviour version dependent possibly?
Dec 20 '12 #10
@NeoPa
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
@TheSmileyCoder
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
NeoPa
32,556 Expert Mod 16PB
RLindahl:
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
TheSmileyCoder
2,322 Expert Mod 2GB
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
TheSmileyCoder
2,322 Expert Mod 2GB
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
NeoPa
32,556 Expert Mod 16PB
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
@TheSmileyCoder
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] & "*"));
  4.  
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

0
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...
1
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...
4
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...
0
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...
6
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...
8
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...
7
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...
2
stonward
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,...
57
thelonelyghost
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...
7
reginaldmerritt
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.