Toggle Control Properties

R

Robert

I have a Public code in a standard module that I am
activating with a command button on a form. This code is
intended to switch the Locked and Enabled controls from
True to False and back again. The code works fine with the
exception that the font once changed to red does not
revert back to black when the properties have been set as
False.

Any help is greatly appreciated. The code is as follows
and I thank you in advance...

Public Function ToggleMode()
Const Red As Integer = 255
Dim ctl As Control
For Each ctl In Screen.ActiveForm
If TypeOf ctl Is TextBox Then
With ctl
.Locked = Not .Locked
.Enabled = Not .Enabled
.ForeColor = Red
Forms!frmReqEdit.Caption = "Locked"
End With
End If
Next ctl
End Function
 
D

Dirk Goldgar

Robert said:
I have a Public code in a standard module that I am
activating with a command button on a form. This code is
intended to switch the Locked and Enabled controls from
True to False and back again. The code works fine with the
exception that the font once changed to red does not
revert back to black when the properties have been set as
False.

Any help is greatly appreciated. The code is as follows
and I thank you in advance...

Public Function ToggleMode()
Const Red As Integer = 255
Dim ctl As Control
For Each ctl In Screen.ActiveForm
If TypeOf ctl Is TextBox Then
With ctl
.Locked = Not .Locked
.Enabled = Not .Enabled
.ForeColor = Red
Forms!frmReqEdit.Caption = "Locked"
End With
End If
Next ctl
End Function

There's nothing there that conditionally changes the ForeColor -- it
just always sets it to Red. You don't need to define your own color
constants for red and black, by the way; VB has predefined them. Your
code could be modified as follows:

'---- start of modified code ----
Public Function ToggleMode()

Dim ctl As Control

For Each ctl In Screen.ActiveForm

If TypeOf ctl Is TextBox Then
With ctl
.Locked = Not .Locked
.Enabled = Not .Enabled
If .Locked = True Then
.ForeColor = vbRed
Else
.ForeColor = vbBlack
End If
Forms!frmReqEdit.Caption = "Locked"
End With
End If

Next ctl

End Function
'---- end of modified code ----

It seems likely that the line
Forms!frmReqEdit.Caption = "Locked"

setting the caption unconditionally, should also be inside the "If
..Locked = True Then ... Else ... End If" structure, but I didn't want to
tinker with that in the absence of confirmation. Is "frmReqEdit" the
active form being manipulated, in which case you would do better to use
another Screen.ActiveForm reference instead of hard-coding the form
name? Or is it a popup status form of some sort?
 
R

Robert

"frmReqEdit" was just put there for a quick change to the
caption line instead of embedding a MsgBox for testing the
code. But, thanks a lot for the help!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top