Solved

ADO and table names?

Posted on 2000-04-11
9
168 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
  • 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
 
LVL 69

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 69

Accepted Solution

by:
Éric Moreau earned 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now