Solved

Listing Access Tables Through VBA?

Posted on 2011-09-19
9
318 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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 61

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
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
Comment Utility
Works perfectly! Thanks a lot!

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

743 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