locked
Checkboxes, strings and arrays RRS feed

  • Question

  • Hi,

    I'm stuck.  I have three groups of varying numbers of checkboxes on a form where the user can select 5 out of each group.  Each checkbox represents a string value and I plan to pass the string variable corresponding to the user's selection into a fixed size array for use later on in my code.  As one checkbox is ticked, the corresponding variable should be passed to the first empty index in the array. As a checkbox is in-ticked, the associated array index is to be cleared and the user's next selection would fill that gap.

    Not a clue how to go about this though! 

    Any suggestions would be massively appreciated.


    Wednesday, June 14, 2017 8:14 AM

All replies

  • Can you describe the purpose of this application.  Maybe it will help me understand your description.
    Wednesday, June 14, 2017 1:59 PM
  • I'm a design engineer using SolidWorks 3D design software and VBA to write a macro for other engineers use.  On our manufacturing drawings we want to include symbols relating to specific PPE, Tools, and working practices (Manpower) that the technicians will need to adhere to in order to build the products efficiently and safely. I have created a custom font file where certain characters correspond to the symbols representing the PPE, Tools and Manpower requirements, i.e. "0" = image of safety glasses, "A" = a drill, "a" = Two men to represent "Work in pairs" etc.

    I created a userform with checkboxes labelled to match the symbols they relate to.  I have a fixed size table template on the drawing limiting the users selection of appropriate symbols to 5 from each group. 

    As the user ticks the checkbox I want the corresponding symbol code to be stored in an array to be passed to the tables cells once "OK" is clicked.  If the user changes their mind about what symbol they need and untick a checkbox, I want that slot in the array to be freed up and the user's next selection to take its place. If they pick less than the 5 from each group, and/or deselect one so there is a gap in the array, I want null values ignored.

    I have already got the code working to limit the number of selections they can make.  I have written the code for the arrays and passing the variables to the table, and when I manually add variables to the array the correct symbols appear.   I just don't know how to populate the array in such a dynamic way that the checkbox variable goes into the first available index.

    Thursday, June 15, 2017 7:06 AM
  • I can maybe populate a dynamic array and loop through for all controls on teh form but how do I restrict the loop to the GroupName of the checkbox control?
    Thursday, June 15, 2017 7:24 AM
  • I'm not sure I fully understand but in the checkbox you can set a property called 'Tag'.  I often use this to set parameters for functions that use the checkbox.  You could also encode the group in the checkbox name.  Example:

    Public cA() As Control
    
    Sub Test()
      Call GetCheckBoxControls
        For k = 0 To UBound(cA)
          If cA(k).value Then
            name = cA(k).name
            tag = cA(k).Tag
            ...do stuff
          End If
        Next k
    End Sub
    
    
    Sub GetCheckBoxControls()
      Dim ctl As Control
      Dim first As Boolean
      
      first = True
      ReDim cA(0)
      For Each ctl In MainFM.MainMP.Pages(0).Controls 'where checkboxes are located.
        If TypeName(ctl) = "CheckBox" Then
          If first Then
            Set cA(UBound(cA)) = ctl
            first = False
          Else
            ReDim Preserve cA(UBound(cA) + 1)
            Set cA(UBound(cA)) = ctl
          End If
        End If
      Next ctl
    End Sub
    


    • Edited by mogulman52 Thursday, June 15, 2017 2:31 PM
    Thursday, June 15, 2017 2:29 PM