Solved

Listing Access Tables Through VBA?

Posted on 2011-09-19
9
391 Views
Last Modified: 2012-05-12
Hello EE!

I'm trying to do a macro that lists all tables in a accessdb, so far I've got to this which doesnt work:


Sub ListTbls()
   
   Dim cnn As ADODB.Connection
   'Dim myTable As ADODB.Table

   Set cnn = New ADODB.Connection
   
   With cnn
      .CursorLocation = adUseServer
      .ConnectionTimeout = 500
      .Provider = "Microsoft.ACE.OLEDB.12.0"
      .ConnectionString = "Data Source=C:\Database.accdb"
      .Open
      .CommandTimeout = 500
   End With
   

   For Each myTable In cnn.Tables
      If myTable.Type <> "VIEW" And _
          myTable.Type <> "SYSTEM TABLE" And _
          myTable.Type <> "ACCESS TABLE" Then MsgBox myTable.Name
   Next myTable
   Set cnn = Nothing

End Sub

Open in new window


I get an error on "For Each myTable In cnn.Tables". Am i missing something here?

What I'll be trying next is once a table has been selected that all columns in that table is shown

Does anyone have any suggestions to how to fix this?

Thank you in advance!
- Thomas
0
Comment
Question by:ThomasFoege
[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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36559234
Sub ListTbls()
   
   Dim cnn As ADODB.Connection

   Set cnn = New ADODB.Connection
   
   With cnn
      .CursorLocation = adUseServer
      .ConnectionTimeout = 500
      .Provider = "Microsoft.ACE.OLEDB.12.0"
      .ConnectionString = "Data Source=C:\Database.accdb"
      .Open
      .CommandTimeout = 500
   End With
   
Dim rsSchema As New ADODB.Recordset
Set rsSchema = cn.OpenSchema(adSchemaTables)

rsSchema.MoveFirst
With rsSchema
While Not .EOF
If .Fields("TABLE_TYPE") = "TABLE" Then
MsgBox  .Fields("TABLE_NAME")
End If
.MoveNext
Wend
   Set cnn = Nothing

End Sub

refer
http://www.xtremevbtalk.com/showthread.php?threadid=69391
0
 

Author Comment

by:ThomasFoege
ID: 36559247
Thanks! I get a "Expected End With" when i run your code?

If i insert that just below Wend, i get a "Object Required"?
0
 
LVL 2

Expert Comment

by:UnicornBoy
ID: 36559252
as far as this page shows: http://www.w3schools.com/ado/ado_ref_connection.asp
ADODB.connection does not have a Tables property to use!
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 36559256
tryt his

Sub ListTbls()
   
   Dim cnn As ADODB.Connection

   Set cnn = New ADODB.Connection
   
   With cnn
      .CursorLocation = adUseServer
      .ConnectionTimeout = 500
      .Provider = "Microsoft.ACE.OLEDB.12.0"
      .ConnectionString = "Data Source=C:\Database.accdb"
      .Open
      .CommandTimeout = 500
   End With
   
Dim rsSchema As New ADODB.Recordset
Set rsSchema = cnn.OpenSchema(adSchemaTables)

rsSchema.MoveFirst
With rsSchema
While Not .EOF
If .Fields("TABLE_TYPE") = "TABLE" Then
MsgBox  .Fields("TABLE_NAME")
End If
.MoveNext
Wend
End With

   Set cnn = Nothing

End Sub

refer
http://www.xtremevbtalk.com/showthread.php?threadid=69391
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36559270
Give this a try...

Sub ListTbls()
   
   Dim db As Database
   Dim myTable As TableDef

   Set db = OpenDatabase("C:\Database.accdb")


   For Each myTable In db.TableDefs
    If Left(myTable.Name, 4) <> "mSys" Then
       msgbox myTable.Name
    End If
   Next myTable
set db = nothing
End Sub

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36559277
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36559283
Just realized you're working from Excel, not Access.  My code will run from Access but not Excel - sorry.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36559285
NOT FOR POINTS

Just an FYI re mbizup's code: you need to set a reference to the Microsoft Office 12.0 (or 14.0 for 2010) Access database engine objects' library as it uses DAO rather than ADO.
0
 

Author Closing Comment

by:ThomasFoege
ID: 36559554
Works perfectly! Thanks a lot!

Is there anyway to list all the columns once a table has been chosen btw?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

726 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