Solved

Multiple checkbox operation...

Posted on 2014-03-21
7
260 Views
Last Modified: 2014-03-21
There are chkbox1, chkbox2, chkbox3, etc. doing some operation shown down below.

Question : Is there a way to simplify this code using something like:

= fnUpdateChkeBoxes("chkbox1")
= fnUpdateChkeBoxes("chkbox2")
= fnUpdateChkeBoxes("chkbox3")

That goes on the click events of the respective check boxes to call:

Function fnUpdateChkeBoxes(strChek As String)

       val_chkbox1 = chkbox1      
       val_chkbox2 = chkbox2
       val_chkbox3 = chkbox3

End Function

but ignoring check box in the parameter strChek?

Thank you.

'Simplified version:  
Sub chkbox1_Click()

    If chkbox1 = True Then
       ' val_chkbox1 = chkbox1       '<-- this one ignore
       val_chkbox2 = chkbox2
       val_chkbox3 = chkbox3
      .
      .
  End if

End Sub

Sub chkbox2_Click()

    If chkbox1 = True Then
       'val_chkbox1 = chkbox1       
       'val_chkbox2 = chkbox2    '<-- this one ignore
       val_chkbox3 = chkbox3
      .
      .
  End if

End Sub

Sub chkbox3_Click()

    If chkbox3 = True Then
       'val_chkbox1 = chkbox1       
       val_chkbox2 = chkbox2   
       'val_chkbox3 = chkbox3   '<-- this one ignore
      .
      .
  End if

End Sub

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39946302
call a sub with a param (1,2 or 3) depending on which box is checked.
e.g.

Sub chkbox1_Click()
   If chkbox1 = True Then testSub  1


sub testSub (chk as long)
if chk = 1 then
      val_chkbox2 = chkbox2
       val_chkbox3 = chkbox3
end if
if chk = 2 then   val_chkbox3 = chkbox3
if chk = 3 then   val_chkbox2 = chkbox2
end sub
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 39946320
re> call a sub with a param (1,2 or 3) depending on which box is checked

There are no 1, 2, 3s to call. I have to have full name of the check box. Checkbox1, 2, 3 are used just for illustration.

Also, if I have 30 check boxes, then I have to have 30 if statements. I want to simplify if possible.

Thanks,

Mike
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39946339
try using

me.activecontrol.name
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39946342
if you want to process an event (chkbox click) you will need to call the function from the event.

e.g.
Sub chkbox1_Click()
   If chkbox1 = True Then testSub  1
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 39946356
try using

me.activecontrol.name  

or

screen.activecontrol.name

or just
screen.activecontrol

depending on how you call it in the sub/function
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39946523
I wonder why you have 30 checkboxes.  Perhaps you should consider normalizing the schema.

Having a single sub to process 30 checkboxes isn't simplification.  You would need a huge case statement to sort out the source of the click and do what ever is required for that particular checkbox.

If you're going to stick with the 30 options, perhaps a multi-select list box would be simpler to program.  There the user could simulate clicks by selecting various rows.  Your code could loop through the selections and process them but again, if you are doing something different for each checkbox, you should have a separate procedure.  

The Switchboard form (circa 2003) does something like this but in that case they have 8 buttons any one of which could perform about 10 functions with various arguments.   The way the switchboard form works, you don't care what button was pressed.  You only care which of the 10 options that button is supposed to perform.  Your description sounds like you have 30 buttons doing 30 things.  There is no logical way to group them.  It would be simpler to just let them do their own thing.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 39946559
Rey Obrero,

I like me.activecontrol.name, etc. solution.
Function fnUpdateChkeBoxes(strChek As String)

       If me.activecontrol.name <> "chkbox1" Then val_chkbox1 = chkbox1      
       If me.activecontrol.name <> "chkbox2" Then val_chkbox2 = chkbox2
       If me.activecontrol.name <> "chkbox3" Then val_chkbox3 = chkbox3

End Function

Open in new window


I tried and it works,


PatHartman,
I am very picky in making sure tables are normalized. However, these check boxes are used for conditional formatting purpose etc. and also I have limited freedom to do what I want in this project.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now