Solved

Listing Access Table Columns Through VBA?

Posted on 2011-09-19
11
330 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
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.

 

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
 

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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

821 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