Solved

Fill a Combo Box with values from a table

Posted on 2004-09-06
13
4,190 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:akr1234
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 7

Expert Comment

by:SQL_Stu
ID: 11990046
Why don't u try the me.combo1.items.add
0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 11990068
Try this:
    Dim var as String
    With Combo1
        .AddItem "departmentVal1"
        .AddItem "departmentVal2"
        var = .Text
    End With
0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 11990105
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:akr1234
ID: 11990217
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.
0
 
LVL 11

Expert Comment

by:bingie
ID: 11991240
Set a reference to MS DAO Object Library

'Set global database path
Set db = OpenDatabase("yourDatabasePath.mdb")

'Set local rs to get next booking number
Set rs = db.OpenRecordset("TBLDEPARTMENTS")

rs.MoveFirst

Do Until rs.EOF
  Combo1.AddItem rs.Fields("DeptName")
  rs.MoveNext
Loop
0
 

Author Comment

by:akr1234
ID: 11992579
How do I do this

>>>>Set a reference to MS DAO Object Library
0
 
LVL 11

Expert Comment

by:bingie
ID: 11992617
Goto:

Project -> References
0
 

Author Comment

by:akr1234
ID: 11992710
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("TBLDEPARTMENT")

rs.MoveFirst

Do Until rs.EOF
  Combo1.AddItem rs.Fields("Departmantname")
  rs.MoveNext
Loop
End Sub
0
 
LVL 11

Accepted Solution

by:
bingie earned 250 total points
ID: 11992756
change,

Dim db As Recordset

to

Dim db as Database
0
 
LVL 11

Expert Comment

by:bingie
ID: 11992799
Thanks, but why a "B" grade?
0
 

Author Comment

by:akr1234
ID: 11992803
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("TBLDEPARTMENT")

rs.MoveFirst

Do Until rs.EOF
  Combo1.AddItem rs.Fields("Departmantname")
  rs.MoveNext
Loop
End Sub
0
 

Author Comment

by:akr1234
ID: 11992813
How can I change it to Excellent??
0
 
LVL 11

Expert Comment

by:bingie
ID: 11992820
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. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro which automatically sends attachment to Outlook 14 81
Add and format columns in vb6 7 70
Advice in Xamarin 21 109
Error with a code discussed on this page 5 38
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question