Solved

Working With Checkbox in VBA Excel

Posted on 2011-03-06
5
1,020 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

724 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