Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-27-2021, 02:37 AM
JamesWood JamesWood is offline At least one checkbox ticked Windows 10 At least one checkbox ticked Office 2019
Advanced Beginner
At least one checkbox ticked
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Question At least one checkbox ticked

Hi guys




I'm trying to write a code that ensures at least one checkbox is ticked on a form. Any idea why it's not working?


Code:
Dim oCtr As Control
For Each oCtr In Me.Controls
  If TypeName(oCtr) = "CheckBox" And oCtr.Value = True Then
    'CODE HERE
  Else
    MsgBox "You must tick at least one."
    Exit Sub
  End If
Next



Thanks a lot!
James

Last edited by macropod; 04-27-2021 at 03:27 AM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 04-27-2021, 04:12 AM
gmayor's Avatar
gmayor gmayor is offline At least one checkbox ticked Windows 10 At least one checkbox ticked Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You could use:
Code:
Private Sub CommandButton1_Click()
Dim oCtr As Control
Dim bChecked As Boolean
    For Each oCtr In Me.Controls
        If TypeName(oCtr) = "CheckBox" And oCtr.value = True Then
            bChecked = True
            Exit For
        End If
    Next
    If Not bChecked = True Then
        MsgBox "You must tick at least one.", vbCritical
        GoTo lbl_Exit
    End If
    'a checkbox is checked so do stuff
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-27-2021, 04:31 AM
JamesWood JamesWood is offline At least one checkbox ticked Windows 10 At least one checkbox ticked Office 2019
Advanced Beginner
At least one checkbox ticked
 
Join Date: Nov 2020
Posts: 37
JamesWood is on a distinguished road
Talking

Thanks so much, I'll try this out! Appreciate it as always
Reply With Quote
Reply

Tags
checkbox

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkbox - toggle hide/unhide other checkbox - XMLMapping GErl Word VBA 9 11-19-2020 03:15 PM
Table cell auto-shading when checkbox ticked. markt Word VBA 2 08-05-2020 06:13 AM
Table cell auto-shading when checkbox ticked. markt Word 2 07-10-2020 08:53 AM
At least one checkbox ticked One Content Control Checkbox checks another Content Control Checkbox DEsh Word VBA 2 10-06-2017 08:23 PM
At least one checkbox ticked Show Userform when Content Control CheckBox ticked derajlance Word VBA 1 05-13-2016 01:55 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:59 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft