Switching optionbuttons – how to make it a correct
Hi All :wave:
I write small app in a VBA. I have on Form a four an OptionButton. I want now to make that after switched they changed BackColor and ForeColor.
in this way I do it but this looks on a bad.
VB Code:
Private Sub optCztery_Click()
If optCztery.Value = True Then
optCztery.BackColor = &HC0FFFF
optCztery.ForeColor = &HFF0000
optTrzy.BackColor = 10931133
optTrzy.ForeColor = &H4080&
optTrap.BackColor = 10931133
optTrap.ForeColor = &H4080&
optProst.BackColor = 10931133
optProst.ForeColor = &H4080&
CommandButton4_Click
End If
End Sub
Private Sub optProst_Click()
If optProst.Value = True Then
optProst.BackColor = &HC0FFFF
optProst.ForeColor = &HFF0000
optTrzy.BackColor = 10931133
optTrzy.ForeColor = &H4080&
optTrap.BackColor = 10931133
optTrap.ForeColor = &H4080&
optCztery.BackColor = 10931133
optCztery.ForeColor = &H4080&
CommandButton4_Click
End If
End Sub
Private Sub optTrap_Click()
If optTrap.Value = True Then
optTrap.BackColor = &HC0FFFF
optTrap.ForeColor = &HFF0000
optTrzy.BackColor = 10931133
optTrzy.ForeColor = &H4080&
optProst.BackColor = 10931133
optProst.ForeColor = &H4080&
optCztery.BackColor = 10931133
optCztery.ForeColor = &H4080&
CommandButton4_Click
End If
End Sub
Private Sub optTrzy_Click()
If optTrzy.Value = True Then
optTrzy.BackColor = &HC0FFFF
optTrzy.ForeColor = &HFF0000
optProst.BackColor = 10931133
optProst.ForeColor = &H4080&
optTrap.BackColor = 10931133
optTrap.ForeColor = &H4080&
optCztery.BackColor = 10931133
optCztery.ForeColor = &H4080&
CommandButton4_Click
End If
End Sub
how to make this better, maybe in this some a loop?
Thanks in advance
Tamgovb
Re: Switching optionbuttons – how to make it a correct
Well, since the code in the If statements is the same, put it in a separate sub and call it from each option button's click event.
Re: Switching optionbuttons – how to make it a correct
thanks you for reply.
Note, that I can't make so. :cry: Because those OptionButtons each time are there other (have a look on their name) the array of controls would be the best solution, but there is no something like this in a VBA. :sick:
I have Win98 and Office2000
thanks
tamgovb
Re: Switching optionbuttons – how to make it a correct
VB Code:
Private Sub optCztery_Click()
If optCztery.Value = True Then
SwitchColors
End If
End Sub
Private Sub optProst_Click()
If optProst.Value = True Then
SwitchColors
End If
End Sub
Private Sub optTrap_Click()
If optTrap.Value = True Then
SwitchColors
End If
End Sub
Private Sub optTrzy_Click()
If optTrzy.Value = True Then
SwitchColors
End If
End Sub
Private Sub SwitchColors()
optTrap.BackColor = &HC0FFFF
optTrap.ForeColor = &HFF0000
optTrzy.BackColor = 10931133
optTrzy.ForeColor = &H4080&
optProst.BackColor = 10931133
optProst.ForeColor = &H4080&
optCztery.BackColor = 10931133
optCztery.ForeColor = &H4080&
CommandButton4_Click
End Sub
Maybe I'm missing something? What's wrong with the above?
You could loop through controls, checking for the ones you want as well.
1 Attachment(s)
Re: Switching optionbuttons – how to make it a correct
Thanks, sorry but.....you didn't understand me probably. I attached file and you have a look. Your solution can't work correctly because always it a refer to a different of group of objects.
So therefore please you again, you have a look on the names of objects - always are other
thanks
Re: Switching optionbuttons – how to make it a correct
Ok, I didn't realize this was Excel. I don't know as much about Excel, so take this for what it's worth.
You may need to put Me. in front of your statements:
VB Code:
Me.optTrap.BackColor = &HC0FFFF
For some things you may need to SetFocus before setting properties:
VB Code:
optTrap.SetFocus
optTrap.BackColor = &HC0FFFF
I'm not sure when you need to do one or the other. I think it depends on what property you're setting.
Anyway, I can't test it on the file you sent because security settings won't let me change the code.
Re: Switching optionbuttons – how to make it a correct
Hi All :wave:
Okay, I found here any code and I modified it
An works it fine for me, but I have other problem now.
I have a three of sections (frame) with the optionbutton controls. If I use this code
then this code to make that only one option will be active and all other will be not active. It will be not correctly.
My question is:
how to make that those all of section would be it works completely of irrespective
here's this code, it work with the objects (optionbutton) only in a one Frame.
VB Code:
Private Sub Highlight_Control(Optional MarkControl As Control)
Dim cMyControl As Control
For Each cMyControl In Me.Controls
If Left(cMyControl.Name, 3) = "Opt" Then
With cMyControl
.BackColor = 10931133
.ForeColor = &H4080&
End With
End If
Next cMyControl
If Not MarkControl Is Nothing Then
With MarkControl
.BackColor = &HC0FFFF
.ForeColor = &HFF0000
End With
End If
End Sub
'>>> for a optionbutton:
Private Sub optTrap_Click()
Highlight_Control optTrap
End Sub
'>>>. . . . . . etc, etc for each the optionbuttons in a this section
thanks in advance
Re: Switching optionbuttons – how to make it a correct
Tamgovb
You will need to change this lie
VB Code:
For Each cMyControl In Me.Controls
to
VB Code:
For Each cMyControl In Me.Frame1.Controls
or whatever the name of the frame is. This change means that the code now only loops throught the controls in frame1 and not all the controls on the form
1 Attachment(s)
Re: Switching optionbuttons – how to make it a correct
Hi All
Thanks DKenny, but I about this knew and made so
If you have three section (a three frames and in each it's after two, three or four optionbuttons)
it will not be well to work, because active only one option will be. These are three independent section
Look on attached images:
Thanks
Re: Switching optionbuttons – how to make it a correct
Try the following version of the Highlight_Control procedure. When calling it, you will need to include both the OptionButton and the Frame as parameters.
e.g.
VB Code:
Private Sub OptionButton1_Click()
Highlight_Control Me.OptionButton1, Me.Frame1
End Sub
Here's the revised procedure.
VB Code:
Private Sub Highlight_Control(ByRef MarkControl As Control, ByRef OptFrame As Control)
Dim cMyControl As Control
If TypeName(OptFrame) <> "Frame" _
Or TypeName(MarkControl) <> "OptionButton" Then
Exit Sub
End If
For Each cMyControl In OptFrame.Controls
If TypeName(cMyControl) = "OptionButton" Then
With cMyControl
.BackColor = 10931133
.ForeColor = &H4080&
End With
End If
Next cMyControl
With MarkControl
.BackColor = &HC0FFFF
.ForeColor = &HFF0000
End With
End Sub