Solved

Listing Access Table Columns Through VBA?

Posted on 2011-09-19
11
323 Views
Last Modified: 2012-06-27
Hello EE!

I recently got some help with listing tables.

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

Open in new window


But how do I go about listing all columns in a table? For example table "Data" and is there any manual or such where i can learn more about using this sort of code?

Thanks in advance!

0
Comment
Question by:ThomasFoege
  • 5
  • 5
11 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36559813
I'd still recommend DAO for Access:
    Dim DAOws As DAO.Workspace
    Dim DAOdb As DAO.Database
    Dim tdef As DAO.TableDef
    Dim fld As DAO.Field
    Dim strDBFile As String
   
      strDBFile = "H:\MyDatabase.accdb"
    Set DAOws = DBEngine.Workspaces(0)
    Set DAOdb = DAOws.OpenDatabase(strDBFile, False, True)
    For Each tdef In DAOdb.TableDefs
      If left$(LCase$(tdef.Name), 4) <> "msys" Then
         MsgBox tdef.Name
         For Each fld In tdef.Fields
            MsgBox fld.Name
         Next fld
      End If
   Next tdef
   DAOdb.Close
   DAOws.Close

Open in new window

0
 

Author Comment

by:ThomasFoege
ID: 36559848
Cool!

Whats the difference between ADODB and DAO?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36559858
Different technologies doing much the same thing. DAO is what Access uses natively, I believe, and seems to me to be easier to work with for Access, as a rule. (especially for parameter queries, for some reason).
0
 

Author Comment

by:ThomasFoege
ID: 36559917
Sub ListTbls()

    Dim DAOws As DAO.Workspace
    Dim DAOdb As DAO.Database
    Dim tdef As DAO.TableDef
    Dim fld As DAO.Field
    Dim strDBFile As String
   
    strDBFile = "C:\Database.accdb" 'ActiveSheet.Range("D4").Value
    Set DAOws = DBEngine.Workspaces(0)
    Set DAOdb = DAOws.OpenDatabase(strDBFile, False, True)
    For Each tdef In DAOdb.TableDefs
      If Left$(LCase$(tdef.Name), 4) <> "msys" Then
         MsgBox tdef.Name
         For Each fld In tdef.Fields
            MsgBox fld.Name
         Next fld
      End If
   Next tdef
   DAOdb.Close
   DAOws.Close
   
End Sub

Open in new window



I tried using your code, i get an error either in line
   
Set DAOdb = DAOws.OpenDatabase(strDBFile, False, True)
    For Each tdef In DAOdb.TableDefs

Open in new window


Runetime error 3343, application-defined or object-defined error

Did i miss something?

Thanks again!
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36559924
What references do you have set?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ThomasFoege
ID: 36559932
Visual Basic for applications
microsoft excel 14 object lib
ole automation
microsoft office 14 object lib
microsoft DAO 3.6 object lib
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36559974

try this simple version


Dim dbs As DAO.Database
 
Dim tdf As DAO.TableDef, fld As DAO.Field
 
Set dbs = DAO.OpenDatabase("C:\Database.accdb")
    For Each tdf In dbs.TableDefs
        If Left(tdf.Name, 4) <> "msys" Then
            For Each fld In tdf.Fields
            Debug.Print tdf.Name, fld.Name
            Next
 
        End If
    Next

dbs.Close

Open in new window

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36560013
You need the 'Microsoft Office 12.0 Access database engine objects' reference rather than DAO3.6
0
 

Author Comment

by:ThomasFoege
ID: 36560039
Perfect!

Is there any site that has a "beginners guide" to DAO?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36560079
I couldn't tell you to be honest. There's a guide to the model here: http://msdn.microsoft.com/en-us/library/ee291983(v=office.12).aspx
and you might find this page useful (though it's VB6 rather than VBA): http://www.vb6.us/tutorials/using-dao-data-access-objects-code-tutorial
0
 

Author Closing Comment

by:ThomasFoege
ID: 36560375
Perfect as always!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

757 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

20 Experts available now in Live!

Get 1:1 Help Now