Solved

Working With Checkbox in VBA Excel

Posted on 2011-03-06
5
1,013 Views
Last Modified: 2012-05-11
I have four checkboxes in my VBA excel application which are as follows

ShowStopper
High
Medium
Low

I will be using this checkboxes to form an query to a database,

If the user selects all the checboxes, then the query that i am looking for should be like,

" ' 1 - Show Stopper' Or '2 - High' Or '3 - Medium' Or '4 - Low' "

How can i form this query line through the selection of  checkboxes

Please help with the coding, if you want i will change the caption of checkbox as 1 - Show Stopper' and as follows to form an query at last try only.


0
Comment
Question by:srikanthv2322
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 35047802
Hi there,

What kind of checkboxes are they?  Please be a bit more specific, as there are many types.  Is it a forms control?  An ActiveX control?  A userform control?  Tell us how you created it, and we'll know what kind it is.  Coding depends on the type.  And if you can tell us where/how you want to fire this, that would help.
0
 

Author Comment

by:srikanthv2322
ID: 35048033
Its a Form Control and i just created from selecting from Toolbox in VB
0
 
LVL 14

Accepted Solution

by:
Zack Barresse earned 500 total points
ID: 35048092
You mean in the VBE?  Those are not forms controls, but ActiveX controls.  Or do you mean a forms control from the Excel environment, and wanting to use VBA on it?  What is your version of Excel?  Assuming you're using a userform from the VBE, you could use something like this...

    Dim sVar As String
    '" ' 1 - Show Stopper' Or '2 - High' Or '3 - Medium' Or '4 - Low' "
    If Me.CheckBox1.Value = True Then
        sVar = "'1 - Show Stoppper'"
    End If
    If Me.CheckBox2.Value = True Then
        sVar = IIf(Len(sVar) > 0, sVar & " Or '2 - High'", sVar)
    End If
    If Me.CheckBox3.Value = True Then
        sVar = IIf(Len(sVar) > 0, sVar & " Or '3 - Medium'", sVar)
    End If
    If Me.CheckBox4.Value = True Then
        sVar = IIf(Len(sVar) > 0, sVar & " Or '4 - Low'", sVar)
    End If
    MsgBox sVar

Open in new window


HTH
0
 

Author Comment

by:srikanthv2322
ID: 35049393
Small correction in the above code, from second Checkbox have the Caption intended in false statement of IIf and not sVar name.

0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 35049432
You mean you want to have the check be opposite only for checkbox2?  Not sure I follow, but if that is correct, then...

    Dim sVar As String
    '" ' 1 - Show Stopper' Or '2 - High' Or '3 - Medium' Or '4 - Low' "
    If Me.CheckBox1.Value = True Then
        sVar = "'1 - Show Stoppper'"
    End If
    If Me.CheckBox2.Value = False Then
        sVar = IIf(Len(sVar) > 0, sVar & " Or '2 - High'", sVar)
    End If
    If Me.CheckBox3.Value = True Then
        sVar = IIf(Len(sVar) > 0, sVar & " Or '3 - Medium'", sVar)
    End If
    If Me.CheckBox4.Value = True Then
        sVar = IIf(Len(sVar) > 0, sVar & " Or '4 - Low'", sVar)
    End If
    MsgBox sVar

Open in new window


If that's not what you meant, can you clarify please?

Zack
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 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