Requerying Recordset Based on Combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bullfrog83
    New Member
    • Apr 2010
    • 124

    #1

    Requerying Recordset Based on Combobox

    I am trying to requery a recordset so that a form changes record based on a combobox. In the form's Open event I'm using this code:

    Code:
        Dim cn As New ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim strSQL As String
        
        cn.Open CurrentProject.Connection
        
        Set rs = New ADODB.Recordset
        
        strSQL = "SELECT * FROM StdDegReq WHERE PersonId = " & Nz(Me.txtPersonId, 0)  
      
        rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic
        
        Set Me.Recordset = rs
    
    Exit_Procedure:
        cn.Close
        Set rs = Nothing
        Exit Sub
    What I'm trying to figure out is how to requery the recordset in the AfterUpdate event of the combobox without having to repaste all my code from the OnOpen event (if possible). I tried this but it doesn't work:

    Code:
        Dim rs As ADODB.Recordset
        
        Set rs = Me.Recordset
        rs.Requery
    I get the error message: "Operation is not allowed when the object is closed". What object is closed? Any suggestions?
    Last edited by bullfrog83; Jul 8 '10, 01:47 PM. Reason: Added error message
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Did you try:
    Code:
    Me.Recordset = Me.Recordset
    Nic;o)

    Comment

    • bullfrog83
      New Member
      • Apr 2010
      • 124

      #3
      Originally posted by nico5038
      Did you try:
      Code:
      Me.Recordset = Me.Recordset
      Nic;o)
      How do you mean? Just have that code in the module?

      Also, I added the the error message that I get if I just execute the code above in my OP: "Operation is not allowed when the object is closed".
      Last edited by bullfrog83; Jul 8 '10, 01:49 PM. Reason: added error msg

      Comment

      • nico5038
        Recognized Expert Specialist
        • Nov 2006
        • 3080

        #4
        This command will requery your form after activation.

        The error messages does look like you're having some code to close the form. Check your code against the event sequence to solve this.

        When stuck attach (part of) the .mdb so I can have a look.

        Nic;o)

        Comment

        • bullfrog83
          New Member
          • Apr 2010
          • 124

          #5
          Originally posted by nico5038
          This command will requery your form after activation.

          The error messages does look like you're having some code to close the form. Check your code against the event sequence to solve this.

          When stuck attach (part of) the .mdb so I can have a look.

          Nic;o)
          If I only have Me.Recordset = Me.Recordset in the AfterUpdate event I get "Error 450: Wrong number of arguments or invalid property assignment." Perhaps I've misunderstood how you want me to include this in my code?

          Comment

          • nico5038
            Recognized Expert Specialist
            • Nov 2006
            • 3080

            #6
            Oops, typo, this needs to be:
            Code:
            Me.RecordSource = Me.RecordSource
            Sorry.

            Nic;o)

            Comment

            • bullfrog83
              New Member
              • Apr 2010
              • 124

              #7
              Originally posted by nico5038
              Oops, typo, this needs to be:
              Code:
              Me.RecordSource = Me.RecordSource
              Sorry.

              Nic;o)
              I think that does the trick as I get no error but the problem is if Me.txtPeopleId is null it errors out. So, I put Nz(Me.txtPeople Id,0) so I don't get that error. However, from checking the code in the Immediate window it appears that the Nz function is returning 0 for any id I select. So, if I select id 1489 it's executing the code as PersonId = 0 instead of 1489. If not Nz, then what can I use to handle the variable if it's null?

              Comment

              • nico5038
                Recognized Expert Specialist
                • Nov 2006
                • 3080

                #8
                First I would "move" your code from the Open event to the OnCurrent event, thus making sure it's executed every time there's a change of record.

                Next I would use this code to see or the combo is filled and issue a warning when no selection has been made:
                Code:
                IF Len(NZ(Me.txtPeopleId)) > 0 then
                   Me.RecordSource = Me.RecordSource
                else
                   Msgbox "Please select a Person"
                endif
                Nic;o)

                Comment

                • bullfrog83
                  New Member
                  • Apr 2010
                  • 124

                  #9
                  Originally posted by nico5038
                  First I would "move" your code from the Open event to the OnCurrent event, thus making sure it's executed every time there's a change of record.

                  Next I would use this code to see or the combo is filled and issue a warning when no selection has been made:
                  Code:
                  IF Len(NZ(Me.txtPeopleId)) > 0 then
                     Me.RecordSource = Me.RecordSource
                  else
                     Msgbox "Please select a Person"
                  endif
                  Nic;o)
                  If I move my code to the OnCurrent event I get an unexpected error has occurred and Access quits. This happens when the code gets to the line: Me.Recordset = rs (see OP).

                  Comment

                  • nico5038
                    Recognized Expert Specialist
                    • Nov 2006
                    • 3080

                    #10
                    When it's a bound combo and filled when the form opens, then I guess a loop will occur.
                    So we'll need to use the OnOpen of the form and the AfterUpdate event of the combo.

                    Nic;o)

                    Comment

                    • bullfrog83
                      New Member
                      • Apr 2010
                      • 124

                      #11
                      Originally posted by nico5038
                      When it's a bound combo and filled when the form opens, then I guess a loop will occur.
                      So we'll need to use the OnOpen of the form and the AfterUpdate event of the combo.

                      Nic;o)
                      Now it's working. Thanks!

                      Comment

                      Working...