Listbox doesn't requery after adding a new record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TravelingCat
    New Member
    • Feb 2010
    • 82

    #1

    Listbox doesn't requery after adding a new record

    Hi,
    I have a mainform, and a subform in it. The main form contains current event details. The subform has ListBox control which contains assignments of that event. To add a new assignment, you click on button and another form opens. The thing is, if the listbox already contains some assignments, the new one are added and shown without a problem. But if the listbox is still empty and i'm adding a first, it doesn't requery, only if i exit the form and reload it. I would really appreciate if someone could point me to the solution.
    Here's the code behind the "add new assignment" button:
    Code:
    Private Sub btnNewAssignment_Click()
    Text = Forms(Parent.Name).Controls("eventNum")
    strRow = ListAssignments.ListIndex + 1
    strAssignNum = Nz(ListAssignments.Column(7))
        DoCmd.OpenForm "frmNewAssignment", , , , acFormAdd, acDialog, 1 & ";" & Text
        ListAssignments.Requery
    End Sub
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    I don't see how line 6 .Requery does anything for you. The form frmNewAssignmen t has just been opened but there has not been time for that to be filled out yet when line 6 is executed.

    I would add some code to the frmNewAssignmen t form so that it requeries the listbox on the original form every time it adds a new record.

    Jim

    Comment

    • TravelingCat
      New Member
      • Feb 2010
      • 82

      #3
      Jim, line 6 executes only after the frmNewAssignmen t has finished all his code, meaning upon return from frmNewAssignmen t to frmAssignments, and the record is supposed to already exist, so i don't really understand why requery doesn't help.

      Comment

      • jimatqsi
        Moderator Top Contributor
        • Oct 2006
        • 1293

        #4
        No, I don't think that's how it works. There is no "return from frmNewAssignmen t"; the form is opened and might stay open forever and a day. If he wants his code to wait for that form to do something he needs to add a loop with DoEvents and keep looping until some signal is received.

        Jim

        Comment

        • TravelingCat
          New Member
          • Feb 2010
          • 82

          #5
          Jim, thanks for your help, but i solved this one

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            Jim is right. The code for Form_Load & Form_Open would run if there were any, before returning to execute line #6. However, any code triggered by an operator interacting with the new form would not cause the succeeding code here to wait, nor indeed would it wait for the form to close. Line #6 is effectively run extremely soon after the opening of the form specifically.

            Generally, the Requery call is done from code in an event procedure of the newly opened form after the process of adding the new record has been completed.

            You say you have a solution. It might be interesting to see it (not simply for our curiosity, but also for anyone searching around for a solution to a similar problem).

            Comment

            • TravelingCat
              New Member
              • Feb 2010
              • 82

              #7
              I figured the solution would be interesting to see, but it has to do with my program specifically, so it wont help anyone i think. Anyway, the listbox is inside frmAssignments, which is the subform of frmEvents. In order for it to display the right assignments, it should get current event number. The problem was with the event num variable, it wasn't correct, so it always requeried some other events and not the current. I simply fixed the listbox's row source.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                Never mind then :)

                Hopefully there is still some interesting info here for any searchers that happen upon this thread.

                Comment

                Working...