Adding Items To a ListBox (1 Viewer)

cmcquain

Registered User.
Local time
Yesterday, 20:59
Joined
Apr 12, 2012
Messages
17
Using VBA, how do I add items to a listbox named lstZipCollection from a text box called txtZipCode2 using a button named btnEnter? Help Please.:eek:
 

bob fitz

AWF VIP
Local time
Today, 00:59
Joined
May 23, 2011
Messages
4,717
Using VBA, how do I add items to a listbox named lstZipCollection from a text box called txtZipCode2 using a button named btnEnter? Help Please.:eek:
I think that depends on how the list box is populated. If is based on a table/query then the new entry would need to be added to one of the tables used. If it is based on a list of values, then you would need a little code to get the current list, add the new entry and repopulate the list.
 

cmcquain

Registered User.
Local time
Yesterday, 20:59
Joined
Apr 12, 2012
Messages
17
As my question stated before. I would like VBA code please. It is based upon what a user types into the list box.
 

bob fitz

AWF VIP
Local time
Today, 00:59
Joined
May 23, 2011
Messages
4,717
Please post the "Row Source" property and the "Row Source Type" property of the list box.
 

bob fitz

AWF VIP
Local time
Today, 00:59
Joined
May 23, 2011
Messages
4,717
I'm not sure what you are asking.
As stated, I am asking you to post the "Row Source" property and the "Row Source Type" property of the list box.

To find these:
Open the form in design view.
Double click the listbox control. This will open the property sheet.
Click the Data Tab.

Copy/Paste the settings of the two required properties in your post.
 

cmcquain

Registered User.
Local time
Yesterday, 20:59
Joined
Apr 12, 2012
Messages
17
row source is blank and row source type is value list
 

bob fitz

AWF VIP
Local time
Today, 00:59
Joined
May 23, 2011
Messages
4,717
Use this code in the On Click event of the button:
Code:
  If Nz(Me.txtZipCode2, "") <> "" Then
    If MsgBox("Add '" & Me.txtZipCode2 & "' to list?", vbYesNo, "Confirmation Required") = vbYes Then
      Me.lstZipCollection.RowSource = Me.lstZipCollection.RowSource & Me.txtZipCode2 & ";"
      Me.lstZipCollection.Requery
    End If
  End If

This needs to be copied to the form's code module. Double click the button while the form is in design mode and find the Button's On Click event. Select [Event Procedure] then click the elipse ... to the right. This will open the code module.
The code goes between the lines
Private Sub btnEnter_Click()
Put code here.
End Sub
 

cmcquain

Registered User.
Local time
Yesterday, 20:59
Joined
Apr 12, 2012
Messages
17
Thank you very much. any suggestions on how I would go about clearing the list with another button?
 

bob fitz

AWF VIP
Local time
Today, 00:59
Joined
May 23, 2011
Messages
4,717
I wondered if you would ask that :)

Use this code:
Code:
    If MsgBox("Clear List?", vbYesNo, "Confirmation Required") = vbYes Then
      Me.lstZipCollection.RowSource = ""
      Me.lstZipCollection.Requery
    End If
in the On Click event of a new button.
 

cmcquain

Registered User.
Local time
Yesterday, 20:59
Joined
Apr 12, 2012
Messages
17
Thanks. I ended up just using lstZipCollection.RowSource = ""

The .Requery looks like it is not needed. Is it just good practice to put it in, or is it needed?
 

bob fitz

AWF VIP
Local time
Today, 00:59
Joined
May 23, 2011
Messages
4,717
If it works without it, that's fine. I only put it in because it was needed in the first code and I just assumed it would be needed in the second.
 

Users who are viewing this thread

Top Bottom