Excel VBA - Collection of checkboxes and labels on UserForm

Posted on 2016-11-07
Last Modified: 2016-11-16
I have a set of Checkboxes and related Labels on a UserForm.
A question on Collections
I have named them chk1AM, chk2AM etc. and (about to) name labels next to them as lbl1AM, lbl2AM etc.

I'm wondering if it would be a better strategy to use Collections, because I need to do the same for each pair and so (hopefully) only have to write a single block of code referenced by 1 to 7 (days of week) and chk or lbl.

I'm learning about collections from here

So what I'm looking for is some direction on strategy.

eg. do I continue to manually name the controls? or some way of looping through 1 to 7 to 'create' them?
How would I reference them to code against them?

Thanks for any advice.
Question by:hindersaliva
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 47

Expert Comment

by:Martin Liss
ID: 41877165
What are you planning on storing in the collection and how will the collection be used?

Author Comment

ID: 41877189
The checkboxes will be TRUE/FALSE to indicate the normal attendance days of an individual. They are FALSE by default. The labels next to each indicate a 'role' (text. Blank by default), which is selected from the combobox at the top.

So when the user turns on a checkbox the combobox a the top becomes visible. When a selection is made the selected item (text) is entered as text into the relevant label. That's the basic functionality I'm trying to achieve.
LVL 47

Expert Comment

by:Martin Liss
ID: 41877202
Can you attach your workbook?
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 41877332
Here it is, Martin. Thanks.

The form is populated from an ADO recordset. The database is updated from the checkboxes and labels when Save is clicked. I can do both those.
LVL 47

Accepted Solution

Martin Liss earned 500 total points
ID: 41877443
I don't see how you could use a collection, but try this modification of your workbook. When the userform shows up (by double-clicking column "O", check a few checkboxes and then click the "Save" button. The changes that I made in the code are marked with 'new.

Author Comment

ID: 41877485
I was hoping to refer to a control by a Name and a Number, such as chkBooking(4) and lblRole(4) to refer to a checkbox and label pair. But your code helps. I can refer to them by a Number (looping through all controls) as I have named them with a Number in its name.

LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 500 total points
ID: 41877505
You could name your controls chkBooking4 and lblRole4, etc.
LVL 47

Expert Comment

by:Martin Liss
ID: 41889963
I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question