473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with MANY checkboxes

2
I have a program with tons of checkboxes (over 250). I also have 2 or 3 combo boxes next to the checkbox. How it works is when a user checks a checkbox, the combo boxes need to become visible. I wrote code for each and every checkbox, but it caused the program to run out of memory. To give you an idea, what I have written is...

Expand|Select|Wrap|Line Numbers
  1. Private Sub chk70_Click()
  2. If chk70.Value = True Then
  3.     cbxYN47.Visible = True
  4.     cbxB47.Visible = True
  5.     lbl35.Visible = True
  6.     lbl36.Visible = True
  7. Else
  8.     cbxYN47.Visible = False
  9.     cbxB47.Visible = False
  10. End If
  11. End Sub

My main question is can I have a routine that will be called when any checkbox is clicked, rather than having 250 subroutines?
Jan 10 '08 #1
7 1458
kadghar
1,295 Expert 1GB
I have a program with tons of checkboxes (over 250). I also have ...

My main question is can I have a routine that will be called when any checkbox is clicked, rather than having 250 subroutines?
It depends of the version of VB you're using, if it allows you to create control arrays, then you're done, since you can name the comboboxes with the same name, different index, and do the same for the checkboxes.. and then just write the code once with indexes.

If your version doesn't allow you to do that then i think you'll have to play a little bit with the controls and their names as strings. Its a little harder but can be done...

...so, what version are you using?
Jan 10 '08 #2
tdw4
2
My version is Visual Basic 6.3 for Excel 2003.
Jan 10 '08 #3
kadghar
1,295 Expert 1GB
My version is Visual Basic 6.3 for Excel 2003.
ok, so it's VBA?? i think we'll have to do it this way. I'll asume you have let the original names for the controls, i.e. CheckBox1, CheckBox2.... ComboBox1, ComboBox2... And they're related to each other the same way, i.e. CheckBox1 is for ComboBox1.
I'll also asume the Form is called UserForm1

Remember you can call any control in Userform1 with something like:
UserForm1.Controls("combobox1").visible = true

so now we will work with the names, to get the number of a control, the easiest way to me would be, knowing they all have the same kind of name (combobox2, checkbox35) to take the name as string and using MID take the characters from the 9th position and transform them into a integer with CINT.

This way if your active control is Checkbox26, the function:
cint(mid(me.activecontrol.name,9)) will return you 26 as an integer.

Try writing a little procedure like:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Something(ByVal i As Integer)
  2.     UserForm1.Controls("combobox" & i).Visible = UserForm1.Controls("checkbox" & i).Value
  3. End Sub
And adding this very same code to every checkbox click event:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBox1_Click() 'it's the same code for each chkbox
  2.     Call Something(CInt(Mid(Me.ActiveControl.Name, 9)))
  3. End Sub
(im not sure if there's a way to make it the default code for the checkboxes click event, or there's something like the HANDLES command, so useful in 2005, surely Killer would know better)

HTH
Jan 10 '08 #4
mafaisal
142 100+
Hello Kadghar

In VBA Control Array Not taken...?
If yes it is easy to use control Array

eg:-
use Checkbox & Combobox has control array
Private Sub Check1_Click(Index As Integer)
combobox1(index).visible=checkbox1(index).value
End sub




ok, so it's VBA?? i think we'll have to do it this way. I'll asume you have let the original names for the controls, i.e. CheckBox1, CheckBox2.... ComboBox1, ComboBox2... And they're related to each other the same way, i.e. CheckBox1 is for ComboBox1.
I'll also asume the Form is called UserForm1

Remember you can call any control in Userform1 with something like:
UserForm1.Controls("combobox1").visible = true

so now we will work with the names, to get the number of a control, the easiest way to me would be, knowing they all have the same kind of name (combobox2, checkbox35) to take the name as string and using MID take the characters from the 9th position and transform them into a integer with CINT.

This way if your active control is Checkbox26, the function:
cint(mid(me.activecontrol.name,9)) will return you 26 as an integer.

Try writing a little procedure like:
Expand|Select|Wrap|Line Numbers
  1. Public Sub Something(ByVal i As Integer)
  2.     UserForm1.Controls("combobox" & i).Visible = UserForm1.Controls("checkbox" & i).Value
  3. End Sub
