Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enable and Disable Text Box

Status
Not open for further replies.

dobber

Programmer
Aug 18, 2000
21
0
0
US
I have fourteen text boxes on a form named "txtBox1", "txtBox2", "txtBox3", etc. I have another text box named "txtNumColumnsNeeded" into which the user types a number (1 to 14). Depending on what number is typed into "txtNumColumnsNeeded", I want to disable (Enabled = False) the text boxes coming after this number. For example - If the user types 3 into the text box, I want to disable "txtBox4", "txtBox5", "txtBox6", etc. through "txtBox14".

I know I could write code something like this:

If me.txtNumColumnsNeeded.value = 1 then
me.txtBox2.Enabled = false
me.txtBox3.Enabled = false
me.txtBox4.Enabled = false
etc., etc., etc., etc., etc.,
Endif

But there must be an easier (shorter) way. Can someone help?
 
Hi!

In the after update event of your number of columns text box this code:

Dim strName As String
Dim intColumns As Integer
Dim intCounter As Integer

intColumns = CInt(txtNumColumnsNeeded.Value) + 1
For intCounter = intColumns To 14
strName = "txtBox" & Format(intCounter)
Me.Controls(strName).Enabled = False (or .Visible)
Next intCounter

hth
Jeff Bridgham
bridgham@purdue.edu
 
The better way to do this, is to change all your text boxes. Rename them all to the same Name "txtBox", then Index them.

So you delete all the text boxes, then add a new Text Box called txtBox. Copy this text Box and paste it. Access should ask you if you want to create a control array, say yes. Then repast all your test boxes in. Now instead of having to change all your text boxes one by one, you create a loop.
[tt]
For i = 0 to 13
txtBox(i).Enabled = False
Next i
'Then activate the ones you want active
textBox(2).Enabled = True
textBox(5).Enabled = True
[/tt]

Or you could keep it the way it is and add the text boxes to a collection, though I'm not sure how to do this in Access, in VB it's done like.
[tt]
'Do this part in the General Declarations
Public objCol as Collection

'This only needs to be done once in your program, usually in the Form Load function
objCol.Add txtBox1
objCol.Add txtBox2
... etc.

'Then in the place where you want to active the text boxes, this will set them all to off.
objCol.Enabled = False
[/tt] Craig, mailto:sander@cogeco.ca
"Procrastination is the art of keeping up with yesterday."
I hope my post was helpful!!!
 
Thanks for the great responses. I was able to both solve my problem and gain some better ideas for designing forms in the future. I really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top