Error when trying to remove all items from Combobox

P

Piper

Hi - I'm trying to remove all the items in a combobox in order to re-populate
it with different items. I thought this would be fairly simple
straightforward code, and I feel silly because I can't figure it out. Iv'e
looked all over the web and on these postings to see if there was a way to
remove all the items at once but I haven't found anything.

So I have been trying to do it using a loop

For rw = 0 To bx.ListCount - 1
bx.RemoveItem rw
Next rw

Everytime the code gets to the last item in the Combobox it gives me an
error and says that the index dosen't exist. Say for instance I have 4 items
in the box it removes indexes 0-2 just fine but when it tries to remove index
3 it says that there is no index 3. so I tried changing the code to
bx.ListCount - 2. And that works fine except the item at index 3 (that it
says dosen't exist) is the 1st item in my new Combobox!

I'm throughly fustrated. If you have any suggestions I'd gladly take them.

Thanks
 
K

Klatuu

It is as simple as declaring a new rowsource property.
Me.MyListBox.RowSource = strNewRowsource
 
P

Piper

Sorry I wasn't being very clear - I use ADODB Recordsets to populate the
Comboboxes. I use bx.additem rcdSet(0) & ";" & rcdSet(1) & " " & rcdSet(2)
in a For loop to fill the box. I tried using Set bx.Recordset = rcdSet but I
kept getting errors so I defaulted to the previous method.

To clear it I tried using Set bx.Recordset = Nothing but then I got an error
because my box Rowsource type was value list and it needed to be Table/Query
to use that. That's why I came up with the previously posted code.

But now that I think of it I wounder if I could just reset my Rowsource in
VB to Table/Query and then use the Set bx.Recordset = Nothing and then set it
back to Value List to do the rest.

Hmmmmm - I'll try that.

Thanks for the post - I should have been more specific earlier.
 
K

Klatuu

Have you tried:
For rw = bx.ListCount - 1 to 0 Step -1
Me.bx.RemoveItem rw
Next rw

If not that, and it is a value list, what about
Me.bx.Rowsource = ""

As to using the Set, that is incorrect. The Rowsource is a property, not an
object, so you would use
Me.bx.RowSource = Rcdset

However, not know what Rcdset is, I am not sure that will work.
Describe rcdset for me, please.
 
P

Piper

I've never seen the first method you posted but it looks promising - is it
kind of the same as a for loop just with a slightly different context? I'm
trying to teach myself VB, but I know other programming languages so I've
been cheating by trying to use their syntax when writing code.

The second method you posted worked!!!! I didn't know if I could use the
bx.RowSourse = ""
if I hadn't used that to initially fill my ComboBox.

The rcdSet in my code is what I use as the name for my ADOB.Recordset
object. Can I still use the
Me.bx.RowSource = rcdSet
if rcdSet is what it is?

I looked in the VB help files of Access and it said that Comboboxes had a
Recordset property and it gave the syntax of
Set bx.Recordset = rcdSet
to use a Recordset object to fill the Combo or to use
Set bx.Recordset = Nothing
to clear the Combo. But like I said that gave me nothing but errors
(headaches) so I used the
bx.addItem
way to fill my Combo.

I'm curious to know if the
Me.bx.Rowsource = rcdSet
will work with a Recordset object. I also use the following to set the
properties of my Combo

bx.ColumnCount = 2
bx.ColumnWidths = "0 in; 1.75 in"

Will the above still work as long as I set the .ColumnCount = to how many
columns are in my Recordset and just set the .ColumnWidths = "0 in" for the
one's I don't want to show?

Thanks for all the help I appreciate it.
 

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