PDA

View Full Version : Solved: Update dynamically created controls?



BigJC
10-03-2011, 05:46 AM
Hi guys and girls,

once again i am here for your infinite wisdom.

A few of you have helped me get past a major problem i had now i am onto something i hope is very easy.

I have a program that creates listboxes at runtime that list data stored in each row of a worksheet.

How can i reference these listboxes in VBA so i can add items to them after they have been made outside of the procedure they are created in.

i.e. i know the names will carry the same number as the row their data is stored in.

So row 5 will add data to ListBox5, row 7 ListBox7 etc

How can i programmatically address these to update them?

I will be updating data in row 5 from anohter control but also want the data to add into the listbox as i go.

i can't address it like ListBox & xRow .additem
and i dont think i can use a variable can i like lbx = "listbox" & xRow
lbx.additem xxx

No i cant bind the listbox to the row as the data is displayed oddly.

Also its a 2 column listbox if that matters.

Thanks in advance.

Bob Phillips
10-03-2011, 06:10 AM
Assign them all to object variables when you create them, and reference them via the variable.

BigJC
10-03-2011, 02:13 PM
Assign them all to object variables when you create them, and reference them via the variable.

I was thinking something like that but then, i am creating x amount.

so i would need x amount of object variables.

How can i create the control and assign it to the next available variable in a sequence.

i.e. listbox5 would be assigned to objLBX5 etc..

I dont know how many listboxes will be created so i can't pre-empt it and decalre like 100 variables....

Bob Phillips
10-03-2011, 02:43 PM
Yes, but you could array them and if you have a 2D array and store the row number that is your id.

BigJC
10-04-2011, 12:34 AM
Yes, but you could array them and if you have a 2D array and store the row number that is your id.

Will the controls being in an array affect the class i created to handle their events?

Aflatoon
10-04-2011, 12:44 AM
If the controls are on a userform, you can refer to them (in code in the form) using:
Me.Controls("objLBX5")
so you can build up the name string as required.

BigJC
10-04-2011, 01:52 AM
If the controls are on a userform, you can refer to them (in code in the form) using:
Me.Controls("objLBX5") so you can build up the name string as required.


WORKED LIKE A TREAT!!

Exactly what i was after. Thank you so so much.