Problems accessing the List Property of a forms listbox

  • Thread starter Thread starter John T Ingato
  • Start date Start date
J

John T Ingato

I had encountered tis problem a while back and do not remember if I ever
solved it.

I have created a form with checkboxes. Once a checkbox is clicked, a click
event procedure is called that adds a string to a list box. Very simple.
If the checkBox is checked, add the string. If it is unchecked, remove the
string. i would like to write a sub that searches through all the items in
the listbox, find a match to a string and returns the index, so I can call
"ListBox.RemoveItem(index)"

Here is the code I was thinking would work. Help says that I CAN use the
"List" property of the listbox, e.g. Mystring = MyListBox.list(4), but the
compiler says "List" is NOT a valid property. Why? Anyone?
Can someone tell me the best way to do this?
My final goal is to take each item that is check and create a query string
out of it, then create a report of those items.

Private Sub The407_01CheckBox_Click()

Call BoxClicked(The407_01CheckBox, "#2 Tape")

End Sub

Private Sub The405_01CheckBox_Click()

Call BoxClicked(The405_01CheckBox, "#1 Stains")

End Sub

Private Sub BoxClicked(objCheckBox As CheckBox, ListData As String)

Dim IndexInList As Integer, objLB As ListBox

Set objLB = Me.MyList

If objCheckBox.Value = True Then
MyList.AddItem (ListData)
Else
Dim LB As ListBox
Call RemoveFromList(objLB, ListData)
End If

End Sub

Private Function RemoveFromList(ByRef LB As ListBox, TestString As String)
as integer
Dim i as single

for i = 0 to MyListBox.Listcount - 1
if LB.list(i) = TestString then
LB.RemoveItem(i)
exit for
end if
next i

End Sub
 
An Access listbox control does not have a List property, John. What's the
title of that help topic? Perhaps it is referring to some other type of
listbox control.

I believe the ItemData property is the property you are looking for.
 
If it is selected items in the list box you want to find, see VBA Help for
the ItemsSelected collection. It has a good example of how it is used.
 
Actually I just typed in the code window... mylistbox.list, ...highlighted
"List" and hit F1 which brought up the list property help. It clearly
stated that list should be a property of list box.

Thank You Thank You Thank You... ItemData was exactly what I was looking
for. It is very difficult navigating through VBA help sometime, especially
on certain quests. Oh well, thank to the new groups , I keep learning.

While I have you on the phone... what is the difference between dim x as
listbox and dim x as msforms.listbox
 
Your last question is the solution to the puzzle - an Access listbox is not
the same thing as an MSForms listbox. The help topic you saw was probably
referring to an MSForms listbox.

If you have references (Tools, References in the VBA editor) to two or more
libraries that contain objects with the same names, such as ListBox in the
Access library and ListBox in the MSForms library, then the library that
appears first in the list of references takes precedence. If you declare a
variable as ListBox, then this will be an Access ListBox if the Access
library appears before the MSForms library, or an MSForms ListBox if the
MSForms library appears before the Access library. As Access adds the VBA
and Access references automatically when you create a new MDB, and does not
allow other libraries to be moved above them, in practice an unqualified
reference to ListBox will always be an Access ListBox.

You can override this behaviour be 'disambiguating', that is to say by using
the fully qualified name of the class, including the library, when declaring
the variable ...

Dim ListBox1 As Access.ListBox
Dim ListBox2 As MSForms.ListBox

Mostly we use the Access controls in Access. I can't comment on the possible
use of the MSForms controls in Access, as I have no recent experience of
that.
 
Thanks again. I had a feeling that had something to do with it. So an
msforms.listbox refers to a control on any form and an access listbox refers
to a listbox that can be present on a datasheet and such. Is the msforms
library shared amongst all the office applications, Excel, Outlook, etc?
 
Thanks again. I had a feeling that had something to do with it. So an
msforms.listbox refers to a control on any form and an access listbox refers
to a listbox that can be present on a datasheet and such. Is the msforms
library shared amongst all the office applications, Excel, Outlook, etc?
 
The msforms library is not commonly used in Access applications. Access has
its own forms, which are different in many ways from the UserForms used by
other Office applications.
 
Back
Top