PDA

Click to See Complete Forum and Search --> : field names


frank ashley
Jun 11th, 2000, 02:52 PM
I'm populating a treeview using table/field names.

The table names I've done using sysobjects but what I need is for VB to pass the table name to SQL and to return all the field names for that table.

Ianpbaker
Jun 11th, 2000, 03:22 PM
Hi Frank

If you are using ADO You can use this piece of code




strSQL = "SELECT * FROM " & yourtable

myrecordset.Open strSQl,Myconnection

Iflag = myrecordset.fields.count - 1

For i = 0 to Iflag
myvariable = myrecordset.fields(i).name
' Enter code here to put it into your list view
Next



Hope This Helps

Ian

frank ashley
Jun 11th, 2000, 04:43 PM
Ian,

Thought of that. Problem is that that will bring back all the data as well. I've got some tables in excess of 100K records.

I was hoping for some kind of schema object somewhere/anywhere.

Clunietp
Jun 11th, 2000, 10:17 PM
what database are you using, frank?

you could also use

Select * from MyTable where MyField = -1

if your MyField field is an autonumber/pri key (or other indexed field), you'll just return the table structure

HTH

Tom

frank ashley
Jun 12th, 2000, 03:15 PM
Clunietp,

I'm using SQL 7

Where you mention MyField do you mean

Dim myField as Field

"Select * from " & strMyTable & " where " & MyField & "= -1"

Vit
Jun 12th, 2000, 09:35 PM
OK, if you're using treeview control think of filling each branch on node.expand event. I mean that when you initialize the tree you fill only the first-level nodes. When you expand a specific node you fill it's child nodes and so on. In this way you don't have to bring all the records at once and the things work much faster

frank ashley
Jun 12th, 2000, 09:39 PM
Just found out that THE way to do it is to call sp_columns.

ie

exec sp_columns 'tablename'

in Query Analyzer grid view

Clunietp
Jun 13th, 2000, 12:02 AM
I meant that MyField would be your primary key field....but you have figured out another way so forget it, I guess...


Tom

frank ashley
Jun 14th, 2000, 01:34 AM
Thanks for your reply Clunietp but the problem was that I wouldn't know the names of either the tables or the fields in advance!

Clunietp
Jun 14th, 2000, 10:50 AM
gotcha

you can also use ADOX to retrieve database info as well if you don't want to tie yourself to SQL Server (but I doubt that the other drivers support this functionality, except maybe the Jet 4.0 provider....)

frank ashley
Jun 15th, 2000, 12:54 AM
Yeah!

Only found out about a way using ADO after the event.

Cheers!