PDA

View Full Version : Copying a Textbox and Combobox at runtime



ukdane
11-25-2009, 06:54 AM
Is it possible to dynamically copy a textbox or a combobox at runtime?

I have a series of textboxes on a form for user input, and thats fine if they only need to put in data for one item.
However at the time of programming, I don't know how many items the user will need to include data for.

What I was thinking was if when the code starts, the user inputs the number of items.

Can VBA copy the textboxes (named unit01, date01, time01) and the combobox (named combo01) for each additional item, and rename the textboxes and combobox as unitxx, datexx, timexx comboxx where xx refers to each item.
So if there were 4 items, I would end up with 16 boxes in all (unit01, unit02, unit03, unit04, date01, date02, date03, date04, time01, time02, time03, time04, combo01, combo02, combo03, combo04)

Is this possible, or is it a can of worms?
Also when the data has been collated and used, and the form closed down, it would need to revert to just one row of textboxes and combobox (still called unit01 etc etc)

Bob Phillips
11-25-2009, 08:12 AM
This has been stated many times, and it is just as true today as it was then.

It is far better to create the most controls that you might need at design time, and hide the ones not in use until they are needed.

ukdane
11-25-2009, 09:30 AM
Hi Xld,
I appreciate that, but the problem is I don't know how many controls I'm going to need, as it will be used much like a datasheet in access. Is it possible to recreate that on an excel form?

Bob Phillips
11-25-2009, 09:51 AM
It is possible, but not easy. And you have to have sopme code to process them, validate the entry and so on, which is even harder.

Surely, you have a limit. Otherwise, how big will the form get?

ukdane
11-25-2009, 11:14 AM
Yeah, there is probably a maximum, of about 30 items. So I think I'll head your advice, and create 30 rows of each item, and name them as indicated above. I can then still run loops to populate the date, and show and hide each of the rows.

Thanks for your advice.