Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ADO and table names?

Posted on 2000-04-11
9
Medium Priority
?
185 Views
Last Modified: 2013-11-23
Via visual basic code, I need to be able the dynamicly get the names of the tables in a database.

I was able to get the field names of a recordset easy enough, but I want table names within a connection.

ie.     Order_Header_CN.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & strPath & "Order_Header.mdb", there are three tables within this database, how can list them out, via VB code??
0
Comment
Question by:Dean_Reedy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 2

Expert Comment

by:DawsonB
ID: 2704705
Select*
from Sysobjects
where
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2704723
Oops, prssed TAB then enter accidentally, andyhow, what I was trying to type was:-
with your connection already established, do the following

dim rs as new ADODB.Recordset
dim sql as string

sql = "SELECT Name FROM sysobjects WHERE type = 'U'"
rs.Open sql,cn


rs will now be populated with the names of the User databases in your db

Hope this helps Dean
 
0
 
LVL 1

Author Comment

by:Dean_Reedy
ID: 2704768
I tried it, but I get an error "micrsoft database jet cannoot find table or 'sysobjects' query......

I tried this:
Dim Order_Header_CN As New ADODB.Connection
Dim rs As New ADODB.Recordset
 Dim sql As StringOrder_Header_CN.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & strPath & "Order_Header.mdb"
    sql = "SELECT Name FROM sysobjects WHERE type = 'U'"
    rs.Open sql, Order_Header_CN
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2704779
A better solution that sysObjects is to use the OpenSchema method. This is the example from the ADO help file:
Public Sub OpenSchemaX()  
Dim cnn1 As ADODB.Connection
Dim rstSchema As ADODB.Recordset  
Dim strCnn As String      

   Set cnn1 = New ADODB.Connection
   strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   cnn1.Open strCnn        
   Set rstSchema = cnn1.OpenSchema(adSchemaTables)  
   Do Until rstSchema.EOF
      Debug.Print "Table name: " & _
         rstSchema!TABLE_NAME & vbCr & _
         "Table type: " & rstSchema!TABLE_TYPE & vbCr      rstSchema.MoveNext
   Loop  
   rstSchema.Close      
   cnn1.Close  
End Sub
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2704900
Dean:
 Sorry, should have seen the Jet bit in the connection string, my query will work in SQL server, don't know in Access.
emoreau: I've not used OpenSchema before, but it looks very interesting, I'll have to have a look.


0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2704965
Thanks emoreau, I'll DEFINATLY be using that!
your debug.print didn't work for me, though, so I changed it to this below, I've just included it in case Dean had the same problem and couldn't find his way around.
        Do Until rstSchema.EOF
     
        Debug.Print "Table name: " & rstSchema("TABLE_NAME") & _
            vbCr & "Table type: " & rstSchema("TABLE_TYPE") & vbCr
         rstSchema.MoveNext
        Loop

cool
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 200 total points
ID: 2705097
What do you mean by «had the same problem and couldn't find his way around»? Which problem?

Here is the code I just test and it works perfectly!
Private Sub Command1_Click()
Dim cnn1 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String

    Set cnn1 = New ADODB.Connection
    strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Microsoft Visual Studio\VB98\Nwind.mdb;Persist Security Info=False"
    cnn1.Open strCnn
    Set rstSchema = cnn1.OpenSchema(adSchemaTables)
    Do Until rstSchema.EOF
        Debug.Print "Table name: " & rstSchema("TABLE_NAME") & _
            vbCr & "Table type: " & rstSchema("TABLE_TYPE") & vbCr
        rstSchema.MoveNext
    Loop
    rstSchema.Close
    cnn1.Close
End Sub
0
 
LVL 1

Author Comment

by:Dean_Reedy
ID: 2705161
Thanks to everyone!
Dean-Iowa
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2706879
What I mean by had the same problem etc is this:..
 this bit cut from your original suggestion,
      Debug.Print "Table name: " & _
         rstSchema!TABLE_NAME & vbCr & _
         "Table type: " & rstSchema!TABLE_TYPE & vbCr      

And from

My rewrite

Debug.Print "Table name: " & rstSchema("TABLE_NAME") & _
            vbCr & "Table type: " & rstSchema("TABLE_TYPE") & vbCr
         

recordset!Table_Name
is different to
recordset("Table_Name")

:-}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question