How can I show my data in a combo box. I need help . anybody help me.

Recommended Answers

All 5 Replies

You can do this one if you like..
I dunno if the code is correct and this is what you want but you can do this..

Use adodb...

Dim cn as ADODB.Connection
Dim rc as ADODB.Recordset
dim cm as ADODB.Command

then..

cn.open "then your connection string"

and then...

cm.CommandText = "your sql command"
cm.execute

while rc.EOF <> True
Combo1.Add rc!FieldName
rc.MoveNext
wend

open a recordset and simple do like.this is easy way to get data in
a combo box.

If Not (rs.EOF And rs.BOF) Then 'check if it is either eof and bof
rs.MoveFirst                'go to first record
Do Until rs.EOF          
CboEmpName.AddItem (rs!Name)     'fill data using additem fuction
rs.MoveNext
Loop
End If

You can do this one if you like..
I dunno if the code is correct and this is what you want but you can do this..

Use adodb...

Dim cn as ADODB.Connection
Dim rc as ADODB.Recordset
dim cm as ADODB.Command

then..

cn.open "then your connection string"

and then...

cm.CommandText = "your sql command"
cm.execute

while rc.EOF <> True
Combo1.Add rc!FieldName
rc.MoveNext
wend

I think you need to set the itemdata property on the added items.
This can be set to anumeric value (like id column)

ex: you can modify the code like this

...

while rc.EOF <> True
Combo1.Add rc!FieldName
Combo1.ItemData(Combo1.NewIndex) = rc!FieldName
rc.MoveNext
wend

Now you can read the itemdata value of the selected item by typing the below code:

Combo1.ItemData(Combo1.ListIndex)

i tried but i got error type mismatch.Kindly let me know the idea.

Private Sub FillEmployee()
Dim strSql As String
Dim con As ADODB.Connection, rs As ADODB.Recordset
Set con = New ADODB.Connection
If Not OpenConnection(con) Then
  MsgBox ("cannot open connection")
  Set con = Nothing
End If
Set rs = New ADODB.Recordset
strSql = "select employees.name from Employees"
rs.Open strSql, con, adOpenDynamic, adLockOptimistic
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF
'CboEmpName.AddItem (rs!Name)
CboEmpName.ItemData(CboEmpName.NewIndex) = rs!Name
'CboEmpName.ItemData (CboEmpName.ListIndex)
rs.MoveNext
Loop
End If
Call CloseRecordset(rs)
Call CloseConnection(con)
End Sub

Heres my code again.. and its working.... ive tried it and i also attached a sample.. just take a look at it and try it okie...

'Declaration
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Public Sub con()

'Open The Connection
cn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database1.mdb;Jet OLEDB:Database Password=;")

'Open The Recordset
rs.Open "SELECT * FROM Table1", cn, adOpenStatic, adLockOptimistic

End Sub

Private Sub FillCombo_Click()

'Fill The Combo box
While rs.EOF <> True
Combo1.AddItem rs!FirstName
rs.MoveNext
DoEvents 'Add this code so that the system will not freeze...
Wend

End Sub

Private Sub Form_Load()
'Call Sub con
Call con

End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.