A DataGrid Control as a Disconnected Data Entry Tool

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


Author: Lothar Haensler

DataGrid Control as a Data-Entry Tool (VB6 – Maybe VB5)

Screen Shot

This sample was added after a superb response from the CodeGuru VB discussion forum. The original requirement was for a way of entering and modifying a list of data in a tabular format. Lothar suggested the use of a disconnected ADOR recordset and the DataGrid control that comes with VB6 – a simple solution that works perfectly for the job.

Firstly, you need to set a reference to Microsoft Active Data Objects Recordset 2.1 library (msador15.dll). Then add the DataGrid component into your VB6 toolbar and paste in the following code into the Form_Load event after adding a Grid (DataGrid1) to your form:



private Sub Form_Load()
    Dim r as ADOR.Recordset
    Dim lCount as Long

'
' Create a new disconnected recordset object
'
    set r = new ADOR.Recordset
'
' Setup the fields - you can use any valid type of
' ado field type for these. I've used VarChar just
' for testing / demonstration purposes.
'
    r.Fields.Append "Name", adVarChar, 10
    r.Fields.Append "Notes", adVarChar, 50
    r.CursorType = adOpenDynamic
    r.Open
    r.AddNew

    r.Fields(0).Value = "Chris"
    r.Fields(1).Value = "almost over the hill"
    r.AddNew
    r.Fields(0).Value = "Chris"
    r.Fields(1).Value = "but enjoying every minute"

    for lCount = 1 to 25
        r.AddNew
        r.Fields(0).Value = "Name " & lCount + 2
        r.Fields(1).Value = "some kind of description"
    next
'
' Populate the datagrid
'
    set DataGrid1.DataSource = r

End Sub
'
'

The attached sample shows how to add new lines to the control ‘on-the-fly’ and also how to query the values in the grid using another ADOR Recordset object.

Download Zipped Project file (3k)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read