Results 1 to 2 of 2

Thread: VB - Retrieve Table/Field Schema

  1. #1

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    VB - Retrieve Table/Field Schema

    VB Code:
    1. Option Explicit
    2.  
    3. Dim cnTables As ADODB.Connection
    4. Dim rsTables As ADODB.Recordset
    5. Dim rsFields As ADODB.Recordset
    6. Dim strCn As String
    7. Dim strSQL As String
    8. Dim i As Integer
    9.  
    10. 'Requires ADO reference
    11. 'Written by Carl R. Armbruster
    12. 'Free to use - it's yours!
    13.  
    14. Private Sub Form_Load()
    15.     'database connnection string
    16.     strCn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    17.             "Persist Security Info=False;" & _
    18.             "Data Source=" & App.Path & "\Northwind.mdb"
    19. End Sub
    20. Private Sub Command1_Click()
    21.     'recordset of field names
    22.     Set rsFields = New ADODB.Recordset
    23.     'connection to recieve tabel schema
    24.     Set cnTables = New ADODB.Connection
    25.     'open connection
    26.     cnTables.Open strCn
    27.    
    28.     'get table schema
    29.     Set rsTables = cnTables.OpenSchema(adSchemaTables)
    30.     'loop through table recordset
    31.     Do While Not rsTables.EOF
    32.         'only get the tables (table schema also retrieves views)
    33.         If rsTables!TABLE_TYPE = "TABLE" Then
    34.             'get an empty recordset(1 never equals 2!)
    35.             strSQL = "SELECT * FROM [" & rsTables!TABLE_NAME & "] WHERE 1 = 2"
    36.             rsFields.Open strSQL, strCn, adOpenStatic, adLockReadOnly
    37.             'loop through the field collection
    38.             For i = 0 To rsFields.Fields.Count - 1
    39.                 Debug.Print "Table: " & rsTables!TABLE_NAME & "; Field: " & rsFields.Fields(i).Name
    40.             Next i
    41.             rsFields.Close
    42.         End If
    43.         rsTables.MoveNext
    44.     Loop
    45.    
    46.     'close and cleanup
    47.     rsTables.Close
    48.     Set rsTables = Nothing
    49.    
    50.     cnTables.Close
    51.     Set cnTables = Nothing
    52.    
    53.     Set rsFields = Nothing
    54. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  2. #2
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Let me add something to it. Use this view and it will give you all
    columns and there attributes information like length, column
    name, size, precision, scale blah blah blah ... My automated
    validation is based on this view which allows me to validate my
    textboxes on the fly. Moreover, you can pass the table name to it to get the individual table elements information.


    VB Code:
    1. CREATE VIEW dbo.Table_Cols AS SELECT a.name AS tablename,
    2. b.name AS columnname, c.name AS col_type_name, b.length AS
    3. Length, b.prec AS Prec, b.scale AS Scale, c.xtype AS col_type_id
    4. FROM dbo.sysobjects a INNER JOIN dbo.syscolumns b ON a.id =
    5. b.id INNER JOIN dbo.systypes c ON b.xtype = c.xtype

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