Log in

View Full Version : Solved: Creating an array of textboxes



ddalenberg
08-13-2010, 06:17 AM
I apologize if this a topic that has already been addressed, I have been looking for solution for a couple days and haven't found anything.

I am creating a document that has one combobox and several textboxes that are controlled by the selection a user makes in said combobox.

The user will choose between 2 part numbers, and then using textboxes that I have inserted manually, the chosen part number wil be displayed on each page. Example of what I have so far:

Private Sub Document1_Open()
ComboBox1.List = Array("Choose a Part #", _
"Part #1", _
"Part #2")
ComboBox1.ListIndex = 0
End Sub

Private Sub ComboBox1_Change()

' TextBox 1 Page 1
If ComboBox1 = "Choose a Part #" Then TextBox1 = Blank
If ComboBox1 = "Part #1" Then TextBox1 = "Part #1"
If ComboBox1 = "Part #2" Then TextBox1 = "Part #2"

End Sub

This works well, and does what I want it to do, but I have a 26 page document, and will have 26 textboxes in which the part number must be displayed.

I am very new to VBA and have no idea how to clean this up. Rather than write the ComboBox1_Change() code 26 times, I would like to create some sort of array for the textboxes, and then run a For loop of sorts for my If conditional statement, to display my part number.

Any idea how to make this happen?
Thanks!

gmaxey
08-13-2010, 11:14 AM
Before going the array route can I ask why you need 26 text boxes? It the user supposed to be able to enter or revise the text in the boxes? If you just want the value of the combobox dispalyed on each page you could but a DocVariable {DocVariable "PN" } field in the header (or on each page) and use code something like this:

Private Sub Document1_Open()
ComboBox1.List = Array("Choose a Part #", _
"Part #1", _
"Part #2")
ComboBox1.ListIndex = 0
End Sub
Private Sub ComboBox1_Change()
Select Case ComboBox1.Value
Case "Choose a Part #"
ActiveDocument.Variables("PN").Value = ""
Case Else
ActiveDocument.Variables("PN").Value = ComboBox1.Value
End Select
ActiveDocument.Fields.Update
End Sub

ddalenberg
08-13-2010, 11:30 AM
I didn't setup it like that because, until 5 minutes ago, I had no idea what a DocVariable was.

Thanks, I tried it out in a test file and it works perfectly.