Solved

ADO and table names?

Posted on 2000-04-11
9
170 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert GUI app into console app for Win32 Env 5 81
using Access 8 59
MS Access Search and Replace Using VBA 6 57
Problem to With line 4 42
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now