PDA

View Full Version : Help with adding checkbox captions to an array



mascot
10-27-2011, 03:56 PM
I am working on a form that has checkboxes with grocery list items for the captions. I would like to be able to create a grocery list from each item I check on the form using these captions and then put this on a word document through a bookmark. I am a newbie. I'm trying to search around to find information. I've been able to piece some stuff together but I keep getting compiling errors. Here is the code I have for iterating through each checkbox control to see if it is checked and if it is True then add it to an array. It doesn't work so I was hoping someone with some experience could help. Maybe I'm going about this all wrong. I'm not sure. Thanks

Dim x as Integer
Dim ctrl as Control
Dim myArray() as String
x=1
For Each ctrl In Me.Controls
If TypeName(ctrl) = "TextBox" and Value = True Then
x=x+1
ReDim Preserve myArray (1 to x)
myArray = Me.Ctrl.Caption

End If
Next

gmaxey
10-27-2011, 04:24 PM
If you are working with checkboxes then the TypeName would need to match CheckBox" no?

Private Sub CommandButton1_Click()
Dim x As Integer
Dim ctrl As Control
Dim myArray() As String
x = 1
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl.Value = True Then
x = x + 1
ReDim Preserve myArray(x)
myArray(x) = ctrl.Caption
End If
End If
Next
End Sub

mascot
10-31-2011, 06:25 AM
Sorry, I mistyped. "Checkbox" is in my code. I get a message saying "compile error. Method or data member not found." It then goes on and highlights .ctrl in the line myArray=Me.ctrl.Caption. Any thoughts on this error? Thanks

Frosty
10-31-2011, 10:06 AM
Are you using Greg's code?

Yes, your code will fail on that line (and another, I think), because you're trying to set a string value (the .Caption property) to the entire array (myArray) instead of a particular element of the array (ex: myArray(x)).

Always make sure to read the help file on the stuff you're trying to do. Your redim preserve line of code is not correct either... however, Greg's code looks like it would work to me.