Results 1 to 9 of 9

Thread: Reading field names from a D-base

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy

    I am writing a little program for myself and would like to be able to pull table name from a database, and then field names from the table. I know how to do this in ASP but I'm relatively new to VB and could really use the help. I knew if there was help to be had, this was the place to get it.
    Thanks,
    Chris

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What type of DB? Are you using ADO or DAO?

    For Access with DAO, use this:
    Code:
    For i = 1 To db.TableDefs(0).Fields.Count
         Debug.Print db.TableDefs(0).Fields(i - 1).Name
    Next
    ...where db is your Database object.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy Using DAO

    Unfortunately you're going to have to assume I know less than you think I do. I understand your code but not how to get to that point, and isn't that just for field names? what about table names? Thanks for the help.
    Thanks,
    Chris

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy I'm further but still confused...

    I connected to the database, and I can iterate through the names, but how do I build a drop down list or a select box out of the values?
    Thanks,
    Chris

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Use:
    Code:
    List1.AddItem db.TableDefs(0).Fields(i - 1).Name
    or similar.

    DAO's objects often have Collection-based properties, for example, under TableDefs, you can use either:
    Code:
    db.TableDefs(0) ' By index
    or
    Code:
    db.TableDefs("MyTable") ' By name
    The same goes for Fields, QueryDefs and so on. Check out the documentation for specifics.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Talking Thank you for your help!

    I'm still not doing it right for some reason because I'm getting values returned that don't exist in my database at all like (data) and (table), but I'll keep playing with it. I know that you have set me on the right track though and so you have the deepest thanks I can offer.
    Thanks,
    Chris

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2000
    Location
    Virginia
    Posts
    55

    Unhappy Database question?

    Why does this:
    Code:
    For i = 1 to db.TableDefs(0).Fields.Count
         List1.AddItem db.TableDefs(0).Fields(i-1).Name
    Next
    NOT return the same as this:
    Code:
    For i = 1 to db.TableDefs("Table1").Fields.Count
         List1.AddItem db.TableDefs("Table1").Fields(i-1).Name
    Next
    Even though table1 is the first table in the database and I thought would equal tabledef(0)? When I use the tabledef(0) I only get half the fields that are in the four field table. I'm very confused, especially since one of the field names returned isn't even in the table! Please help me out.
    Thanks,
    Chris

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    [I get the same for both arguments [field names]]
    
    Option Explicit
    
    Public db As Database
    Public rs As Recordset
    
    Private Sub Command1_Click()
    Dim i As Integer
    
    Set db = Workspaces(0).OpenDatabase("C:\keepers\art.mdb")
          
    For i = 1 To db.TableDefs(0).Fields.Count
         List1.AddItem db.TableDefs(0).Fields(i - 1).Name
    Next
    End Sub
    
    Private Sub Command2_Click()
    Dim i As Integer
    Set db = Workspaces(0).OpenDatabase("C:\keepers\art.mdb")
    
    For i = 1 To db.TableDefs("art").Fields.Count
         List1.AddItem db.TableDefs("art").Fields(i - 1).Name
    Next
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You've probably opened one of the system tables, which store information about the database. In this case, it is always best to use the table name.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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