Results 1 to 4 of 4

Thread: Database tables and their fields

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Wellington, NZ
    Posts
    267

    Database tables and their fields

    Hi

    I'm using an Access database with ADODB.

    I don't have any collection that will allow me to get the names of the tables in the database, and their fields.

    I don't care if I have to open it a different way, then close it and re-open as I do now. I just want a list of tables and fields in the database.

    Thanks
    Robert

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Database tables and their fields

    Put two listboxes on your form (one for table names, and then when a table is selected, the fields will be displayed in the other.)
    VB Code:
    1. 'NOTE:  A reference to Microsoft ActiveX Data Objects, and Microsoft ADO 'Ext X.X for DDL and Security must be made
    2. 'This makes use of two ListBoxes.   lstTables displays all tables in the
    3. 'selected database.   lstFieldStats lists the individually selected field
    4. '(from lstTables) statistics
    5. Private ADOCn As ADODB.Connection
    6. Private ConnString As String
    7. Private MyCat As ADOX.Catalog
    8. Private MyTable As ADOX.Table
    9.  
    10. Private Sub ListTables(DBName As String)
    11.  
    12. ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    13.         "Data Source=" & DBName & ";Persist Security Info=False"
    14. Set ADOCn = New ADODB.Connection
    15. ADOCn.ConnectionString = ConnString
    16. ADOCn.Open ConnString
    17.  
    18. lstTables.Clear
    19.  
    20. Set MyCat = New ADOX.Catalog
    21. MyCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    22.  "Data Source=" & DBName & ";"
    23.  
    24.  For Each MyTable In MyCat.Tables
    25.     'exclude VIEWS
    26.     If MyTable.Type <> "VIEW" Then
    27.         'exclude system tables
    28.         If Left$(MyTable.Name, 4) <> "MSys" Then lstTables.AddItem MyTable.Name
    29.     End If
    30.  Next
    31. Set MyCat = Nothing
    32.  
    33. End Sub
    34.  
    35. Private Sub Command1_Click()
    36. ListTables "c:\program files\mydb.mdb"
    37. End Sub
    38.  
    39. Private Sub lstTables_Click()
    40. Dim MyField As Field
    41. Dim rs As ADODB.Recordset
    42. Dim SQL As String
    43. lstFieldStats.Clear
    44.  
    45. Set rs = New ADODB.Recordset
    46.  
    47. SQL = "SELECT * FROM " & lstTables.List(lstTables.ListIndex)
    48. rs.Open SQL, ADOCn, adOpenForwardOnly, adLockOptimistic
    49.       For Each MyField In rs.Fields
    50.           lstFieldStats.AddItem "Name = " & MyField.Name & " Size = " & MyField.DefinedSize
    51.       Next
    52. rs.Close
    53. Set rs = Nothing
    54. End Sub

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Database tables and their fields

    Quote Originally Posted by Hack
    Put two listboxes on your form (one for table names, and then when a table is selected, the fields will be displayed in the other.)
    VB Code:
    1. 'NOTE:  A reference to Microsoft ActiveX Data Objects, and Microsoft ADO 'Ext X.X for DDL and Security must be made
    2. 'This makes use of two ListBoxes.   lstTables displays all tables in the
    3. 'selected database.   lstFieldStats lists the individually selected field
    4. '(from lstTables) statistics
    5. Private ADOCn As ADODB.Connection
    6. Private ConnString As String
    7. Private MyCat As ADOX.Catalog
    8. Private MyTable As ADOX.Table
    9.  
    10. Private Sub ListTables(DBName As String)
    11.  
    12. ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    13.         "Data Source=" & DBName & ";Persist Security Info=False"
    14. Set ADOCn = New ADODB.Connection
    15. [B]ADOCn.ConnectionString = ConnString
    16. ADOCn.Open ConnString
    17. [/B]
    18. lstTables.Clear
    19.  
    20. Set MyCat = New ADOX.Catalog
    21. MyCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    22.  "Data Source=" & DBName & ";"
    23.  
    24.  For Each MyTable In MyCat.Tables
    25.     'exclude VIEWS
    26.     If MyTable.Type <> "VIEW" Then
    27.         'exclude system tables
    28.         If Left$(MyTable.Name, 4) <> "MSys" Then lstTables.AddItem MyTable.Name
    29.     End If
    30.  Next
    31. Set MyCat = Nothing
    32.  
    33. End Sub
    34.  
    35. Private Sub Command1_Click()
    36. ListTables "c:\program files\mydb.mdb"
    37. End Sub
    38.  
    39. Private Sub lstTables_Click()
    40. Dim MyField As Field
    41. Dim rs As ADODB.Recordset
    42. Dim SQL As String
    43. lstFieldStats.Clear
    44.  
    45. Set rs = New ADODB.Recordset
    46.  
    47. SQL = "SELECT * FROM " & lstTables.List(lstTables.ListIndex)
    48. rs.Open SQL, ADOCn, adOpenForwardOnly, adLockOptimistic
    49.       For Each MyField In rs.Fields
    50.           lstFieldStats.AddItem "Name = " & MyField.Name & " Size = " & MyField.DefinedSize
    51.       Next
    52. rs.Close
    53. Set rs = Nothing
    54. End Sub
    Just intrigued, if you have already specified the connectionstring then why are you still using it as a parameter in the Open method?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Database tables and their fields

    RobertLees,

    Take a look in my signature for Multi Database Connection. The project will give you an idea of how to do it.

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