PDA

View Full Version : make textboxes visible at runtime?



hypuk
10-11-2009, 07:41 AM
Hi there

I'm new to VBA and was wondering how I would go about doing this.

Basicaly I have 9 sets of text boxes on my userform and only wanted 1 set to appear unless the user enters 2 in the textbox (number of guests) therefore 2 sets of text boxes would appear and so on.

Any help would be much appreciated

Thanks
Mark

Plagööri
10-11-2009, 08:46 AM
Hi,

Assuming you stick with the Standardnames of the Textboxes (TextBoxN) and they are numbered 1..10, then try this code for TextBox1:
Private Sub TextBox1_Change()
Dim iX As Integer
If Val(TextBox1) < 11 And Val(TextBox1) > 1 Then
For iX = 2 To 10
Me.Controls("TextBox" & iX).Visible = (iX <= Val(TextBox1))
Next iX
End If
End Sub
GreetZ Hansueli

hypuk
10-11-2009, 08:54 AM
Hi Hansueli

thanks for the reply, so which part of the code do I change for the "number of guests" text box?

Thanks
Mark

hypuk
10-11-2009, 08:55 AM
Here is a screen sho to give a clearer picture

Thanks
Markhttp://markalmondlive.co.uk/12.jpg

Plagööri
10-11-2009, 08:57 AM
Hi Mark,

OK, It looks you have a lot of controls.
If your NrOfGuest Textbox is named TextBox6 and your Guest(1) TextBox is named TextBox7 and the next TextBox8 aso, then modify the Code below as follows:

Private Sub TextBox6_Change()
Dim iX As Integer
If Val(TextBox6) < 11 And Val(TextBox6) > 1 Then
For iX = 2 To 10
Me.Controls("TextBox" & (5 + iX)).Visible = (iX <= Val(TextBox6))
Next iX
End If
End Sub
GreetZ Hansueli