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!

optionbutton problem

Status
Not open for further replies.

softlover

Programmer
Apr 4, 2002
88
0
0
CN
In EXCEL userform I put some OptionButtons in a Frame, then I select one of the OptionButtons and want return the Caption of the selected OptionButton.

Private Sub MyFrame_Enter()
Dim objControl As Control
For Each objControl In MyFrame.Controls
If objControl.Value = True Then
MsgBox objControl.Caption
Exit For
End If
Next
End Sub
 
That's ok, the problem could be that the 'Enter' event fires only when one of frame controls gets focus and before updating anything. You can try:
Code:
Private Sub myFrame_Enter()
Call Test("Enter")
End Sub

Private Sub myFrame_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call Test("Exit")
End Sub

Private Sub Test(TestWhen As String)
For Each cntrl In Me.myFrame.Controls
    If TypeName(cntrl) = "OptionButton" Then
        If cntrl.Value = True Then
            MsgBox TestWhen & ": " & cntrl.Caption
            Exit For
        End If
    End If
Next cntrl
End Sub

However, the typical way of interacting with option buttons is their 'Click' event.

combo
 
You really should be using, as combo mentions, the Click event of the control.

Say you have a frame with two option buttons in it. Clicking an option button - to select it as True - will indeed fire Frame_Enter...but it will fire before you get at the control.

Example:

optButton1 is not selected (= False)
optButton2 is not selected (= False)

Clicking either button, your code returns nothing as neither control = True.

optButton1 is already selected (= True)
optButton2 is not selected (= False)

Clicking optButton2 - assuming focus is indeed outside the frame - will indeed display "optButton1". However, after the message is displayed, you will have to click optButton2 a second time. What is the point of that?

Further, say optButton1 is true (optbutton2 is false) and you click optbutton2, if you are clicking between them, then Frame_Enter is NOT fired, so even though which button is True changes, you do not get a display.

Better to use the Click event of the control, although I fail to see the actual point of this at all, really.

What is it you are trying to achieve?


Gerry
 
What I want to see is when I click one of any optionbuttion in the frame the caption information displayed to show which one selected. When click another one then display the caption of another optionbutton.
It seems simple but I failed with codes for Frame_Click or Frame_Enter event.
I know that the Optionbuttion_Click event can solve the problem but I don't like so long codes when I have a lot of optionbutton in a frame.
 
There is no control arrays in vba, you could adapt a tip from my faq707-4976.

combo
 
What I want to see is when I click one of any optionbuttion "

my bolding

Is that not...the Click event of optionbutton? I do not see why you are adverse to using it. Yes, if you have many it is a pain, but then I fail to see why you are doing this in the first place. What advantage are you gaining?

You can try combo's suggestion, but you are correct: Frame_Click and Frame_Enter are not going to do it for you. At least as far as I understand what you are trying to do.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top