PDA

Click to See Complete Forum and Search --> : DBGrid and Recordset


ghoff12
Aug 15th, 2000, 10:13 AM
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

Paul Warren
Aug 16th, 2000, 03:29 AM
Is this a DBGrid as in VB5 or a DataGrid as in VB6 ?

ghoff12
Aug 16th, 2000, 09:10 AM
Its in VB6

davidrobin
Aug 16th, 2000, 10:09 AM
set the Datasource property of the datagrid to the recordset you have created

Set DataGrid1.DataSource = Adodc1

Then with the datagrid on the form right click and choose 'properties'

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
DataGrid1.Columns.Add(0).Caption = "Au_ID"


Do this for each column you want.

davidrobin
Aug 16th, 2000, 10:19 AM
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

ghoff12
Aug 16th, 2000, 11:04 AM
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?

davidrobin
Aug 17th, 2000, 03:30 AM
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?

davidrobin
Aug 17th, 2000, 05:18 AM
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.

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

You may want to play around with the rs.open cursor and locking parameters

ghoff12
Aug 17th, 2000, 01:50 PM
works great, thanks