Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Listing Access Table Columns Through VBA?

Posted on 2011-09-19
11
Medium Priority
?
344 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
[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
  • 5
  • 5
11 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

721 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