And adding this very same code to every checkbox click event:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBox1_Click() 'it's the same code for each chkbox
  2.     Call Something(CInt(Mid(Me.ActiveControl.Name, 9)))
  3. End Sub
(im not sure if there's a way to make it the default code for the checkboxes click event, or there's something like the HANDLES command, so useful in 2005, surely Killer would know better)

HTH
Jan 10 '08 #5
kadghar
1,295 Expert 1GB
Hello Kadghar

In VBA Control Array Not taken...?
If yes it is easy to use control Array

eg:-
use Checkbox & Combobox has control array
Private Sub Check1_Click(Index As Integer)
combobox1(index).visible=checkbox1(index).value
End sub
noup, VBA and VB 2005 doesnt have control arrays. You know its sad but true (with guitar solo). Anyway, the me.controls(index) its not a "that hard" alternative. And i'm sure there're many other ways.
Jan 10 '08 #6
Killer42
8,435 Expert 8TB
noup, VBA and VB 2005 doesnt have control arrays. You know its sad but true (with guitar solo). Anyway, the me.controls(index) its not a "that hard" alternative. And i'm sure there're many other ways.
Don't forget, there are ways to simulate a control array. One is pretty much what you described here - that is, just using a number on the end of the control name as an "index".

Another possibility (don't know whether it has been tried in VBA) is what I proposed back in December 2006 in this thread. I don't know whether anyone has put it to practical use yet, but it should make coding simpler.

I guess it still hits the same problem, though - you need some way to connect it up to the actual events.

You know, it occurs to me that you might be able to work around the immediate problem (out of memory) by simply using shorter names, if it's the size of the source which is the problem. For example, "C70" rather than "Chk70". Along the same lines, you could...
  • Remove or reduce indenting
  • Remove unnecessary parts of the code.
    For instance, these two lines perform (I think) the exact same test...
    If Chk70.Value = True Then
    If Chk70 Then
Jan 11 '08 #7
Killer42
8,435 Expert 8TB
Another thought comes to mind (prompted in part by the mention of the Controls collection). But it depends on one important question. Do controls on userforms in Excel have a Tag property? I don't remember.

If so, you could use one big loop through the control on the userform, and put special values in the Tag property of each control to indicate how they link together. For instance, if a combobox has "ABC" in it's tag, then it is made visible when any checkbox with "ABC" in its tag is checked. And so on...
Jan 11 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: daniel | last post by:
Hi, This is more a javascript than a PHP problem, but anyway, if anyone can help.... I have a script that will dynamically create a series of checkboxes according to the number of values in...
1
by: Claire | last post by:
Hello, I am having a problem in my struts application with the checkboxes in my form. I have an array of checkboxes, some of which may be already selected when the form loads. My problem is when...
4
by: David Jubinville | last post by:
Hi All, I recently came across an annoying problem and would greatly appreciate any help someone/anyone could offer. Here we go: 1. We have a 'Checklist' consisting of checkboxes and relating...
3
by: Adam Toline | last post by:
In reference to the following: http://www.bellecose.com/form.htm At the top of each column there is a box for "All". When one is checked I need to check all of (and only) those boxes...
4
by: Alex Sibilev | last post by:
Hello, I have a really weird problem I've been trying to solve it without any luck for the last couple of hours :( I'm writing a "conference board" application (quite similar to ASP.NET...
2
by: JIM | last post by:
Hello, I would like to solve the following problem : I've a web site with ex. 5 checkboxes Each checkbox contains the name of a certain task ex. asp:Checkbox 1: Task 1 asp:Checkbox 2: ...
7
by: pavancognizant | last post by:
Hi, I am having a html table with lots of check box, and i am having a link in html on click of that i call a function and append all checkboxes with id value pair in href, not the problem is when...
2
by: dixonjm | last post by:
I have quite a complicated problem with the above. I will do my best to explain, although I have a word doc (too large to attach here 149KB) with the problem explained full with screen shots if any...
6
by: thanigaimani83 | last post by:
Hi guys. i can explain for my doubt with example.see.my table have multiple type of employee details like as Mangemment,Programmer,Designers,Accountants,ContractEmployees.Each type of department...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.