Results 1 to 11 of 11

Thread: field names

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    London
    Posts
    99

    Wink

    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.


  2. #2
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696

    Talking

    Hi Frank

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


    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
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    London
    Posts
    99
    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.

  4. #4
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    London
    Posts
    99
    Clunietp,

    I'm using SQL 7

    Where you mention MyField do you mean

    Dim myField as Field

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

  6. #6
    Lively Member
    Join Date
    Mar 1999
    Posts
    93
    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
    Regards,
    Vit

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    London
    Posts
    99

    Talking

    Just found out that THE way to do it is to call sp_columns.

    ie

    exec sp_columns 'tablename'

    in Query Analyzer grid view


    VB6 Enterprise sp5, SQL Server2000

  8. #8
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    I meant that MyField would be your primary key field....but you have figured out another way so forget it, I guess...


    Tom

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    London
    Posts
    99
    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!
    VB6 Enterprise sp5, SQL Server2000

  10. #10
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    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....)

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    London
    Posts
    99

    Talking

    Yeah!

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

    Cheers!
    VB6 Enterprise sp5, SQL Server2000

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