Combo Box List Problems

P

Pam

I developed a user form in Word using a list box, which
populated great from a 3 column table. Code follows.

Now boss wants a combo box not a list box, and I can't get
the 3 columns to load. I want the first column to be the
options to choose from in the combo box (list of names)
and then populate two text boxes (textbox1 and textbox2)
with the information from columns 2 and 3 going with the
name selected from column one. I just can't make it work,
can anyone help.

Here is the listbox code that does work:

Private Sub CommandButton2_Click()
Dim Msg, Style 'set dim for message box when Exit is
pressed

Msg = "Please wait while program shuts down" ' define
message
Style = vbYesOk + vbCritical + vbDefaultButton2 '
define message response buttons
Response = MsgBox(Msg, Style) ' Display message

If True Then Unload UserForm1
Application.DisplayAlerts = False

Word.Application.Quit

End Sub

Private Sub UserForm_Initialize()


Dim MyArray() As String
RowCount = ActiveDocument.Tables(1).Rows.Count
RowCount = ActiveDocument.Tables(1).Rows.Count
ColCount = ActiveDocument.Tables(1).Columns.Count
ReDim MyArray(RowCount - 1, ColCount - 1)
For i = 1 To RowCount
For j = 1 To ColCount
'Select each cell in the table
Celldata = ActiveDocument.Tables(1).Cell(i, j)
'Remove the paragraph and end-of-cell markers
'as we lod the array

MyArray(i - 1, j - 1) = Left(Celldata, Len
(Celldata) - 2)
Next
Next
ListBox1.ColumnCount = ColCount
ListBox1.List() = MyArray


Dim MyArrayExternal() As String
RowCount = ActiveDocument.Tables(2).Rows.Count
RowCount = ActiveDocument.Tables(2).Rows.Count
ColCount = ActiveDocument.Tables(2).Columns.Count
ReDim MyArrayExternal(RowCount - 1, ColCount - 1)
For m = 1 To RowCount
For n = 1 To ColCount
'Select each cell in the table
Celldata = ActiveDocument.Tables(2).Cell(m, n)
'Remove the paragraph and end-of-cell markers
'as we load the array
MyArrayExternal(m - 1, n - 1) = Left(Celldata,
Len(Celldata) - 2)
Next
Next
ListBox2.ColumnCount = ColCount
ListBox2.List() = MyArrayExternal

End Sub

Private Sub ListBox1_Click()

For x = 0 To ListBox1.ListCount

If ListBox1.Selected(x) = True Then
MsgBox "Name:" & " " &
ListBox1.List(x) & (Chr(13)) & "Office Symbol:" &
ListSeparator & " " & ListBox1.List(x, 1) & (Chr
(13)) & "Project(s):" & ListSeparator & " " &
ListBox1.List(x, 2)
End If
Next x
End Sub

Private Sub ListBox2_Click()

For x = 0 To ListBox2.ListCount

If ListBox2.Selected(x) = True Then
MsgBox "Name:" & " " &
ListBox2.List(x) & (Chr(13)) & "Office Symbol:" &
ListSeparator & " " & ListBox2.List(x, 1) & (Chr
(13)) & "Project(s):" & ListSeparator & " " &
ListBox2.List(x, 2)
End If
Next x
End Sub


I've tried replacing ListBox1.Selected(x) with a
MatchFound for the ComboBox, but it wouldn't work. Where
am I missing the boat.
 
W

Word Heretic

G'day "Pam" <[email protected]>,

when the combo _Change event fires, examine the list value and
populate the two list boxes accordingly.


Pam said:
I developed a user form in Word using a list box, which
populated great from a 3 column table. Code follows.

Now boss wants a combo box not a list box, and I can't get
the 3 columns to load. I want the first column to be the
options to choose from in the combo box (list of names)
and then populate two text boxes (textbox1 and textbox2)
with the information from columns 2 and 3 going with the
name selected from column one. I just can't make it work,
can anyone help.

Here is the listbox code that does work:

Private Sub CommandButton2_Click()
Dim Msg, Style 'set dim for message box when Exit is
pressed

Msg = "Please wait while program shuts down" ' define
message
Style = vbYesOk + vbCritical + vbDefaultButton2 '
define message response buttons
Response = MsgBox(Msg, Style) ' Display message

If True Then Unload UserForm1
Application.DisplayAlerts = False

