I'm putting together a clientban moderation option, and I have checkboxes representing each client that can be banned.
Dim tmpString As String
If chkSEXP.value = vbChecked Then
tmpString = tmpString & "SEXP:"
End If
If chkSTAR.value = vbChecked Then
tmpString = tmpString & "STAR:"
End If
If chkD2DV.value = vbChecked Then
tmpString = tmpString & "D2DV:"
End If
If chkD2XP.value = vbChecked Then
tmpString = tmpString & "D2XP:"
End If
If chkW2BN.value = vbChecked Then
tmpString = tmpString & "W2BN:"
End If
If chkWAR3.value = vbChecked Then
tmpString = tmpString & "WAR3:"
End If
If chkW3XP.value = vbChecked Then
tmpString = tmpString & "W3XP:"
End If
tmpClients() = Split(tmpString, ":")
modOpts.cbClientList = Replace(tmpString, ":", "")
Here, I develope a string that contains all of the clients that the user wishes to ban. I then split up this string to an array by delimeter ":", and add them to the array tmpClients().
Now, at the form's load event I want to check each box that applies to a desired client to ban, but I do not want to set up several unnecessary control statements to do check each box that applies. So, I set up a simple For statement to cycle through the elements:
(On form load)
Dim i As Byte
For i = 0 To UBound(tmpClients) - 1
chk(tmpClients(i)).value = vbChecked
Next i
When I compile and test however, I get a "Sub or function not defined error". Obviously this is because it's trying to read chk as a function/sub named "chk()". Yet, I don't know any other way of appending the client ID to the end of "chk" so that it matches each CheckBox. (BTW, my checkboxes are named as chkSEXP, chkSTAR, etc.)
Any ideas?
[BUMP] This is urgent, someone please respond.
Did you think of maybe creating an array of CheckBoxes?
Quote from: MyndFyre on November 02, 2004, 05:53 PM
Did you think of maybe creating an array of CheckBoxes?
He probably doesn't know how to do that. Is there a way to iterate over all of the controls on a form without having them in arrays?
Quote from: Adron on November 02, 2004, 07:20 PM
Quote from: MyndFyre on November 02, 2004, 05:53 PM
Did you think of maybe creating an array of CheckBoxes?
He probably doesn't know how to do that. Is there a way to iterate over all of the controls on a form without having them in arrays?
Yes, my example: Add checkboxes to a form, name form to form1, slap on a command button:
Private Sub Command1_Click()
Dim Apple
For Each Apple In Form1
If (Apple.Value = vbChecked) Then
Debug.Print Apple.Name & " is checked"
End If
Next Apple
End Sub
So could you then adapt your code to:
Private Sub Command1_Click()
Dim Apple
For Each Apple In Form1
If Apple.Name = "chk" & GameID Then
' ... do something
End If
Next Apple
End Sub
?
Quote from: MyndFyre on November 02, 2004, 08:04 PM
So could you then adapt your code to:
Private Sub Command1_Click()
Dim Apple
For Each Apple In Form1
If Apple.Name = "chk" & GameID Then
' ... do something
End If
Next Apple
End Sub
?
It should
Quote from: Adron on November 02, 2004, 07:20 PM
Quote from: MyndFyre on November 02, 2004, 05:53 PM
Did you think of maybe creating an array of CheckBoxes?
He probably doesn't know how to do that. Is there a way to iterate over all of the controls on a form without having them in arrays?
Yes Adron, I do. Anyway, for some reason I wasn't thinking that you could treat control names as strings, such as "chk" & tmpClients(i).value = vbChecked. If you can, I guess I'm horribly mistaken and stupid. :P
If iterating over all controls in a Form, be sure to check the control type before checking properties. Value might not be present in all of the controls, causing an error that property is not supported by object. I think "TypeName()" will return the name of the control type, i.e. CheckBox.
I dropped a control of each basic type on a form and ran this code:
Dim C As Control
Dim M As String
For Each C In Form1.Controls
M = M & vbCrLf & TypeName(C)
Next
ClipBoard.SetText M, vbCFText
Producing this output:
CommandButton
ListBox
ComboBox
OptionButton
CheckBox
CommandButton
Frame
TextBox
Label
Why is that needed when I know the type of property I'm dealing with? Anyway, that's not my problem. Is it possible to refer to a property's name through a string? For instance, I have 6 CheckBoxes named chk1, chk2, chk3, chk4, chk5, and chk6. I have an array consisting of (1,3,4,5). Can I create a For loop to select the checkboxes like so?
Dim i as byte
For i = 0 to Ubound(Array)
"chk" & Array(i) & ".value" = vbChecked
next i
Would this work? Is there a variation that will make it work? Is it all possible?
Quote from: LivedKrad on November 03, 2004, 12:09 PM
Would this work? Is there a variation that will make it work? Is it all possible?
The obvious way is to make them an array. Other than that, yes, it's possible to change the syntax a bit and make it work.
If I didn't (for some stupid reason) want to choose the obvious way, how would I go about using my idea yet actually making it work?
Quote from: LivedKrad on November 03, 2004, 12:15 PM
If I didn't (for some stupid reason) want to choose the obvious way, how would I go about using my idea yet actually making it work?
Something like this works (not to say it's a good solution):
Private Function ctrl(ByVal name As String) As Control
Dim C As Control
For Each C In Me.Controls
If C.name = name Then Set ctrl = C: Exit Function
Next
End Function
Private Sub Command1_Click()
Dim i As Byte
Dim a(0 To 1) As Integer
a(0) = 1
a(1) = 3
For i = 0 To UBound(a)
ctrl("chk" & a(i)).Value = vbChecked
Next i
End Sub