1

I hope someone can help, I am new to programming, my problem is this:

40 checkboxes on a form which one by one, needs to be checked for boolean "True" or "False" (checked or unchecked by user), so that I can run individual code for each "True" case.

I am trying to include a counter "i" in the "MS Access checkbox reference" which I use for the variable, to give me the value of the given checkbox. The following code should show what I try to accomplish, can anybody point me in the right direction, without using a very advanced solution? I presume it is because it cannot execute all of this in a single step or because it only sees my input for the variable as a string and not a command to execute :

Do While Flag = True

Dim CheckboxVar As Boolean
i = i + 1

If i = 40 Then
    Flag = False
End If

CheckboxVar = "Me.Checkbox" & i & ".Value"
AddWhereSQL = SQLBuilder (CheckboxVar)
Loop

(Retrieve the value of Checkbox1 and send it to SQLBuilder, retrieve Checkbox2 and send it to SQLBuilder, and so on)

1
  • Thank you all for your suggestions, for my case and persona, Kostas K's solution works best (It is implemented and running). I use the name of the control instead of tag, and I run individual/unique code depending on which checkbox is True. This site is a wonderful place with wonderful people, thank you very much. Michael Commented Nov 3, 2017 at 12:20

3 Answers 3

5

Loop through your checkboxes like this:

Sub Test()
    Dim ctrl As Control

    For Each ctrl In Me.Controls
        If TypeOf ctrl Is CheckBox Then
            If ctrl.Value = True Then
                'do something
            End If
        End If
    Next ctrl
End Sub

Naming your checkboxes "Checkbox1", "Checkbox2", etc. is tiresome and not the best naming practice. The code above will find every checkbox on the form but you could easily restrict it to all checkboxes with a specific tag, for example.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that you may have to account for checkboxes that shouldn't be in the loop. You can use tags for that (e.g. set a tag for all checkboxes that should, and check If ctrl.Tag = "something" Then)
Your code applies to Userforms, but not to Access forms (they are not MSForms). -- With 40 checkboxes on a form, chances are that the only sensible way to name them is Checkbox1, Checkbox2, ...
@ErikvonAsmuth, tagging works -- for the same reason I suggested putting them all in a frame and looping through UserForm1.Frame1.Controls, for example.
@CallumDA Yes, but like Andre said, it's an Access question and not a userform, so frames are unavailable. Tags are the usual way for Access. Your further code will work fine after minor changes (remove MSForms., change UserForm1 to Me)
Adjusted for use in MS Access, @Andre it should be fine now, I agree with CallumDA that Checkbox1, Checkbox2 etc. should be avoided.
|
2

To loop through the controls, you can do this:

Dim i As Long
For i = 1 To 40
    If Me.Controls("CheckBoxControlName" & i).Value = -1 Then
        'The control value is True
    End If
Next i

If you code is placed in a Standard Module, change Me to Forms!YourFormName.

5 Comments

Small note: use an integer instead of a long to save memory and execution time (the max value is 40)
@Erik von Asmuth VBA internally converts all Integers to Long therefore there's no performance advantage.
@ErikvonAsmuth. I learnt something new today as well (thanks Kostas K.!) stackoverflow.com/questions/26409117/…
@KostasK. Thanks for the heads up, sorry for the incorrect criticism. Guess I can go optimize literally all code that uses integers I ever wrote.
@Erik von Asmuth Ah, no bother man. I had to do the exact same thing when I found out. :)
0

I think you need a "for" loop. Maybe something like:

CheckboxVar = " WHERE "
For i = 1 to 40
    CheckboxVar = Me.Controls("Checkbox" & i).Value = True & " AND " & checkboxVar
next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.