Word.Application.Quit

End Sub

Private Sub UserForm_Initialize()


Dim MyArray() As String
RowCount = ActiveDocument.Tables(1).Rows.Count
RowCount = ActiveDocument.Tables(1).Rows.Count
ColCount = ActiveDocument.Tables(1).Columns.Count
ReDim MyArray(RowCount - 1, ColCount - 1)
For i = 1 To RowCount
For j = 1 To ColCount
'Select each cell in the table
Celldata = ActiveDocument.Tables(1).Cell(i, j)
'Remove the paragraph and end-of-cell markers
'as we lod the array

MyArray(i - 1, j - 1) = Left(Celldata, Len
(Celldata) - 2)
Next
Next
ListBox1.ColumnCount = ColCount
ListBox1.List() = MyArray


Dim MyArrayExternal() As String
RowCount = ActiveDocument.Tables(2).Rows.Count
RowCount = ActiveDocument.Tables(2).Rows.Count
ColCount = ActiveDocument.Tables(2).Columns.Count
ReDim MyArrayExternal(RowCount - 1, ColCount - 1)
For m = 1 To RowCount
For n = 1 To ColCount
'Select each cell in the table
Celldata = ActiveDocument.Tables(2).Cell(m, n)
'Remove the paragraph and end-of-cell markers
'as we load the array
MyArrayExternal(m - 1, n - 1) = Left(Celldata,
Len(Celldata) - 2)
Next
Next
ListBox2.ColumnCount = ColCount
ListBox2.List() = MyArrayExternal

End Sub

Private Sub ListBox1_Click()

For x = 0 To ListBox1.ListCount

If ListBox1.Selected(x) = True Then
MsgBox "Name:" & " " &
ListBox1.List(x) & (Chr(13)) & "Office Symbol:" &
ListSeparator & " " & ListBox1.List(x, 1) & (Chr
(13)) & "Project(s):" & ListSeparator & " " &
ListBox1.List(x, 2)
End If
Next x
End Sub

Private Sub ListBox2_Click()

For x = 0 To ListBox2.ListCount

If ListBox2.Selected(x) = True Then
MsgBox "Name:" & " " &
ListBox2.List(x) & (Chr(13)) & "Office Symbol:" &
ListSeparator & " " & ListBox2.List(x, 1) & (Chr
(13)) & "Project(s):" & ListSeparator & " " &
ListBox2.List(x, 2)
End If
Next x
End Sub


I've tried replacing ListBox1.Selected(x) with a
MatchFound for the ComboBox, but it wouldn't work. Where
am I missing the boat.

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

If my answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 
G

Guest

Still not sure how that would work?
-----Original Message-----
G'day "Pam" <[email protected]>,

when the combo _Change event fires, examine the list value and
populate the two list boxes accordingly.


"Pam" <[email protected]> was spinning this yarn:


Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

If my answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
.
 
W

Word Heretic

G'day <[email protected]>,

We define a 3d array. Col 1 is for combo, 2 is for textbox1 and 3 is
for textbox2

We stuff our values into the array - easy enough.

We read back the array,1 to fill combo

Combo change event uses .listindex to get the offset into the array to
drag up values for textbox1 and textbox2

If the user needs to change textbox1 and textbox2 it gets trickier.

We define a private to the form var called Changing of type Boolean

Private Changing as Boolean

When we go to pop the textboxes from our change event, we set
Changing=True. After setting the textboxes, we set change-false again.

The textboxes change events then look like this:

If Not Changing then
'doo de doo everything you need in here
end if



Still not sure how that would work?

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

If my answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 
P

Pam

Thanks, I will try it with the 3d array.
-----Original Message-----
G'day <[email protected]>,

We define a 3d array. Col 1 is for combo, 2 is for textbox1 and 3 is
for textbox2

We stuff our values into the array - easy enough.

We read back the array,1 to fill combo

Combo change event uses .listindex to get the offset into the array to
drag up values for textbox1 and textbox2

If the user needs to change textbox1 and textbox2 it gets trickier.

We define a private to the form var called Changing of type Boolean

Private Changing as Boolean

When we go to pop the textboxes from our change event, we set
Changing=True. After setting the textboxes, we set change- false again.

The textboxes change events then look like this:

If Not Changing then
'doo de doo everything you need in here
end if



<[email protected]> was spinning this yarn:

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

If my answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top