Solved

Working With Checkbox in VBA Excel

Posted on 2011-03-06
5
1,009 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 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

8 Experts available now in Live!

Get 1:1 Help Now