How to pass a frame to a function

jriano

Registered User.
Local time
Yesterday, 19:56
Joined
Dec 9, 2003
Messages
45
[RESOLVED]How to get the caption of a radio button with a function

Hello guys, I hope you can give me a hand.
I want to have a function that I can pass a frame to (a frame that contains a group of radio buttons), and return the caption of the button selected. Basically I have two problems, I dont know how to pass the form to the function. second, I dont know how to access the properties of the frame within the function. This is the code. It does not work at all, I post it so you guys get an idea of what I want.
Thanks

Code:
Private Sub Toggle8_Click()
    Dim str As String
   
    str = getSelectedOptionInFrame(Me.Frame0)
    MsgBox "the caption of the buton is" & str
End Sub

Public Function getSelectedOptionInFrame(fraFrame As Frame) As String
    Dim bytNumOptions As Byte   'The number of options in the frame
    Dim i As Byte      'Counter
    bytNumOptions = fraFrame.Controls.count 'HERE I WANT TO COUNT ONLY OPTION BUTTONS

    For i = 1 To bytNumOptions  'I WANT TO LOOP ONLY OPTION BUTTONS
        If fraFrame = i Then ' the selected option button
             'I WANT TO RETURN THE CAPTION OF THE OPTION BUTTON
            getSelectedOptionInFrame = ' i dont know how to reference the caption of the option button that matches the condition
        End If
    Next i    
End Function
 
Last edited:
G’day jriano

Give this a shot: -

Behind the Form.
Code:
Option Explicit
Option Compare Text

Private Sub Toggle8_Click()
   
    MsgBox "The caption of the buton is " & GetSelectedOptionInFrame(Me.Frame0)
    
End Sub

In the Global Module.
Code:
Option Explicit
Option Compare Text

Public Function GetSelectedOptionInFrame(ByRef ctlFrame As Control) As String
    Dim lngI As Long
    
    For lngI = 1 To ctlFrame.Controls.Count - 1 Step 2
        If ctlFrame.Controls.Item(lngI).OptionValue = ctlFrame Then
           GetSelectedOptionInFrame = ctlFrame.Controls.Item(lngI + 1).Caption
           Exit For
        End If
    Next lngI
    
End Function
Hasn’t been fully tested but should get you started.

Regards,
Chris.
 
Any chance you could tell a little more on what you're going to use the function for, I suspect an easier solution could be found...
 
Yes there probably is a simpler way and it would help to know how you wish to use it.

Just for a little clarification, the Controls.Items(x) seem to be like this: -

An Option Group that has three selections...

ctlFrame.Controls.Item(0) = Frame Label
ctlFrame.Controls.Item(1) = Option 1
ctlFrame.Controls.Item(2) = Option 1 Label
ctlFrame.Controls.Item(3) = Option 2
ctlFrame.Controls.Item(4) = Option 2 Label
ctlFrame.Controls.Item(5) = Option 3
ctlFrame.Controls.Item(6) = Option 3 Label

ctlFrame = Value of the Selected Option.

Not something I have looked at before but hope it helps.

Regards,
Chris.
 
hi

Thank you for anwering guys, I just got back to work. Well basically I want to be able to pick the caption of the selection made on any group of radio buttons within a frame. so that i can tell the "name" of the selection made by the user. like for example in a frame called "Frecuency", with radio butons "Daily", "Monthly", "Yearly", I can give a confirmation message to to user saying "Yo have selected Monthly". But i want to return the caption, not the index, so I can reuse it with any other frame group. Also y prefer to use a loop to count the radio buttons, again, so that I can use the same function with different frames, no matter how many radio buttons it has. Thanks again, and I am going to try the code in your replies and will be back to you.
 
Last edited:
ChrisO, I loved your code, the only problem is that the line in red does not work. The method is not supported. I have tried other methods, but can not figure out what to do.

Code:
Option Explicit
Option Compare Text

Public Function GetSelectedOptionInFrame(ByRef ctlFrame As Control) As String
    Dim lngI As Long
    
    For lngI = 1 To ctlFrame.Controls.Count - 1 Step 2
        If ctlFrame.Controls.Item(lngI).[COLOR=Red]OptionValue[/COLOR] = ctlFrame Then
           GetSelectedOptionInFrame = ctlFrame.Controls.Item(lngI + 1).Caption
           Exit For
        End If
    Next lngI
    
End Function
 
well, i just realized that option buttons in access do not have caption, they instread have an associated label, so I need to check for the caption of the associated label of the radio button selected, but even though i know that, i have not been able so solve this...l
 
Sounds like a lot of trouble. Why not just do some hard code for this one frame/form and put a copy in txt file where you put other chunks of reusable code...
 
Looks like the question has been changed slightly but here is the database I tested it in.

If that’s not what you require can you please post an A97 version of what you have.

Regards,
Chris.
 

Attachments

Thanks ChrisO, that is exactly what I wanted. I knew it was not difficult (although it was for me, I could not do it). I am posting the code here, so everyone else can read it. Thanks again.
Code:
Private Sub Toggle8_Click()
    MsgBox "The caption of the buton is " & GetSelectedOptionInFrame(Me.Frame0)
End Sub

Public Function GetSelectedOptionInFrame(ByRef ctlFrame As Control) As String
    Dim lngI As Long
    
    '   An Option Group with three selections...
    
    '   ctlFrame.Controls.Item(0) = Frame Label
    '   ctlFrame.Controls.Item(1) = Option 1
    '   ctlFrame.Controls.Item(2) = Option 1 Label
    '   ctlFrame.Controls.Item(3) = Option 2
    '   ctlFrame.Controls.Item(4) = Option 2 Label
    '   ctlFrame.Controls.Item(5) = Option 3
    '   ctlFrame.Controls.Item(6) = Option 3 Label
    
    '   ctlFrame = Value of the Selected Option.
    
    For lngI = 1 To ctlFrame.Controls.Count - 1 Step 2
        If ctlFrame.Controls.Item(lngI).OptionValue = ctlFrame Then
           GetSelectedOptionInFrame = ctlFrame.Controls.Item(lngI + 1).Caption
           Exit For
        End If
    Next lngI
End Function
 

Users who are viewing this thread

Back
Top Bottom