Hi, im using vb6 and access, im using combo box in my form. I want to retrieve the items for combo box automatically from database. Plzzzzzz help me , urgent. Thanks.
Populate combo box items from database
Collapse
X
-
Tags: None
-
-
[CODE=vb]Dim myconn As ADODB.Connectio n
'Declares strSQL as a string for the SQL statement
Dim strSQL As String
db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
db_file = db_file & "Everspark2.mdb "
'Sets myconn as a connection
Set myconn = New ADODB.Connectio n
myconn.Connecti onString = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
myconn.Open
Combo1.Clear
Dim rsRecords As New ADODB.Recordset
Set rsRecords = New ADODB.Recordset
Dim sSQL As String
sSQL = "SELECT CarType FROM CarList"
rsRecords.Open sSQL, myconn
With rsRecords
.Open CarList, myconn
Do While Not .EOF
Combo1.AddItem .Fields("CarTyp e")
.MoveNext
Loop
Combo1.AddItem
.Close
End With
Set rsRecords = Nothing[/CODE]Comment
-
Hi,
You have already opened the recordset, again why open...?
check the modified code here :
[code=vb]
With rsRecords
' Comment the Line Below
'.Open CarList, myconn
Do While Not .EOF
Combo1.AddItem .Fields("CarTyp e")
.MoveNext
Loop
[/code]
REgards
VeenaComment
-
I get the solution.Thanks .
[code=vb]db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
db_file = db_file & "Everspark2.mdb "
' Open a connection.
Set conn = New ADODB.Connectio n
conn.Connection String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
conn.Open
' Select the data.
statement = "SELECT CarType FROM CarList "
' Get the records.
Set rs = conn.Execute(st atement, , adCmdText)
With Combo1
Do Until rs.EOF
.AddItem rs.Fields("CarT ype").Value
rs.MoveNext
Loop
End With
rs.Close
conn.Close[/code]Comment
Comment