Results 1 to 9 of 9

Thread: DBGrid and Recordset

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    32

    Question

    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

  2. #2
    Hyperactive Member Paul Warren's Avatar
    Join Date
    Jun 2000
    Location
    UK
    Posts
    282
    Is this a DBGrid as in VB5 or a DataGrid as in VB6 ?
    That's Mr Mullet to you, you mulletless wonder.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    32
    Its in VB6

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Question

    set the Datasource property of the datagrid to the recordset you have created

    Code:
     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
    Code:
    DataGrid1.Columns.Add(0).Caption = "Au_ID"
    Do this for each column you want.


    Things I do when I am bored: DotNetable

  5. #5
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    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


    Things I do when I am bored: DotNetable

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    32

    Question

    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?

  7. #7
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    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?


    Things I do when I am bored: DotNetable

  8. #8
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    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.

    Code:
    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


    Things I do when I am bored: DotNetable

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    32

    Thumbs up

    works great, thanks

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