Solved

ADO and table names?

Posted on 2000-04-11
9
172 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

809 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