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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling text boxes

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
I want to click a button on a form that would Disable/enable all text boxes on a form and subform

As there a loads of textboxes, I would prefer not to do things like
Code:
txtbox.enabled = true

Is there like a 'Collection' object which I could use to refer to all all text boxes on a form. I know
Code:
Controls(index)
refer to the controls, but how could I check whether it is a text box or not, as not all controls could be disabled/enabled..??
 
There is a type (or something like that) property of the controls collection, which you can use.

Something like

dim ctl as control

for each ctl in Me!Controls
if ctl.type = acTextBox then.....

end if
next


Look up acTextBox in Help. That should point you in the right direction.

Kathryn


 
It should be
Code:
Me.Controls
but
Code:
ctl.type
is an invalid properties for Control object, so is
Code:
ctl.enabled
!!

Help!!
 
This works- :)
Code:
 Dim ctl As Control
 For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
        ctl.Enabled = False
    End If
 Next
 
The following doesn't work though when I try to disable + grayout textboxes on subforms.
Code:
Dim ctl as Control
 For Each ctl In Me!childDetails.Controls
    If ctl.ControlType = acTextBox Then
        ctl.Enabled = False
    End If
Next
I get the following error-
Run time error 2164
Can't disable a control while it has the focus



 
You can go to Tools->Advanced Tab and set the code to break on all errors. Then when you get to the error, the code will pause and you can use the Debug Window to find out what control is causing the problem.

To get to the Debug Window, type Control-G. In the lower pane, you can enter an expression preceded by a ? to find out various values. Alternatively, look in the upper pane and you can navigate through the hierarchies shown.

I have found that it is usually quicker to use the Debug Window. In your case, type:
?ctl.name
to find the name of the control that is causing the error.

Hope this helps.

Kathryn


 
If your intent is to keep records from being accidentally edited...

I use

me.allowedits = false
me.allowadditions = false

To lock editing of the form and created a button to "Edit" the record that would switch the allows to true.

Just a thought... since I'm not sure what you are trying to accomplish.

Mary :eek:)
 
The reason you are getting that error is because, as it says, you can't disable a control when it has focus. Try setting the focus to some non-textbox control, like a label, before you run your loop. The syntax is:

lblLabel.SetFocus

:-Q Durkin
alandurkin@bigpond.com
 
The code given earlier will work fine, you just need to set focus to any other control other than a text box in the line before you attempt to disable the text boxes. This will stop the error.

HTH :-Q
RDH

Ricky Hicks
rdhicks@mindspring.com

 
I did try to setfocus to somthing else, and not it didn't work!
so I set the backgroud colour and lock the texboxes instead :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top