|
-
Sep 16th, 2000, 01:11 AM
#1
Thread Starter
Member
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.
-
Sep 16th, 2000, 01:17 AM
#2
Monday Morning Lunatic
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
-
Sep 16th, 2000, 01:33 AM
#3
Thread Starter
Member
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.
-
Sep 16th, 2000, 01:55 AM
#4
Thread Starter
Member
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?
-
Sep 16th, 2000, 09:03 AM
#5
Monday Morning Lunatic
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
-
Sep 16th, 2000, 10:36 AM
#6
Thread Starter
Member
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.
-
Sep 17th, 2000, 02:18 PM
#7
Thread Starter
Member
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.
-
Sep 17th, 2000, 02:41 PM
#8
_______
<?>
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
-
Sep 17th, 2000, 03:18 PM
#9
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|