Enabling and disabling controls with toggle button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

Simple question: I have eight textboxes on a form which I wish to be
disabled by default, but which can be enabled for input by means of a toggle
button. How would I go about achieving this?

Your help would be greatly appreciated!
 
This example uses the Tag property to determine which controls should be
enabled. You'll find the Tag property on the 'Other' tab of the Properties
window in Form Design view.

Private Sub Toggle7_Click()

Dim ctls As Controls
Dim ctl As Control

Set ctls = Me.Controls
For Each ctl In ctls
If ctl.Tag = "EnableMe" Then
ctl.Enabled = Not ctl.Enabled
End If
Next ctl

End Sub
 
In the Click event of the toggle button, set the Enabled property of the
controls to the value of the toggle button. This will enable the controls
when the button is pressed and disable them when the button is up.

Example:
Me.MyControl1.Enabled = Me.ToggleButton
Me.MyControl2.Enabled = Me.ToggleButton

You will also want to either reset the toggle button to False and/or set the
Enabled property of the controls in the form's Current event so that the
enabled property of the controls will be correct as you move from record to
record.

If you name the controls similar to what I have listed above (with a common
root name followed by a sequential number), you can save a little typing by
using the following:

For i = 1 To 8
Me.Controls("MyControl" & i).Enabled = Me.ToggleButton
Next i
 
Miguel said:
Hi!

Simple question: I have eight textboxes on a form which I wish to be
disabled by default, but which can be enabled for input by means of a
toggle
button. How would I go about achieving this?

Your help would be greatly appreciated!

In your command button's Click event:

If Me.cmdMyButton.Caption = "Enable textboxes" Then
Me.cmdMyButton.Caption = "Disable textboxes"
Me.txtMyTextBox1.Enabled = True 'Repeat for remaining text boxes
Else
Me.cmdMyButton.Caption = "Enable textboxes"
Me.txtMyTextBox1.Enabled = False 'Repeat for remaining text boxes
End If

In your form's Open event, set the button's default caption to "Enable
textboxes" (or whatever takes your fancy, but it must match what's in the
code).

HTH - Keith.
www.keithwilby.com
 
Hello

i am trying to acheive the same results with 2 check boxes. do you not need
a field in your table to do the enable disable toggle with the fields?

i have a table where i want to be able to have a form control disabled by
default but that is enabled when the user checks a check box.

in my form there is a field for a Subtotal, GST, PST and Total. the subtotal
needs to be typed in no matter what, but the GST and PST are dependant on
whether they were charged or not. the total field is where all three,
Subtotal, GST, PST are totaled up.

i want the controls for GST and PST to be disabled until the check box is
checked. because the GST and PST are stored information i have a field in the
table that the form is based on. do i not have to have a yes no field to tell
the form whether the GST or PST controls should be enabled?

further how do i get the check boxes to cause the GST and PST to toggle
thier enable disabled state? there are three solutions here and i dont know
which one is the correct one for my situation.

i need to know where to put the code, and what code to put there. thanks for
any and all help! :)
 
Back
Top