Snailspace
Board Regular
- Joined
- Jan 28, 2009
- Messages
- 56
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com
ffice
ffice" /><o
></o
>
I’m trying to build a list of tables and their corresponding fields, hopefully using excel (2003) rather than querying each table manually and saving it.<o
></o
>
The following code (from http://www.vbaexpress.com/kb/getarticle.php?kb_id=707) looks like it would do what I want if I had access, which unfortunately I do not.<o
></o
>
If anyone could help me tweak this it would be greatly appreciated.<o
></o
>
The following is the recorded VBA from querying to show Database name, where ZPROD is the DSN database and ABNFIELD is the first table.
<o
></o
>
<o
></o
>
<o
>Thanks in advance SP</o
>
I’m trying to build a list of tables and their corresponding fields, hopefully using excel (2003) rather than querying each table manually and saving it.<o
The following code (from http://www.vbaexpress.com/kb/getarticle.php?kb_id=707) looks like it would do what I want if I had access, which unfortunately I do not.<o
If anyone could help me tweak this it would be greatly appreciated.<o
Code:
Option Compare Database
Option Explicit
Sub ListTablesAndFields()
'Macro Purpose: Write all table and field names to and Excel file
Dim lTbl As Long
Dim lFld As Long
Dim dBase As Database
Dim xlApp As Object
Dim wbExcel As Object
Dim lRow As Long
'Set current database to a variable adn create a new Excel instance
Set dBase = CurrentDb
Set xlApp = CreateObject("Excel.Application")
Set wbExcel = xlApp.workbooks.Add
'Set on error in case there is no tables
On Error Resume Next
'Loop through all tables
For lTbl = 0 To dBase.TableDefs.Count
'If the table name is a temporary or system table then ignore it
If Left(dBase.TableDefs(lTbl).Name, 1) = "~" Or _
Left(dBase.TableDefs(lTbl).Name, 4) = "MSYS" Then
'~ indicates a temporary table
'MSYS indicates a system level table
Else
'Otherwise, loop through each table, writing the table and field names
'to the Excel file
For lFld = 1 To dBase.TableDefs(lTbl).Fields.Count - 1
lRow = lRow + 1
With wbExcel.sheets(1)
.range("A" & lRow) = dBase.TableDefs(lTbl).Name
.range("B" & lRow) = dBase.TableDefs(lTbl).Fields(lFld).Name
End With
Next lFld
End If
Next lTbl
'Resume error breaks
On Error GoTo 0
'Set Excel to visible and release it from memory
xlApp.Visible = True
Set xlApp = Nothing
Set wbExcel = Nothing
'Release database object from memory
Set dBase = Nothing
End Sub
The following is the recorded VBA from querying to show Database name, where ZPROD is the DSN database and ABNFIELD is the first table.
Code:
Connection:=”ODBC;DSN=ZPROD;”<o:p></o:p>
“FROM XXX01.ZPROD.ABNFIELD”<o:p></o:p>
<o
<o