PDA

View Full Version : Link userform check boxes with corresponding text boxes



jdub12280
10-28-2009, 02:07 PM
Greetings,

I am in early stages of creating a user form to tally sales results. After reading a few threads here, one by fumei in particular, i've realized I need to break down all my If statements and turn in to a loop. The form contains 7 different frames with anywhere from 3 to 7 items including text and check boxes located next to each other. I have an "add items selected" and a "remove items selected" button on form.

I have so far, named all the check boxes and text boxes, in an easy to understand manner (chkExmpl1 / txtExmpl1, chkExmpl2 / txtExmpl2). I believe somehow the names of these objects needs to be variably linked, so we don't have to reference each one in code. I just don't quite grasp how to accomplish this yet.

I am attaching a sample workbook with the form embedded. I am showing all of my non brilliance, and have texted out an attempted loop statement at the bottom of my code.

Any and all help is greatly appreciated!

Thanks

Justin

jdub12280
10-29-2009, 08:08 AM
I might have picked a better title to this thread, the loop is where i need the help, and i assume the loop will involve linking the checkbox names with the textbox names. I stated the code out by building if statements for the add and remove buttons, but need those to be loops so i can modify the form easier in the future.

Thanks again,

Justin

lucas
10-29-2009, 10:37 AM
Are they only going to be either a 0 or a 1?

jdub12280
10-29-2009, 01:26 PM
Are they only going to be either a 0 or a 1?

No, the way i wrote the indiv if statements.. the as long as the check boxes are checked, each press of the Add button will add 1 to whatever the value is in the text box. On the remove button, i have it coded to remove 1 at a time as long as the check boxes are checked until it reaches zero. At that point, it just shows zero even if they continue to hit the remove button.

My problem isn't there ( i dont think), but i would like to use a loop for each button, rather than have an if statement for each chk /txt box combo.

At the bottom of my remove button code, i texted out an attempted loop statement (of what i perceived it would be like), but then i had a mental break down..and couldn't complete it.

Hope that helps clarify what I am trying to accomplish...thanks for response. I am open to suggestions as to a better / maybe more efficient way of doing this.

Justin

lucas
10-29-2009, 01:55 PM
Untested but it should head you in the right direction:
Dim ctl As Control
Dim objTextbox As TextBox
For Each ctl In fraUverseVoice.Controls
If TypeOf ctl Is CheckBox And ctl = True Then
For Each objTextbox In fraUverseVoice.Controls
If objTextbox.Name = Right(ctl.Name, Len(ctl.Name) - 3) Then
Val = objTextbox.Value
objTextbox.Value = Val - 1
End If
Next
Exit For
End If
Next