PDA

View Full Version : Is it Possible to Group Items on a Form?



Zephid15
05-01-2007, 06:27 AM
I want to group a few text boxes and some check boxes together to be, say, group1. I don?t know if this is possible but if it is it would drastically reduce the length of my code.

lucas
05-01-2007, 09:21 AM
On a userform?
Put a frame on the form and draw your objects on the frame.

fumei
05-01-2007, 10:30 AM
More correctly it is draw your controls IN the frame...just teasing Steve.

Zephid15, could you describe in more detail how grouping will "drastically reduce the length" of the code? It certainly could, but I am curious as to what you are doing.

lucas
05-01-2007, 10:51 AM
Just checking to be sure your watching and keeping me on track Gerry..:devil2: Don't hesitate because I sometimes step in it and need bailing out..

Zephid15
05-02-2007, 06:07 AM
fumei, it will cut down on code by allowing me to enable/disable checkboxes and textboxes. Say I have 10 checkboxes in a Colum. And I want to write code that when any checkbox is enabled or disabled something triggers. I would have

Checkbox1 code code code
Checkbox2 code code code
Checkbox3 code code code
Checkbox4 code code code
Checkbox5 code code code
Checkbox6 code code code
Checkbox7 code code code
Checkbox8 code code code
Checkbox9 code code code
Checkbox10 code code code


But if I could group checkboxes 1-10 in to say group checkboxgroup then the code would be condensed to

Checkboxgroup code code code

Keep in mind that I still want the checkboxes to trigger individual events when activated.

Zephid15
05-02-2007, 07:49 AM
here is a better and real life example:


Sub Checkbox1b_change()
If CheckBox1b.Value = False Then
txt1q.Enabled = False
txt1a.Enabled = False
txt1b.Enabled = False
txt1c.Enabled = False
txt1d.Enabled = False
txt1dis.Enabled = False
txt1e.Enabled = False
Else
If CheckBox1b.Value = True Then
txt1q.Enabled = True
txt1a.Enabled = True
txt1b.Enabled = True
txt1c.Enabled = True
txt1d.Enabled = True
txt1dis.Enabled = True
txt1e.Enabled = True
End If
End If



Change to something more condenced:


Sub Checkbox1b_change()
If CheckBox1b.Value = False Then
txtgroup.Enabled = False
Else
If CheckBox1b.Value = True Then
txtgroup.Enabled = True

fumei
05-03-2007, 04:32 AM
First, um, you need a few more coding basics.
Sub Checkbox1b_change()
If CheckBox1b.Value = False Then
txt1q.Enabled = False
' etc. etc.
Else
If CheckBox1b.Value = True Then
What is wrong with this?

The If CheckBox1b.Value = True line.

This is completly NOT needed.If CheckBox1b.Value = False
' yadda
Else ' this MEANS CheckBox1b.Value = TrueNo need to do another extraneous If statement.

If it is NOT False, then it is True. Technically speaking, it is possible that you could have it set up to have a Null (although I doubt it), but in any case it may be even better to use a Select Case. That way you check against the actual value.

Select Case CheckBox1b.Value
Case True
' do this
' groupBlah.enabled = False
Case False
' do that
End SelectSelect Case can be your friend.

Next, you can loop through a set of controls. Here is, in fact, code that will specifically enable/disable the controls you name - depending on the checkbox value.Sub CheckBox1_Change()
Dim txtBoxNames()
Dim var
txtBoxNames = Array("q", "a", "b", "c", _
"d", "dis", "e")
Select Case CheckBox1.Value
Case True
For var = 0 To UBound(txtBoxNames)
Me.Controls("txt" & txtBoxNames(var)).Enabled _
= False
Next
Case False
For var = 0 To UBound(txtBoxNames)
Me.Controls("txt" & txtBoxNames(var)).Enabled _
= True
Next
End Select
End SubThe textboxes are not grouped - except as part of the array. Of course textboxes actually named txtq, txta, txtc etc are VERY badly named, so I assume they are not actually named that. The point is that you CAN group by names, using an array.

This is useful if you want to group SOME textboxes, but not others. You simply make the array of the names you want to action. An alternative (if you are actioning ALL textboxes) is to make a Control object and loop through the controls on the userform, checking if each is a textbox, and if it is - then action it. Like this:
Dim oCtl As Control
If CheckBox1.Value = False Then
For Each oCtl In Me.Controls
If TypeOf oCtl Is MSForms.TextBox Then
Me.Controls(oCtl.Name).Enabled = False
End If
Next
Else ' again, this means it is true!!
For Each oCtl In Me.Controls
If TypeOf oCtl Is MSForms.TextBox Then
Me.Controls(oCtl.Name).Enabled = True
End If
Next
End If

Zephid15
05-04-2007, 05:55 AM
I did not even notice that If/Then error. I will see if that array will help me out.

Thanks for the help.