Add item to listbox from a popup form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SidneySquirrel
    New Member
    • Aug 2010
    • 2

    Add item to listbox from a popup form

    I am trying to populate data into a listbox using VBA from a popup form.

    The main form holds the listbox and the button that opens the second form. I want the user to be able to enter an address into the second form. When the user clicks the close button on the second form, I want to add the data into a new row in the mainforms listbox without writing the data to a table at this stage (this is done later on the collection of other data).

    I can not get my references correct in the code of the second form to populate the data into the listbox on the first form.

    Any help offered will be appreciated.

    Thanks
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Assuming your 1st Form is named Form1, contains a List Box named List1, your 2nd Form is named Form2, the 2nd Form contains a Text Box named txtValue which contains the Value to be added to the List Box on Form1, Form1 opens Form2 and remains Open, then, in the Close() Event of Form2:
    Code:
    Private Sub Form_Close()
    Dim lst As ListBox
    
    Set lst = Forms!Form1![List1]
    
    If Not IsNull(Me![txtValue]) Then
      lst.AddItem Me![txtValue]
      lst.Requery
    End If
    End Sub

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      Note that for ADezii's code to work the RowSourceType has to be set to "Value List." Also, this method is only available in post Access2000 versions. In v2000 and earlier versions AddItem was used to add items to Menus, I believe.

      Linq ;0)>

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Excellent point by Linq. If you are working in an earlier Version (AddItem not supported), and the RowSourceType of the Value List is Value List, then:
        Code:
        Private Sub Form_Close()
        Dim lst As ListBox
          
        Set lst = Forms!Form1![List1]
          
        If Not IsNull(Me![txtValue]) Then
          lst.RowSource = lst.RowSource & ";" & Me![txtValue]
          lst.Requery
        End If

        Comment

        • SidneySquirrel
          New Member
          • Aug 2010
          • 2

          #5
          Thank you ADezii and missingling for your replies. It has worked perfectly.

          Comment

          Working...