[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Listing Access Tables Through VBA?

Posted on 2011-09-19
9
Medium Priority
?
424 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

649 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