Populate combo box items from database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vaniKanabathy
    New Member
    • Mar 2008
    • 52

    Populate combo box items from database

    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.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Kindly post the code that you are working on.

    Comment

    • vaniKanabathy
      New Member
      • Mar 2008
      • 52

      #3
      [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]
      Last edited by debasisdas; Apr 11 '08, 09:48 AM. Reason: added code=vb tags

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        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
        Veena

        Comment

        • vaniKanabathy
          New Member
          • Mar 2008
          • 52

          #5
          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]
          Last edited by debasisdas; Apr 11 '08, 09:49 AM. Reason: added code=vb tags

          Comment

          Working...