By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

Populate combo box items from database

P: 52
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.
Apr 11 '08 #1
Share this Question
Share on Google+
4 Replies


debasisdas
Expert 5K+
P: 8,127
Kindly post the code that you are working on.
Apr 11 '08 #2

P: 52
Expand|Select|Wrap|Line Numbers
  1. Dim myconn As ADODB.Connection
  2. 'Declares strSQL as a string for the SQL statement
  3. Dim strSQL As String
  4.  
  5. db_file = App.Path
  6.     If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
  7.     db_file = db_file & "Everspark2.mdb"
  8.  
  9. 'Sets myconn as a connection
  10. Set myconn = New ADODB.Connection
  11. myconn.ConnectionString = _
  12.         "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  13.         "Data Source=" & db_file & ";" & _
  14.         "Persist Security Info=False"
  15.     myconn.Open
  16.  
  17.  Combo1.Clear
  18.  
  19.  
  20.      Dim rsRecords As New ADODB.Recordset
  21.      Set rsRecords = New ADODB.Recordset
  22.      Dim sSQL As String
  23.      sSQL = "SELECT CarType FROM CarList"
  24.      rsRecords.Open sSQL, myconn
  25.  
  26.     With rsRecords
  27.     .Open CarList, myconn
  28.     Do While Not .EOF
  29.     Combo1.AddItem .Fields("CarType")
  30.         .MoveNext
  31.     Loop
  32.  
  33.     Combo1.AddItem
  34.     .Close
  35.  
  36.     End With
  37.     Set rsRecords = Nothing
Apr 11 '08 #3

QVeen72
Expert 100+
P: 1,445
Hi,

You have already opened the recordset, again why open...?
check the modified code here :

Expand|Select|Wrap|Line Numbers
  1. With rsRecords
  2. ' Comment the Line Below
  3. '.Open CarList, myconn
  4.  
  5. Do While Not .EOF
  6. Combo1.AddItem .Fields("CarType")
  7. .MoveNext
  8. Loop
  9.  
  10.  
REgards
Veena
Apr 11 '08 #4

P: 52
I get the solution.Thanks.

Expand|Select|Wrap|Line Numbers
  1. db_file = App.Path
  2.     If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
  3.     db_file = db_file & "Everspark2.mdb"
  4.  
  5.     ' Open a connection.
  6.     Set conn = New ADODB.Connection
  7.     conn.ConnectionString = _
  8.         "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  9.         "Data Source=" & db_file & ";" & _
  10.         "Persist Security Info=False"
  11.     conn.Open
  12.  
  13.     ' Select the data.
  14.     statement = "SELECT CarType FROM CarList "
  15.  
  16.     ' Get the records.
  17.     Set rs = conn.Execute(statement, , adCmdText)
  18.  
  19. With Combo1
  20. Do Until rs.EOF
  21. .AddItem rs.Fields("CarType").Value
  22. rs.MoveNext
  23. Loop
  24. End With
  25.  rs.Close
  26.     conn.Close
Apr 11 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.