Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    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
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  4. #4
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  5. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    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
    Attached Images Attached Images
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  6. #6
    4 Star Lounger
    Join Date
    May 2004
    Location
    Toronto, Ontario, Canada
    Posts
    425
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •