Results 1 to 7 of 7

Thread: Filling a Combobox using an ADO recordset

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    29
    I need to be able to fill a combobox from a recordset I have created. Can anyone give me an example of how to do this using ADO?

  2. #2
    New Member
    Join Date
    Jun 2000
    Posts
    8

    Wink

    Suppose you have installed controls Sheridan DataGrid/Combo/DropDown (OLEDB).
    Anyway you'll use only SSOLEDBCombo for this problem.

    After you open the recordset:
    Set cboBox.DataSourceList = rsREC
    cboBox.DataFieldList = rsREC.Fields(0).Name'depends what fields you wanna display in combo

    Legend:
    -cboBox-name of the SSOleDBCombo
    -rsREC-name of the recordset
    These 2 properties "DataSourceList" and "DataFieldList" are essentials.
    Example:
    Const gsConnectionStr As String = "DSN=gest_pers;UID=admin;PWD=''"
    dim cnn as Adodb.Connection
    dim rsREC as New Adodb.recordset
    dim sSQL as string
    set cnn=New Adodb.connection
    cnn.Open gsConnectionStr
    sSQL="SELECT date FROM Hiredate"
    rsREC.open sSQL,cnn,adOpenStatic, adLockReadOnly
    'This is what interest you
    Set cboBox.DataSourceList = rsREC
    cboBox.DataFieldList = rsREC.Fields(0).Name

  3. #3
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Sudbury, Ontario, Canada
    Posts
    274
    Here's quick example. Assuming you have a connection (fCnn1)

    Dim strSql as String
    Dim rstTemp as ADODB.Recordset

    strSql = "SELECT region "
    strSql = strsql & "FROM region ORDER BY Region"

    Set rstTemp = fCnn1.Execute(strSql, , adCmdText)

    Do While Not rstTemp.EOF
    cboRegion.AddItem rstTemp!region
    rstTemp.MoveNext
    Loop

    rstTemp.Close
    Set rstTemp = Nothing

    Hope this helps.

  4. #4
    Addicted Member JasonGS's Avatar
    Join Date
    May 2000
    Location
    California
    Posts
    155

    another way to do

    Code:
    ' Connect here
    adoConn.Open "Database Connection String"
    
    ' Fill your combobox here
    adoRset.Open "SELECT * FROM table", adoConn, adOpenForwardOnly + adOpenStatic, adLockReadOnly
    Do While Not adoRset.EOF
        Combo1.AddItem adoRset("field1")
        adoRset.MoveNext
    Loop
    adoRset.Close

  5. #5
    Lively Member
    Join Date
    May 2000
    Posts
    70
    Im new to this VB stuff so excuss me if any advice I give is crap but...
    If you have VB 6 ADO allows you to fill a combo box without the need for any coding. I used a dataCombo because that was the only one that seemed to work.

    Create a DataEnvironment, a ADO Connection, a SQL Command on tables required. In the properties of your datacombo you should now be able to select from these.

    Datasource should = DataEnvironment
    Datamember should = SQL command created
    Datafield should = the field you want to place in combobox
    Rowsource & Rowmember should = same as Datasource & Datamem
    Listfield should = Datafield
    Combo (Style) should = 2 dbcDropdownlist

    Lets us know how you went.

    Cheers Jonny


  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The new guy is right it is easier to use the DataCombo instead. It is in the 'Microsoft DataList 6.0 (SP3) (OLEDB)' if you have vb6. Then just set the DataSource property to the ADO control and the Listfield property to whichever field you want to populate the DataCombo dropdown with.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    29
    Thanks a lot for all the suggestions. I finally used the code from Jason but I was wondering how I go about excluding the Combobox.Text entry from appearing in the list of options.

    The Combobox is filled but the first entry is the text property of the Combobox control which I really don`t want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width