Friday, November 19, 2010

How to create a number of textboxes in the form during runtime for VB in excel?

Forget it

VBA of Excel (up to Excel2007) does not have the ';Control Array'; feature as in regular VB

simply because the ';Index'; property is not there



You will need to do alternatives



Trust me, VBAXLMan is back and knows what he is talking aboutHow to create a number of textboxes in the form during runtime for VB in excel?
Sorry I missed the Excel part...

Report Abuse

How to create a number of textboxes in the form during runtime for VB in excel?
It really depends on what you want to achieve by doing this. However you will need to have them created on a particular event such as the user clicking a button or somthing or you could write a VBA sub / function that runs a timer in the background with the text boxes being created on timer event. Without knowing further details I would suggest that you create the text boxes on the form in design mode and then change their 'visible' property to false so that they are hidden and the when the user triggers the event / action then change the text box visible property to true.
Use an array.



1. On your form, create a textbox (Text1).

2. Change Text1.Index to ';0'; (This must be done at design time, using the Properties box)

3. Create a button (Command1).

4. Enter this code for Command1:



Private Sub Command1_Click()

Static i As Integer

If i %26lt; 1 Then i = 1

Load Text1(i)

Text1(i).Text = ';New TextBox '; %26amp; i

Text1(i).Visible = True

Text1(i).Top = Text1(i - 1).Top + Text1(i - 1).Height

i = i + 1

End Sub



Each time Command1 is clicked a new textbox is created below the previous one.



Obviously, you can call the code using something other then a command.



If you don't know how VB uses the .index I suggest you read up on it on MSDN or something, it's pretty simple.



Don't forget to set the properties of your new control.

No comments:

Post a Comment