ListBox.AddItem

  • Thread starter Thread starter Don Cossitt
  • Start date Start date
D

Don Cossitt

I have looked through the forums as I am sure this has been brought up
before but could not find anything. In VB 6.0 a ListBox has an AddItem
method. In Microsoft Froms 2.0 ListBox there is no such thing. I have
tried many tricks with .AddItem and the like yet will not allow.

I have built a function that will get filenames from a given root and
directory and want to populate my ListBox with the results. How to?

TIA
doco
 
Hi,

The listbox you speak off does have an AddItem method but it is not exposed
through the vba interface using the usual drop down listing of object
properties an methods.

Here's as an example of how to do it. I've used the double click event of
the listbox, but you can add the item whichever way suits you.

Private Sub lstYourListBox_DblClick(ByVal Cancel As Object)
Dim strAddValue As String

strAddValue = "Hello world!"
Me.lstYourListBox.AddItem strAddValue

End Sub


Jamie
 
Don Cossitt said:
I have looked through the forums as I am sure this has been brought up
before but could not find anything. In VB 6.0 a ListBox has an AddItem
method. In Microsoft Froms 2.0 ListBox there is no such thing. I have
tried many tricks with .AddItem and the like yet will not allow.

The AddItem Method is Only available on Access 2003 and above. On Access 2K,
you will have to employ the following to additems to a listbox:

Set the ListBox RowSource Type to ValueList.

Private Sub ListAddItem(LB As ListBox, strItemToAdd As String)
If LB.RowSource = "" Then
LB.RowSource = IIf(LB.RowSource = "", strItemToAdd, "")
Else
LB.RowSource = LB.RowSource & ";" & strItemToAdd
End If
End Sub
 
Private Sub Form_Load()

With Me.lstFiles
.AddItem "this file"
.AddItem "that file"
.AddItem "the other file"
End With

End Sub

produces Compile error:
Method or data member not found

doco
 
Yes, that works quite nicely - thanks

doco


MikeB said:
The AddItem Method is Only available on Access 2003 and above. On Access
2K,
you will have to employ the following to additems to a listbox:

Set the ListBox RowSource Type to ValueList.

Private Sub ListAddItem(LB As ListBox, strItemToAdd As String)
If LB.RowSource = "" Then
LB.RowSource = IIf(LB.RowSource = "", strItemToAdd, "")
Else
LB.RowSource = LB.RowSource & ";" & strItemToAdd
End If
End Sub
 
Van T. Dinh said:
BTW, the O.P. was asking about ListBox in MSForms 2.0, not Access
ListBox...

Well, that's what he *asked*, but I'm not sure that's what he meant.
After all, Mike's answer did appear to get him where he wanted to go.
 
I am sure you would check with the O.P. before you offer your solution?

It just happens that both MSForms 2.0 ListBox and Access ListBox have AddNew
....
 
Van T. Dinh said:
I am sure you would check with the O.P. before you offer your solution?

It just happens that both MSForms 2.0 ListBox and Access ListBox have AddNew

Never tried it with Forms 2.0, but the OP tried the other solution before my
post and said it didn't work. Course we aren't privvy to the code either. But
heck, If it ain't broke, don't fix it...
 
Van T. Dinh said:
I am sure you would check with the O.P. before you offer your solution?

It just happens that both MSForms 2.0 ListBox and Access ListBox have AddNew


And yes MSForms 2.0 listbox does support AddItem (just had to check). I never
thought of it because I can't think of using anything in the MSForms 2.0 that I
have used in 4 or 5 years.
 
If you got an error with that code then you must be using an Access listbox,
not the Forms 2.0. The code I posted was for the Forms 2.0 control (which
is what you asked about). It may work with a standard Access listbox in
later versions (2002 -->) I couldn't say.


Jamie
 
Back
Top