ado and flexgrid

Hi,
How do I bind an ado data control to a msflexgrid? I am using ado data control and I can't seem to get it bind to my flexgrid (the flexgrid only allows the dao data controls to be bound to it). Will binding the datacontrol to my flexgrid improve performance; my understanding is that it will only populate the grid to the extent of what is visible (let's say I have 10 grid rows) and only repopulate when the user scrolls to the next 10 etc.
Will binding to the data control limit my functionality of my flexgrid. This is because I want to be able to remove rows from my flexgrid (from the grid display only not the database); if my flexgrid is bound to the ado datacontrol, how can I do that?

Would be grateful if somebody can help.
thanks.
tanmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mohan_sekarCommented:
Hi tanm,

         Why don't u try Data Grid Control. I hope it would help u.

bye
Mohan
TimCotteeHead of Software ServicesCommented:
The standard MSFlexgrid does not have good ADODC binding. You can bind an ADO recordset at Runtime, but not at designtime. If you include the following:

Dim rstData As ADODB.Recordset
Dim cnnData As ADODB.Connection

Private Sub Form_Load()
  Set cnnData = New ADODB.Connection
  cnnData.ConnectionString = "Copy the Connectionstring property from your ADODC"
  cnnData.Open
  rstData.Open "Select * From MyTable",cnnData,adOpenStatic,adLockReadOnly
  Set MSFlexgrid1.Datasource = rstData
  Set rstData.ActiveConnection = Nothing 'Disconnect the recordset from the underlying data source
  'Close and destroy the database connection
  cnnData.Close
  Set cnnData = Nothing
End Sub

Or similar in your code, you should see the recordset in the flexgrid.

This creates the recordset without using the data control and disconnects it from the database connection. From this point the recordset only exists in the memory of your computer and you can do whatever you like with it without affecting the underlying data.
tanmAuthor Commented:
Hi TimCottee,
I tried your code but I'm getting the following error message when I run it..
"Run-time error 91"
"Object variable or With variable not set"

It stops at the line  "Set MSFlexGrid1.DataSource = rstData". I have already created the msflexgrid1 flexgrid.
Can the code be used on VB5 or has it got to be VB6?
I think I ran a similar code on VB6 and I'm getting the same message.
Please help.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

iboutchkineCommented:
cnnData.Open
>here set the recordset
Set rsData = New ADODB.Recordset

 rstData.Open "Select * From MyTable",cnnData,adOpenStatic,adLockReadOnly
 
tanmAuthor Commented:
hi iboutchkine,
I did that but it still gives me the same error.
what do you think is the problem?
tanmAuthor Commented:
since i'm not receiving a reasonable response i will delete this question.
i_jester69Commented:
That error almost always means you either did not dim the variable correctly or the Reference is not valid in your project.

iboutchkineCommented:
In TimCottee answer add the line

Dim rstData As ADODB.Recordset
Dim cnnData As ADODB.Connection

Private Sub Form_Load()
 Set cnnData = New ADODB.Connection
 cnnData.ConnectionString = "Copy the Connectionstring property from your ADODC"
 cnnData.Open

'add this line here------------------
Set rsData = New ADODB.Recordset
'-------------------------------

 rstData.Open "Select * From MyTable",cnnData,adOpenStatic,adLockReadOnly
 Set MSFlexgrid1.Datasource = rstData
 Set rstData.ActiveConnection = Nothing 'Disconnect the recordset from the underlying data source
 'Close and destroy the database connection
 cnnData.Close
 Set cnnData = Nothing
End Sub

tanmAuthor Commented:
hi iboutchine,
i tried your code but it still gave me the same error. i corrected the typo in your suggestion from rsdata to rstdata..  

I have already added the references but it still gave me the same error.
it stopped at line
Set MSFlexgrid1.Datasource = rstData

anybody has any clues?

Chandramouli kArchitectCommented:
hi tanm,

********************************
first, u cannot bind ado dc to msflexgrid. but u can do with VSFlexgrid Pro. www.qbssoftware.com. VSFlexGrid Pro provides several options for data binding: ADO/OLEDB, DAO, bind to 2-D or 3-D arrays etc. but it costs around 190 sterling.
********************************

