Results 1 to 6 of 6

Thread: How to fetch table names

  1. #1

    Thread Starter
    New Member aspguruindia's Avatar
    Join Date
    Oct 2001
    Location
    New Delhi, India
    Posts
    14

    Unhappy How to fetch table names

    Hello everyone,

    I want to fetch names of the tables in a specified .mdb file.
    How can I do that.

    Thanks in advance for helping.
    =========================
    Naveen Bhalla

    CEO - Xfriday Consulting Services

    Its nice to be important, but its important to be nice.
    =========================

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    Using ADO

    VB Code:
    1. Option Explicit
    2. Private cat As ADOX.Catalog
    3. Private tbl As ADOX.Table
    4. Private cnn As New ADODB.Connection
    5.  
    6. Private Sub Command1_Click()
    7.     Set cat = New ADOX.Catalog
    8.     Set tbl = New ADOX.Table
    9.     cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB"
    10.     Set cat.ActiveConnection = cnn
    11.  
    12.     For Each tbl In cat.Tables
    13.         Debug.Print tbl.Name
    14.     Next tbl
    15. End Sub
    -= a peet post =-

  3. #3

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    Using DAO

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim db As Database
    3.     Dim tDef As TableDef
    4.     Dim i As Integer
    5.     Set db = OpenDatabase("C:\test.mdb")
    6.     For Each tDef In db.TableDefs
    7.         Debug.Print "Table : " & vbTab & tDef.Name
    8.         For i = 0 To tDef.Fields.Count - 1
    9.             Debug.Print "Field : " & vbTab & vbTab & tDef.Fields(i).Name
    10.         Next i
    11.     Next tDef
    12. End Sub
    -= a peet post =-

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    peets answer will get you all of the tables, including the hidden system tables. These are tables that you probably don't want to mess with.

    I have one app in which I read in all of the tables in any given .Mdb and display then in a checked list box. But, before the LoadTables sub finished, I remove the hidden system tables from the listbox display, just to be nice and safe. Fortunately, the system tables conveniently start with the same 4 letters, so removing them is very easy.
    VB Code:
    1. For i = lstDbTables.ListCount - 1 To 0 Step -1
    2.         If lstDbTables.List(i) Like "MSys*" Then
    3.             lstDbTables.RemoveItem i
    4.         End If
    5.     Next
    lstDbTables is my list control, and i is Dimmed As Long (did you ever wonder why i is not a VB reservered word permently dimension as a Long? IT SHOULD BE!

  6. #6
    State Guy
    Guest
    Looks good. But I'm looking for the way to put field names from a table into a list box. I'm using VB 6 with unbound data controls and a dataenvironment. I'm groping in the dark here, being thrown into the VB deep end.

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