akr1234
asked on
Fill a Combo Box with values from a table
I have a ComboBox called Combo1 in VB6.
I have a table called DepartmentName with values department.
1) How do I fill the combo box with these values?
and
2) once selection is made within the combobox, how do I store it in a var to be used in another subprocedure?
I tried filling a list1 when a form loads with a value as follows:
with me.list1
.RowSource ...............
I can't find the rowsource value on the list that drops down after the dot.
I have a table called DepartmentName with values department.
1) How do I fill the combo box with these values?
and
2) once selection is made within the combobox, how do I store it in a var to be used in another subprocedure?
I tried filling a list1 when a form loads with a value as follows:
with me.list1
.RowSource ...............
I can't find the rowsource value on the list that drops down after the dot.
Why don't u try the me.combo1.items.add
Try this:
Dim var as String
With Combo1
.AddItem "departmentVal1"
.AddItem "departmentVal2"
var = .Text
End With
Dim var as String
With Combo1
.AddItem "departmentVal1"
.AddItem "departmentVal2"
var = .Text
End With
Hi,
In addition to my previous comment...
You can use .AddItem method of combo box control to fill your values in the control. In case of "departmentVal1 and departmentVal2 you can also use the values from the RecordSet.
Eg:
With Combo1
.AddItem rst.Fields("FieldName")
End With
Moreover if you want to store the value of the selected item then you can use .Text property of the control or you can use .ListIndex which will give you the index of the selected item.
I hope it will help you.
Cheers
In addition to my previous comment...
You can use .AddItem method of combo box control to fill your values in the control. In case of "departmentVal1 and departmentVal2 you can also use the values from the RecordSet.
Eg:
With Combo1
.AddItem rst.Fields("FieldName")
End With
Moreover if you want to store the value of the selected item then you can use .Text property of the control or you can use .ListIndex which will give you the index of the selected item.
I hope it will help you.
Cheers
ASKER
Nothing works
Don't assume I know anything.
I think I asked too many questions to begin with.
I have a table called TBLDEPARTMENTS with fields AutoNum and DeptName.
When the form starts I want to fill in the combo box with the values from the DeptName fields.
Don't assume I know anything.
I think I asked too many questions to begin with.
I have a table called TBLDEPARTMENTS with fields AutoNum and DeptName.
When the form starts I want to fill in the combo box with the values from the DeptName fields.
Set a reference to MS DAO Object Library
'Set global database path
Set db = OpenDatabase("yourDatabase Path.mdb")
'Set local rs to get next booking number
Set rs = db.OpenRecordset("TBLDEPAR TMENTS")
rs.MoveFirst
Do Until rs.EOF
Combo1.AddItem rs.Fields("DeptName")
rs.MoveNext
Loop
'Set global database path
Set db = OpenDatabase("yourDatabase
'Set local rs to get next booking number
Set rs = db.OpenRecordset("TBLDEPAR
rs.MoveFirst
Do Until rs.EOF
Combo1.AddItem rs.Fields("DeptName")
rs.MoveNext
Loop
ASKER
How do I do this
>>>>Set a reference to MS DAO Object Library
>>>>Set a reference to MS DAO Object Library
Goto:
Project -> References
Project -> References
ASKER
I set the reference to MS DAO 3.51 Object Library
When I run the code Form opens up and the Combo box is empty. Is this the right event which I am using.
Also it gives an error at the """Set db = OpenDatabase("c:\db1.mdb") """ Type Mismatch.
Private Sub Combo1_Change()
Dim db As Recordset
Dim rs As Recordset
Set db = OpenDatabase("c:\db1.mdb")
Set rs = db.OpenRecordset("TBLDEPAR TMENT")
rs.MoveFirst
Do Until rs.EOF
Combo1.AddItem rs.Fields("Departmantname" )
rs.MoveNext
Loop
End Sub
When I run the code Form opens up and the Combo box is empty. Is this the right event which I am using.
Also it gives an error at the """Set db = OpenDatabase("c:\db1.mdb")
Private Sub Combo1_Change()
Dim db As Recordset
Dim rs As Recordset
Set db = OpenDatabase("c:\db1.mdb")
Set rs = db.OpenRecordset("TBLDEPAR
rs.MoveFirst
Do Until rs.EOF
Combo1.AddItem rs.Fields("Departmantname"
rs.MoveNext
Loop
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks, but why a "B" grade?
ASKER
Thanx bingie, I clicked on good by mistake, it was excellent. Sorry.
Here is the code, the event should be dropdown
Private Sub Combo1_DropDown()
Dim db As Database
Dim rs As Recordset
'Set global database path
Set db = OpenDatabase("c:\db1.mdb")
'Set local rs to get next booking number
Set rs = db.OpenRecordset("TBLDEPAR TMENT")
rs.MoveFirst
Do Until rs.EOF
Combo1.AddItem rs.Fields("Departmantname" )
rs.MoveNext
Loop
End Sub
Here is the code, the event should be dropdown
Private Sub Combo1_DropDown()
Dim db As Database
Dim rs As Recordset
'Set global database path
Set db = OpenDatabase("c:\db1.mdb")
'Set local rs to get next booking number
Set rs = db.OpenRecordset("TBLDEPAR
rs.MoveFirst
Do Until rs.EOF
Combo1.AddItem rs.Fields("Departmantname"
rs.MoveNext
Loop
End Sub
ASKER
How can I change it to Excellent??
Glad you got it to work :)
You can post the request in Community Support (http:Community_Support)
Don't forget to leave feedback, and i'll leave some for you. :)
You can post the request in Community Support (http:Community_Support)
Don't forget to leave feedback, and i'll leave some for you. :)