Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Listing Access Table Columns Through VBA?

Posted on 2011-09-19
11
Medium Priority
?
347 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 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

876 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