Dim item As ListItem

G

Gustaf

Hi,

I get an error on line 2 in this code:

Dim item As ListItem
item.Text = product.Name
ListBox1.AddItem item
products.Add product, product.Name

The error is

Object variable of With block variable not set

For some reason, the ListItem object won't let itself be instantiated:

Dim item As ListItem
Set tem = New ListItem ' Compile error

My idea is to first instantiate a ListItem, set some properties to it and THEN add it to the ListBox. Why doesn't that work?

Gustaf
 
J

JP

What object library is ListItem from? Do you have a reference set to
its object library?

--JP
 
P

Patrick Molloy

afaik there is no listobject item. you can put text into a list , but not an
object unless its a string

Dim oitem As Object
Set oitem = New Class1
oitem.val = "A"
ListBox1.AddItem oitem.val

here class1 is an object with one property, val defined as string.
your method would work with a collection - such as the scripting dictionary
 
G

Gustaf

Hi,

Right, I had mistakenly added a reference to mscomctl.ocx, where ListItem is. Unfortunately, I can't add any dependencies to this project, since it will cause problems for the users. I assume there is no way to tie an object to a listbox item without using controls not available by default in VBA, so I'll have to find another way.

Gustaf

--
 
P

Patrick Molloy

so instead of

item.Text = product.Name
ListBox1.AddItem item

what is wrong with

ListBox1.AddItem product.name
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top