Setting Subform RecordSource in VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RLindahl
    New Member
    • Dec 2012
    • 13

    Setting Subform RecordSource in VBA

    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:

    Code:
    Private Sub btnSearch_Click()
    On Error GoTo btnSearch_Click_Err
    
        Forms![frmQuickSearchMedia]![Media Search subform].Form.RecordSource = "Media Search"
        Forms![frmQuickSearchMedia]![Media Search subform].Form.Requery
        
    
    
    btnSearch_Click_Exit:
        Exit Sub
    
    btnSearch_Click_Err:
        MsgBox Error$
        Resume btnSearch_Click_Exit
    
    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
    Last edited by Rabbit; Dec 19 '12, 09:29 PM. Reason: Please use code tags when posting code.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    You need to Requery the SubForm Control itself, and not the actual Form that is its Record Source. Try:
    Code:
    With Forms![frmQuickSearchMedia]![Media Search subform]
      .Form.RecordSource = "Media Search"
         .Requery
    End With

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      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.

      Comment

      • RLindahl
        New Member
        • Dec 2012
        • 13

        #4
        Originally posted by ADezii
        You need to Requery the SubForm Control itself, and not the actual Form that is its Record Source. Try:
        Code:
        With Forms![frmQuickSearchMedia]![Media Search subform]
          .Form.RecordSource = "Media Search"
             .Requery
        End With
        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.

        Comment

        • RLindahl
          New Member
          • Dec 2012
          • 13

          #5
          Originally posted by NeoPa
          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.
          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.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            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.

            Comment

            • RLindahl
              New Member
              • Dec 2012
              • 13

              #7
              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.
              [imgnothumb]http://bytes.com/attachments/attachment/6785d1356024263/screenhunter_01-dec.-20-11.12.jpg[/imgnothumb]
              [imgnothumb]http://bytes.com/attachments/attachment/6786d1356024263/screenhunter_02-dec.-20-11.12.jpg[/imgnothumb]
              [imgnothumb]http://bytes.com/attachments/attachment/6787d1356024263/screenhunter_03-dec.-20-11.13.jpg[/imgnothumb]
              Attached Files
              Last edited by NeoPa; Dec 20 '12, 10:55 PM. Reason: Made pics visible.

              Comment

              • TheSmileyCoder
                Recognized Expert Moderator Top Contributor
                • Dec 2009
                • 2322

                #8
                What I often do if I don't want the subform to load data during open is to set the recordsource to:
                Code:
                SELECT * FROM MyTableName WHERE FALSE
                This will load no records at startup, and then you can simply change the recordsource when you are ready.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  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.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    Originally posted by Smiley
                    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?

                    Comment

                    • RLindahl
                      New Member
                      • Dec 2012
                      • 13

                      #11
                      Originally posted by NeoPa
                      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.
                      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.

                      Comment

                      • RLindahl
                        New Member
                        • Dec 2012
                        • 13

                        #12
                        Originally posted by TheSmileyCoder
                        What I often do if I don't want the subform to load data during open is to set the recordsource to:
                        Code:
                        SELECT * FROM MyTableName WHERE FALSE
                        This will load no records at startup, and then you can simply change the recordsource when you are ready.
                        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?

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32633

                          #13
                          Originally posted by RLindahl
                          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.

                          Comment

                          • TheSmileyCoder
                            Recognized Expert Moderator Top Contributor
                            • Dec 2009
                            • 2322

                            #14
                            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.

                            Comment

                            • TheSmileyCoder
                              Recognized Expert Moderator Top Contributor
                              • Dec 2009
                              • 2322

                              #15
                              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?

                              Comment

                              Working...