While 
               
       		GetSelectedOptionIndex 
       
       shows a handy method to retrieve the index of the selected item when 
       using an option button control array, due to the hard-coding of the 
       control array name the method is restricted to just that control array. 
       Using this technique when a form contains many option control arrays 
       requires a separate procedure for each control name. 
       
       Although the method below, based on a post in the msnews groups by Rick Rothstein, uses the Object 
       data type and therefore late binding, it does provide additional 
       flexibility over the abovementioned routine in that it accepts any option 
       button control array on the form. 
       
       As coded, if no option buttons are selected the routine returns -1 to 
       indicate no selection. The routine also returns -1 if an incorrect 
       control type is passed (for example an array of text boxes rather than 
       option buttons) something that theoretically should never happen as you 
       have command of the application code. 
        | 
   
   
      
         Option Explicit 
         
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
Private Sub Form_Load()
  Dim cnt As Integer
  Dim nControls As Long
  
  nControls = 6
  
  For cnt = 0 To nControls - 1
   
     'control(0) already exists; don't try to load it!
      If cnt > 0 Then Load Option1(cnt)
      
     'position and show
      With Option1(cnt)
         .Move 300, 200 + (cnt * 300), 3500, 285
         .Caption = "Option button - index #" & cnt
         .Visible = True
      End With
   Next
   
   Command1.Caption = "What's Selected?"
   Command2.Caption = "Deselect All"
   
   Command1.Move Option1(0).Left, _
                 Option1(nControls - 1).Top + _
                 Option1(nControls - 1).Height + 230
                 
   Command2.Move Command1.Left + Command1.Width + 50, _
                 Command1.Top, _
                 Command1.Width, _
                 Command1.Height
End Sub
Private Sub Command1_Click()
   MsgBox "The button selected is index #" & GetSelectedOptionIndex2(Option1)
End Sub
Private Sub Command2_Click()
   Dim ctl As Control
   
   For Each ctl In Me
      If TypeOf ctl Is OptionButton Then
         ctl.Value = False
      End If
   Next
   
End Sub
Private Function GetSelectedOptionIndex2(OB As Object) As Long
   Dim ctl As OptionButton
  
  'default return value if nothing
  'is selected
   GetSelectedOptionIndex2 = -1
  
   If VarType(OB) = vbObject Then
      If TypeOf OB(OB.LBound) Is OptionButton Then
    
         For Each ctl In OB
            If ctl.Value Then
               GetSelectedOptionIndex2 = Abs(ctl.Value * ctl.Index)
               Exit For
            End If
         Next
      
      End If  'If TypeOf
   End If  'If VarType
End Function |