I have a userform that populates a ComboBox from a spreadsheet using DAO.
The datasource has 3 columns of information.
Private Sub UserForm_Initialize()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
Set db = OpenDatabase("C:\test\book1.xls", False, False, "Excel 8.0;")
Set rs = db.OpenRecordset("SELECT * FROM info")
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
ComboBox1.ColumnCount = rs.Fields.Count
ComboBox1.Column = rs.GetRows(NoOfRecords)
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
How do I return multiple values from the user's choice? I found one
solution in KnowledgeBase:
Private Sub ComboBox1_Change()
Dim SourceData As Range
'Get Range that the ComboBox is bound to
Set SourceRange = Range(ComboBox1.RowSource)
Val1 = ComboBox1.Value
'Get the value of the second column
Val2 = SourceRange.Offset(ComboBox1.ListIndex, 1).Resize(1, 1).Value
'Get the value of the third column
Val3 = SourceRange.Offset(ComboBox1.ListIndex, 2).Resize(1, 1).Value
'Concatenate the three values together and display them in Label1
Label1.Caption = Val1 & " " & Val2 & " " & Val3
End Sub
This solution works great if my Userform is created in Excel and populated
using the RowSource property of ComboBox, but I need to create this UserForm
in Word, so this won't work with the DAO steps I need to use in the
UserForm_Initialize event.
I hope this explanation makes sense. I am definitely a novice with VBA --
most of my efforts involve piecing together bits of code and making minor
changes to try to get them to work together.
I have been working on this form for over a week and I think this is the
last piece of this puzzle.
Any help would be greatly appreciated!
Colleen
Greg Maxey - 14 Dec 2007 15:10 GMT
Collen,
I probably don't understand exactly what it is that you want to do. If I
populate a ComboBox using your first procedure and select one of the entries
then this code will create the label caption that contains all the data from
that corresponding list index of the selected item:
Private Sub ComboBox1_Change()
Label1.Caption = ComboBox1.Text & ComboBox1.List(ComboBox1.ListIndex, 1) &
ComboBox1.List(ComboBox1.ListIndex, 2)
End Sub

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
> I have a userform that populates a ComboBox from a spreadsheet using
> DAO. The datasource has 3 columns of information.
[quoted text clipped - 64 lines]
>
> Colleen
David Sisson - 21 Dec 2007 16:52 GMT
Greg's answer is correct.
Perhaps a different view.
Private Sub OK_Click()
Element1 = ComboBox1.List(ComboBox1.ListIndex, 0)
Element2 = ComboBox1.List(ComboBox1.ListIndex, 1)
End Sub
They are zero based, meaning, the first one starts at zero.