• FOR NEXT control

    Author
    Topic
    #466434

    Hi all,

    I am trying to test buttons again, I have a form with more than a few optionbuttons inside a frame control, in the frame, I also have other controls, but none that would give a true false value, except for the optionbutton. I can’t figure out why my code will not work.

    I keep getting “objects doe not support”

    Can anyone see wrong with this code?

    Thanks,
    Darryl.

    option explicit

    Public Function AreAnyTrue() As Boolean
    Dim ctl As Control
    Dim z As String
    Dim opt As OptionButton
    UnitsAreAnyTrue = False
    For Each opt In UserForm2.FrUnit
    If ctl.Value = True Then
    UnitsAreAnyTrue = True
    Exit Function
    End If
    Next opt
    End Function

    Viewing 4 reply threads
    Author
    Replies
    • #1208173

      Try this:

      Code:
      Public Function AreAnyTrue() As Boolean
      	Dim ctl As Control
      	Dim z As String
      	AreAnyTrue = False
      	For Each ctl In UserForm2.FrUnit.Controls
       	If TypeName(ctl) = "OptionButton" Then
       	If ctl.Value = True Then
       	AreAnyTrue = True
       	Exit Function
       	End If
       	End If
      	Next ctl
      End Function
      
    • #1208934

      Hey Rory,

      Thanks for your reply,

      You gave me that code about 4 years ago:)

      Can you tell me why I can’t get this to work?

      Passing ZX would be an arugment like Userform1.frame1.controls?

      I have about 10 different frames with option buttons and I thought if I could modify your code to possibly handle arguments.

      Public Function TestAreAnyTrue(ZX As Variant) As Boolean
      Dim ctl As Control
      TestAreAnyTrue = False
      For Each ctl In ZX
      If ctl.Value = True Then
      TestAreAnyTrue = True
      End If
      Next ctl
      End Function

    • #1208997

      Is this a legal function?

      Any one?

      Public Function TestAreAnyTrue(ZX As Collection) As Boolean
      Dim ctl As Control
      TestAreAnyTrue = False
      For Each ctl In ZX
      If ctl.Value = True Then
      TestAreAnyTrue = True
      End If
      Next ctl
      End Function

    • #1209014

      I ran a test and the function will work with one minor change: Define ZX as Object vs Collection.

      Code:
      Option Explicit
      '*** Place this code in a Module --- not the form ***
      Public Function TestAreAnyTrue(ZX As Object) As Boolean
                     Dim ctl As Control
                     TestAreAnyTrue = False
                     For Each ctl In ZX
                        If ctl.Value = True Then
                          TestAreAnyTrue = True
                        End If
                     Next ctl
      End Function

      Calling code in form process/OK button:

      Code:
      Private Sub cbProcess_Click()
      
         Debug.Print TestAreAnyTrue(UserForm1.frOne.Controls)
         Unload Me
      
      End Sub

      Results of two runs…1. All false 2. One true

      Code:
      False
      True
      

      RG

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1209043

      Thanks dude,

      I tried a few variations of the code, I have never passed an object with an argument.

      Thank you kindly.

      HUH:)

      Oops that not true…I have, but I haven’t used it as the argument.

    Viewing 4 reply threads
    Reply To: FOR NEXT control

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: