filling a combo box


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: filling a combo box

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    filling a combo box

    [Originally posted by jw]

    I am looking for some sample code of loading data in a combo box from an access database? Can anyone help. Thank you

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:filling a combo box

    [Originally posted by Dipankar]

    write the code in combo's drop down event-->
    (works for access db with dsn connection)
    dim rs as new adodb.recordset
    set rs=nothing
    set rs=cn.execute("Select * from tablename")
    'cn is the connection string
    do until rs.eof=true
    combo.additem rs![fieldname to add]
    rs.movenext
    loop


  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:filling a combo box

    [Originally posted by Gordon]

    Using DAO - Use your path/file name; change expression SomeField to
    name of field from Access table which you want to display in the
    ComboBox; change SomeTable to name of your table:

    Dim dbGord As Database
    Dim rsGord As DAO.Recordset

    Set dbGord = OpenDatabase("C:\SomePath\Your.mdb")
    Set rsGord = dbGord.OpenRecordset("SELECT SomeField FROM SomeTable")

    With rsGord
    If .RecordCount > 0 Then
    Do Until .EOF
    cboYourComboName.AddItem !SomeField
    .MoveNext
    Loop
    End If
    End With

    rsGord.Close
    Set rsGord = Nothing
    dbGord.Close
    Set dbGord = Nothing

    What we're telling VB to do is: go get the records from your table.
    If there are any records (If .RecordCount > 0), then start at beginning
    of recordset and loop thru them until the End Of File (.EOF) is
    reached. As it goes thru each record, it adds the field you specify
    (!SomeField) to the ComboBox. After that, we close database and
    recordset and clear memory of those references by setting them to
    Nothing.

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:filling a combo box

    [Originally posted by Sabir Mustafa]

    Use this!

    Note:- Replace variables as necessary.

    Dim Rs as adodb.recordset
    Set Rs=New Adodb.recordset

    rs.cursorlocation=aduseclient

    Rs.open "Customer",Cn

    if rs.recordcount>0 then

    do while not rs.eof
    combo1.additem Rs![CustName]
    rs.movenext
    loop
    else
    Msgbox "No records"
    end if



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center