Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple checkbox operation...

Posted on 2014-03-21
7
Medium Priority
?
274 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 34

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39946339
try using

me.activecontrol.name
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 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 40

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 34

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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

877 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