Instead of binding the grid with ado-datacontrol, i would suggest u to populate the flexgrid with contents of dummy ClientSide(CS) recordset.

The following code uses a Client side recorset. All the records from the Original recordset is copied into the Dummy CS recordset. The records of CS recordset is then populated into Flexgrid. when the user deletes a record, the record in the dummy CS recordset is deleted and the remaining recordset is again populated. by this way, u can delete rows from flexgrid without actually deleting the data from the database. and there will be no overhead on the server since the recordset is ClientSide only. I have used the Biblio database and Publishers table.

**************************
Try this code if possible.
**************************

**************************
ALL THE BEST

regards
KCM
**************************

Dim Objcn As ADODB.Connection
Dim Objrs As ADODB.Recordset
Dim objClientSet As ADODB.Recordset
***********
Private Sub Command1_Click()
  Objrs.Open "Select * from Publishers", Objcn, 0, 1
  AddtoDummyRecSet
End Sub
***********
Private Sub Form_Load()
  Set Objcn = New ADODB.Connection
  Set Objrs = New ADODB.Recordset
  Objcn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data  Source=D:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB;Persist Security Info=False"

End Sub
***********
Private Sub AddtoDummyRecSet()
  Set objClientSet = New ADODB.Recordset
    'client side cursors
  objClientSet.CursorLocation = adUseClient
    'changes are not visible to others
  objClientSet.CursorType = adOpenKeyset
  For i = 0 To Objrs.Fields.Count - 1
    objClientSet.Fields.Append Objrs.Fields(i).Name,  Objrs.Fields(i).Type, _
        Objrs.Fields(i).DefinedSize
  Next i
 
  objClientSet.Open
 
  While Not Objrs.EOF
    objClientSet.AddNew
    For i = 0 To Objrs.Fields.Count - 1
        If Not IsNull(Objrs(i)) Then
            objClientSet(i) = Objrs(i)
        Else
            objClientSet(i) = ""
        End If
    Next i
    objClientSet.Update
    Objrs.MoveNext
  Wend
  Objrs.Close

  If Not objClientSet.BOF Then objClientSet.MoveFirst
 
  PopulateData objClientSet
End Sub
***********
Private Sub PopulateData(objClientSet As ADODB.Recordset, Optional nId As Double)

Dim intRowCtr As Integer
 
  If nId > 0 Then
    If Not objClientSet.BOF Then objClientSet.MoveFirst
   
    objClientSet.Find "PubId = " & nId
    If Not objClientSet.EOF Then objClientSet.Delete
   
    If Not objClientSet.BOF Then objClientSet.MoveFirst
  End If

  If Not (objClientSet.BOF And objClientSet.EOF) Then
    MSFlexGrid1.FixedCols = 0
    intRowCtr = 0
    MSFlexGrid1.Cols = objClientSet.Fields.Count
    For i = 0 To objClientSet.Fields.Count - 1
        MSFlexGrid1.TextMatrix(intRowCtr, i) = objClientSet.Fields(i).Name
    Next i
    While Not objClientSet.EOF
        intRowCtr = intRowCtr + 1
        MSFlexGrid1.Rows = intRowCtr + 1
        For i = 0 To objClientSet.Fields.Count - 1
            If Not IsNull(objClientSet(i)) Then
                MSFlexGrid1.TextMatrix(intRowCtr, i) = objClientSet(i)
            Else
                MSFlexGrid1.TextMatrix(intRowCtr, i) = ""
            End If
        Next i
        objClientSet.MoveNext
    Wend
  End If
End Sub
***********
Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
  Dim nUniqId As Double

  With MSFlexGrid1
    nUniqId = CDbl(.TextMatrix(.Row, 0))
  End With

  If KeyCode = vbKeyDelete Then
    PopulateData objClientSet, nUniqId
  End If
End Sub
***********

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanRollinsCommented:
Hi tanm,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: TimCottee and kcm76

tanm, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
NetminderCommented:
Per recommendation, force-accepted.

Netminder
CS Moderator

TimCottee: points for you at http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20353470.html
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.