I want to display data in a DBGrid. I create a recordset at runtime. Is there a way i can use the values from the recordset and display them in the DBGrid?
Is there any other way that might be better to do this?
Thanks in advance
Printable View
I want to display data in a DBGrid. I create a recordset at runtime. Is there a way i can use the values from the recordset and display them in the DBGrid?
Is there any other way that might be better to do this?
Thanks in advance
Is this a DBGrid as in VB5 or a DataGrid as in VB6 ?
Its in VB6
set the Datasource property of the datagrid to the recordset you have created
Then with the datagrid on the form right click and choose 'properties'Code:Set DataGrid1.DataSource = Adodc1
On the columns tab select the column and designate a caption then specify the database column you are binding.
Take a look at the other settings while you are there like update, and addnew settings.
The headers can also be done in code
Do this for each column you want.Code:DataGrid1.Columns.Add(0).Caption = "Au_ID"
Don't forget to use the 'Retrieve Fields' option from the pop up menu when you have right clicked on the control after assigning the recordset to the datagrid
I recieved an error that said:
"class does not support automation or does not support expected interface"
This is what i did. I wrote code on the onload,
set dbgrid1.datasource = rsfeatures
then i went into properties on the dbgrid, and on the columns tab, entered the database field which was called feature_number into the datafield combobox.
My recordset is being created at runtime, so i don't know how it would recognize it being bound to the DBGrid.
Am i forgetting to do something, or is there a different way to do this?
Are you using ADO or DAO?
The Data Environment, Data Control or strictly code to set up the recordset?
Is the database Access or SQL server?
I got the error you described when I used tried using an ADO recordset with a DBGrid instead of a DataGrid. A DBGrid is DAO not ADO compatible a DataGrid is. When I used a DataGrid with an ADO recordset as in the code below there was no problem, I didn't even have to set any of the properties of the DataGrid at all just place a DataGrid on a form, place the following code on the form and 'hey presto', Bob's your Auntie.
You may want to play around with the rs.open cursor and locking parametersCode:Option Explicit
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Form_Load()
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
cnn.Open '<COnnectionString>
rs.Open "SELECT * from staff", cnn, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = rs
End Sub
works great, thanks