Results 1 to 13 of 13

Thread: Binding Data Grids & Adding References [RESOLVED]

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    Everywhere
    Posts
    46

    Resolved Binding Data Grids & Adding References [RESOLVED]

    It's been a while since I've had VB.Net and even longer since I've had VB6. I'm working in VB6 because I don't have a choice.

    My questions are:

    1) What is the process for binding a data grid, or simply showing a tables contents on a grid?

    2) Are there any specific references that I need to add to a project to get it to work?

    3) Can someone give me a brief quick and dirty example of pulling all records from a table (MS Access) and display them in a data grid?



    Thanks SO much!
    Last edited by Myriad_Rocker; Feb 16th, 2005 at 05:17 PM.
    Myriad Rocker
    The ability to speak does not make you intelligent

    I haven't had VB since college...go easy.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Binding Data Grids & Adding References

    1. Go to components and add MS DataGrid 6.0 (OLEDB) and also ADO Data Control
    2. Paste both controls on the form
    3. Set (at design in the properties window) grid's Datasource = ADODC1
    4. Right click on the data control and select properties.
    5. Select from list of drivers MS Jet 4.0 (or 3.51) - depends on Access you might have
    6. Select "Use Connection String" and click "Bui;d ..."
    7. Follow simple instructions and click OK.
    8. Select Recordsource tab on the Property Pages (ado data control)
    9. Select adCmdTable from Command Type dropdown box
    10. Select Table from next dropdown box.

    Save your project and run it.

  3. #3
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627

    Re: Binding Data Grids & Adding References

    Without using the data control, you could do the following...

    Add a reference to Microsoft ActiveX Data Objects (2.7 Library in my case)

    Add a datagrid to your form and a command button

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim cn As ADODB.Connection
    3.     Dim rs As ADODB.Recordset
    4.    
    5.     Set cn = New ADODB.Connection
    6.     Set rs = New ADODB.Recordset
    7.    
    8.     'open connection
    9.     cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb;Persist Security Info=False"
    10.    
    11.     'set recordset cursor location to client (sometimes grid acts up without this line)
    12.     rs.CursorLocation = adUseClient
    13.    
    14.     'open recordset
    15.     rs.Open "SELECT * FROM table1", cn, adOpenKeyset, adLockReadOnly, adCmdText
    16.    
    17.     'make sure records returned
    18.     If rs.RecordCount = 0 Then
    19.         MsgBox "No records returned"
    20.         Exit Sub
    21.     End If
    22.    
    23.     'set grids datasource
    24.     Set DataGrid1.DataSource = rs
    25.     DataGrid1.Refresh
    26. End Sub
    Here's to us!
    Who's like us?
    Darned few, and they're all dead!

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    Everywhere
    Posts
    46

    Re: Binding Data Grids & Adding References

    Thanks very much to both of you! Got it.
    Myriad Rocker
    The ability to speak does not make you intelligent

    I haven't had VB since college...go easy.

  5. #5
    New Member
    Join Date
    Feb 2005
    Posts
    7

    Re: Binding Data Grids & Adding References

    Quote Originally Posted by demotivater
    Without using the data control, you could do the following...

    Add a reference to Microsoft ActiveX Data Objects (2.7 Library in my case)

    Add a datagrid to your form and a command button

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim cn As ADODB.Connection
    3.     Dim rs As ADODB.Recordset
    4.    
    5.     Set cn = New ADODB.Connection
    6.     Set rs = New ADODB.Recordset
    7.    
    8.     'open connection
    9.     cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb;Persist Security Info=False"
    10.    
    11.     'set recordset cursor location to client (sometimes grid acts up without this line)
    12.     rs.CursorLocation = adUseClient
    13.    
    14.     'open recordset
    15.     rs.Open "SELECT * FROM table1", cn, adOpenKeyset, adLockReadOnly, adCmdText
    16.    
    17.     'make sure records returned
    18.     If rs.RecordCount = 0 Then
    19.         MsgBox "No records returned"
    20.         Exit Sub
    21.     End If
    22.    
    23.     'set grids datasource
    24.     Set DataGrid1.DataSource = rs
    25.     DataGrid1.Refresh
    26. End Sub
    I find your code very useful. But I have *.dbf file. And when I replace *mdb with *.dbf , it says "unrecognized database format". Is it problem with provider??

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Binding Data Grids & Adding References [RESOLVED]

    To open DBF type data file you'll have to use slightly different technic:
    VB Code:
    1. sDbfConString = "Provider=SDEOLEDB;Data Source=C:\DBF\;Mode=Read|Write;SDE OLEDB: RDE Type=FoxPro"
    2. adoConDbf.Open sDbfConString
    3.  
    4. adoRST.Open "File=FILENAME;Index=TAG:INDEXNAME", adoConDbf, adOpenStatic, adLockOptimistic, adCmdTable

  7. #7
    New Member
    Join Date
    Feb 2005
    Posts
    7

    Re: Binding Data Grids & Adding References [RESOLVED]

    Quote Originally Posted by RhinoBull
    To open DBF type data file you'll have to use slightly different technic:
    VB Code:
    1. sDbfConString = "Provider=SDEOLEDB;Data Source=C:\DBF\;Mode=Read|Write;SDE OLEDB: RDE Type=FoxPro"
    2. adoConDbf.Open sDbfConString
    3.  
    4. adoRST.Open "File=FILENAME;Index=TAG:INDEXNAME", adoConDbf, adOpenStatic, adLockOptimistic, adCmdTable
    thanks a lot, but it says provider can't be found. can i use other providers ? I think i have following provider installed

    Microsoft OLE DB Provider for ODBC
    Microsoft OLE DB Provider for Microsoft Index Server
    Microsoft OLE DB Provider for Microsoft Active Directory Service
    OLE DB Provider for Microsoft Jet
    Microsoft SQL Server OLE DB Provider
    Microsoft OLE DB Provider for Oracle

  8. #8

  9. #9
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627

    Re: Binding Data Grids & Adding References [RESOLVED]

    You'll have to get the Visual FoxPro ODBC Provider - you can find it here
    Here's to us!
    Who's like us?
    Darned few, and they're all dead!

  10. #10
    New Member
    Join Date
    Feb 2005
    Posts
    7

    Re: Binding Data Grids & Adding References [RESOLVED]

    Quote Originally Posted by demotivater
    You'll have to get the Visual FoxPro ODBC Provider - you can find it here
    thanks for your replies.I installed the Microsoft OLE DB Provider for Visual FoxPro 9.0. I also re-installed my visual basic and visual foxpro but still not working. It keeps giving error message "Provider unknown or may not be installed properly"

  11. #11
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627

    Re: Binding Data Grids & Adding References [RESOLVED]

    Can you post your connection string?
    Here's to us!
    Who's like us?
    Darned few, and they're all dead!

  12. #12
    New Member
    Join Date
    Feb 2005
    Posts
    7

    Re: Binding Data Grids & Adding References [RESOLVED]

    here it is :

    VB Code:
    1. Dim cn As ADODB.Connection
    2. Set cn = New ADODB.Connection
    3.    
    4. cn.Open "Provider=SDEOLEDB;Data Source=C:\DBF\;Mode=Read|Write;SDE OLEDB: RDE Type=FoxPro"
    Last edited by ted_123; Mar 2nd, 2005 at 07:44 PM.

  13. #13
    New Member
    Join Date
    Feb 2005
    Posts
    7

    Resolved Re: Binding Data Grids & Adding References [RESOLVED]

    I used this. and it works !

    VB Code:
    1. cn.Open "Provider=VFPOLEDB.1;Data Source=C:\DBF;Persist Security
    2. Info=False;"

    thanks a lot for your help
    Last edited by ted_123; Mar 2nd, 2005 at 08:39 PM.

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