Click to See Complete Forum and Search --> : Filling a Combobox using an ADO recordset
Spooked
May 31st, 2000, 07:32 PM
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?
Aldea
May 31st, 2000, 08:15 PM
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
dcarlson
Jun 1st, 2000, 03:48 AM
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.
JasonGS
Jun 1st, 2000, 05:48 AM
' 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
JonnyCab
Jun 1st, 2000, 10:29 AM
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
Edneeis
Jun 1st, 2000, 02:14 PM
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.
Spooked
Jun 1st, 2000, 02:52 PM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.