Results 1 to 15 of 15
  1. #1
    Thompyt is offline Expert
    Windows 11 Office 365
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862

    Set controls Value to True

    How do I set the checkbox control to be true if the caption value is met? I am having issues with the bold. Thanks



    Code:
    Private Sub Report_Load()
    
        Call SearchControlCaption("RptSoleSource", Forms!frmSoleSingle.CHCks)
    
    End Sub
    
    Sub SearchControlCaption(reportName As String, searchCaption As String)
    Dim rpt As Report
    Dim ctrl As Control
        
        Set rpt = Reports(RptSoleSource)
        
        For Each ctrl In rpt.Controls
            If TypeOf ctrl Is Label Then
                If ctrl.Caption = searchCaption Then
                    If ctrl Is CheckBox Then
                      Set ctrlv.Value = True
                        Exit Sub
                    End If
                End If
            End If
        Next ctrl
        
    End Sub

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You're testing if ctrl is a label, therefore within those nested IF's it cannot be a checkbox as well.

    I don't recognize some of the words you've used there, such as label or checkbox as a control type. Methinks it's acCheckbox and acLabel. I also think that TypeOf is an operator that's used to return the data type of an expression. However, if you use TypeName(ctrl) then checkbox, label or textbox would be valid. The property name of an object is not the same as the type property.
    Last edited by Micron; 05-01-2025 at 10:31 AM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    is the checkbox associated with the Label?
    if it is, try:
    Code:
    Sub SearchControlCaption(reportName As String, searchCaption As String)
    Dim rpt As Report
    Dim ctrl As Control
        
        Set rpt = Reports(RptSoleSource)
        
        For Each ctrl In rpt.Controls
            If TypeOf ctrl Is CheckBox Then
                If ctrl.Controls(0).Caption = searchCaption Then
                    Set ctrl.Value = True
                    Exit Sub
                End If
            End If
        Next ctrl
        
    End Sub

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    jojo, don't use Set with setting a control's value. Just ctrl.Value = True. Then code works.
    Use Set for setting object variables.

    Thom, what report section is this control located in? I don't understand report setup. Why would unbound checkbox value be dependent on caption?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Thompyt is offline Expert
    Windows 11 Office 365
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862
    jojowhite
    The checkbox is associated with the label. Your code fails on "Set ctrl.Value = True" and give runtime error 94 Object required. Same as what I was getting with mine.

    Micron
    I changed the names to acctrl & acreport. Makes sense and less confusion.

    I am searching among the checkbox controls with the caption that is the same as searchCaption. Once found I want to set the checkbox to have a checkmark. Setting it to true. I have all the checkbox controls referenced to 1 control ChkBoxID with the Name of the control changed to the reference ID from a previous form combobox.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You have a typo?
    ctrlv.value not ctrl.value
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    did you see post 4? Set is for objects only.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Either of these versions should work
    Code:
    For Each ctrl In rpt.Controls
        If TypName(ctrl) = "Checkbox" And ctrl.Controls(0).Caption = searchCaption Then ctrl = True
    Next
    
    For Each ctrl In rpt.Controls
        If TypeName(ctrl) = "Checkbox" And ctrl.Controls(0).Caption = searchCaption Then 
            ctrl = True
            Exit Sub
        End If
    Next
    EDIT - if there is any checkbox that doesn't have a label or later, you dis-associate one, I'm fairly certain either of those will raise an error.
    EDIT 2 - edited code. I have a habit of typing ctl, not ctrl so had to fix, and fix typo on TypeName
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Sighhh, you can't have both tests on one line. I got this to run
    Code:
    Dim ctrl As Control
    Dim searchCaption As String
    
    searchCaption = "Check12"
    ''For Each ctrl In rpt.Controls
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" Then
            If ctrl.Controls(0).Caption = searchCaption Then
                ctrl = True
                Exit Sub
            End If
        End If
    Next
    NOTE I don't have the report so I used the form that the checkbox was on. You can adapt?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    You could even get away with
    Code:
    For Each ctl In Me.Controls
        
            On Error Resume Next
            
            If ctl.Caption = SearchCaption Then
                ctl.Parent.Value = True
            End If
    
        Next
    Ignore the errors and then use the parent property to set value
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Yikes. Maybe OK for this example, but not exactly best practice? If there is more going on in the procedure, you'd have to alter the On Error statement to suit. Not my style.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I changed the names to acctrl & acreport. Makes sense and less confusion.
    on the contrary acReport is a vba constant - it's value is 3. acForm is 2, acCheckBox is103, acLabel is 100, etc

  13. #13
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Quote Originally Posted by Micron View Post
    Yikes. Maybe OK for this example, but not exactly best practice? If there is more going on in the procedure, you'd have to alter the On Error statement to suit. Not my style.
    Yea I somewhat agree but it's debatable, not unheard of. It's kind of why I worded it as "you could get away with".
    The 2 possible errors, 2465 and 438, both relate to an object without caption properties and could be trapped within conventional error trapping. This doesn't appear to be a complicated procedure unless the
    label captions are dynamic or unknown and the scope of the procedure is pretty limited and unlikely to affect other code. Not sure why you'd be running it in a report though.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  14. #14
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    I change the code, unfortunately it won't work if the checkbox is Bound to a field:
    Code:
    Sub SearchControlCaption(reportName As String, searchCaption As String)
    Dim rpt As Report
    Dim ctrl As Control, p As Control
        
        Set rpt = Reports(reportName)
        
        For Each ctrl In rpt.Controls
            If TypeOf ctrl Is CheckBox Then
                Set p = ctrl.Controls(0)
                If p.Caption = searchCaption Then
                    ctrl.Value = True
                    Exit Sub
                End If
            End If
        Next ctrl
        
    End Sub

  15. #15
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    In a report you likely need to make changes in the underlying data. Reports are designed for display, not data entry.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 03-23-2023, 05:45 AM
  2. Replies: 6
    Last Post: 01-05-2023, 05:13 PM
  3. Replies: 5
    Last Post: 11-10-2022, 10:42 AM
  4. DLookup A Field in Table and set a value to true ?
    By d9pierce1 in forum Programming
    Replies: 9
    Last Post: 12-26-2021, 12:54 AM
  5. Replies: 8
    Last Post: 10-06-2014, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums