Jump to content

adding multiple text boxes to a form


comcu

Recommended Posts

Hi,

 

I hope someone can help.

 

I am trying to add a command button to a form. This button, when pressed will display an unlimited number (with in reason) of text boxes ,called TBxInt ready for the users input. The qty of the text box ‘TBxInt ‘ depends on the value held by another text box, called TBxVal.

 

For eg

if TBxVal.value = “9” then create TBxInpt(0-9)

TBxInpt.width = 8
TBxInpt(0).left = 0
TBxInpt(1).left = 10
TBxInpt(2).left = 20

 

Etc etc

The user can then input values into each text box and the values will be used for further calculations.

If anyone knows of any example or tutorials on this I would appreciate it

 

Many thanks,

col

Link to comment
Share on other sites

In VB, this would be called a "Control Array", and its built in.

But in VBA, since we dont have Control Arrays, we'll make our own Array of Controls.

 

' in a blank VBA userform, drop in the following code, run the form,

'and click the form to have it create the controls on the fly.

 

' ---- snip -------- snip -------- snip -------- snip ----

 

 

Dim cmdButton() As MSForms.CommandButton

 

Private Sub UserForm_Click()

 

Dim numButtons As Integer: numButtons = 4

ReDim cmdButton(numButtons)

Dim I As Integer

 

For I = 0 To numButtons

' create a control on the fly

Set cmdButton(I) = Me.Controls.Add("Forms.CommandButton.1", "Command" & LTrim$(Str$(I)), True)

 

' set its position and size

cmdButton(I).Height = 20: cmdButton(I).Width = 100

cmdButton(I).Left = 5: cmdButton(I).Top = (I * 25) + 5

cmdButton(I).Caption = "Command " & I

Next I

 

' resize the form around the text boxes

Me.Width = 115

Me.Height = ((numButtons + 2) * 25)

 

End Sub

 

' ---- snip -------- snip -------- snip -------- snip ----

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...