Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to clear a listbox after you select new record

Status
Not open for further replies.

brwnsuga21

Technical User
Aug 23, 2007
15
US
When I select a new record the listbox in my database does not clear. I have tried putting "null" in the default value property field as well as (""). I've even tried the below code changing the command button name to the one in my database and that didnt work either.

Private Sub YourCommandButtonName_Click()

Dim varItm As Variant

With YourListBoxName

For Each varItm In .ItemsSelected
.Selected(varItm) = False
Next varItm

End With
End Sub

Since I am new to this I cant seem to make anything work. Please help. I am running out of solutions.
 
What happens if you put Me.NameofListbox.RowSource = Me.NameofListbox.RowSource
 
I assume you have an unbound listbox on a bound form. If so I would use the On current event of the form. But that code works for me. Do not really need the with, does not shorten the code.

Private Sub Form_Current()
Dim Lbx As ListBox
Dim varItem As Variant
Set Lbx = Me.yourListBoxName
For Each varItem In Lbx.ItemsSelected
Lbx.selected(varItem) = False
Next varItem
End Sub

Hate to ask, but did you put your controls name in "YourListBoxName"?
 
I did not put the control name in the "YourListBoxName". I also entered the code provided and the list box did not clear. What should I try next? I should mention that the list box is populated when i choose a selection from a drop down menu. Does this chnage things?
 
Assuming a bound form, I would suggest the on current event of the form. ZOR solution is much simpler. Both work.
 
Where should i enter this code "Me.NameofListbox.RowSource = Me.NameofListbox.RowSource"? I am new to this and am unsure of where things should go if it is not clearly stated.
 
Use ZOR's solution, on current event of the form, and you do need to put the name of your control where it says "nameOfListBox" (i.e. Me.list1)
 
Perfect! I put the "Me.NameofListbox.RowSource = Me.NameofListbox.RowSource" in the onCurrent event of the form and it worked. You guys are awesome!!!
 
In form design view click on the form properties and go to the event tab for the form. Where it says "oncurrent" click on the ellipses. Select code. It should create a procedure to add the code.
 
Sorry I did not get back afterwards of suggesting something. You can thank PHV for that one, he showed me a while back. Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top