-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_acc_SchemaTableIntoExcel.bas
executable file
·146 lines (123 loc) · 5.8 KB
/
mod_acc_SchemaTableIntoExcel.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Attribute VB_Name = "mod_acc_SchemaTableIntoExcel"
Option Compare Database
Option Explicit
' mod_acc_SchemaTableIntoExcel
' 130927.AMG cribbed from forum and allen browne site
' credit > http://www.access-programmers.co.uk/forums/showpost.php?p=1036536&postcount=10
Sub ListTablesAndFields()
'Macro Purpose: Write all table and field names to and Excel file
'Source: vbaexpress.com/kb/getarticle.php?kb_id=707
'Updates by Derek - Added column headers, modified base setting for loop to include all fields,
' added type, size, and description properties to export
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 are no tables
On Error Resume Next
'DJK 2011/01/27 - Added in column headers below
lRow = 1
With wbExcel.sheets(1)
.Range("A" & lRow) = "Table Name"
.Range("B" & lRow) = "Field Name"
.Range("C" & lRow) = "Type"
.Range("D" & lRow) = "Size"
.Range("E" & lRow) = "Description"
End With
'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 = 0 To dBase.TableDefs(lTbl).Fields.Count - 1 'DJK 2011/01/27 - Changed initial base from 1 to 0, and added type, size, and description
lRow = lRow + 1
With wbExcel.sheets(1)
.Range("A" & lRow) = dBase.TableDefs(lTbl).Name
.Range("B" & lRow) = dBase.TableDefs(lTbl).Fields(lFld).Name
.Range("C" & lRow) = FieldTypeName(dBase.TableDefs(lTbl).Fields(lFld))
.Range("D" & lRow) = dBase.TableDefs(lTbl).Fields(lFld).Size
.Range("E" & lRow) = GetDescrip(dBase.TableDefs(lTbl).Fields(lFld))
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
Function FieldTypeName(fld As DAO.Field) As String
'Purpose: Converts the numeric results of DAO Field.Type to text.
' credit > http://allenbrowne.com/func-06.html
Dim strReturn As String 'Name to return
Select Case CLng(fld.Type) 'fld.Type is Integer, but constants are Long.
Case dbBoolean: strReturn = "Yes/No" ' 1
Case dbByte: strReturn = "Byte" ' 2
Case dbInteger: strReturn = "Integer" ' 3
Case dbLong ' 4
If (fld.Attributes And dbAutoIncrField) = 0& Then
strReturn = "Long Integer"
Else
strReturn = "AutoNumber"
End If
Case dbCurrency: strReturn = "Currency" ' 5
Case dbSingle: strReturn = "Single" ' 6
Case dbDouble: strReturn = "Double" ' 7
Case dbDate: strReturn = "Date/Time" ' 8
Case dbBinary: strReturn = "Binary" ' 9 (no interface)
Case dbText '10
If (fld.Attributes And dbFixedField) = 0& Then
strReturn = "Text"
Else
strReturn = "Text (fixed width)" '(no interface)
End If
Case dbLongBinary: strReturn = "OLE Object" '11
Case dbMemo '12
If (fld.Attributes And dbHyperlinkField) = 0& Then
strReturn = "Memo"
Else
strReturn = "Hyperlink"
End If
Case dbGUID: strReturn = "GUID" '15
'Attached tables only: cannot create these in JET.
Case dbBigInt: strReturn = "Big Integer" '16
Case dbVarBinary: strReturn = "VarBinary" '17
Case dbChar: strReturn = "Char" '18
Case dbNumeric: strReturn = "Numeric" '19
Case dbDecimal: strReturn = "Decimal" '20
Case dbFloat: strReturn = "Float" '21
Case dbTime: strReturn = "Time" '22
Case dbTimeStamp: strReturn = "Time Stamp" '23
'Constants for complex types don't work prior to Access 2007 and later.
Case 101&: strReturn = "Attachment" 'dbAttachment
Case 102&: strReturn = "Complex Byte" 'dbComplexByte
Case 103&: strReturn = "Complex Integer" 'dbComplexInteger
Case 104&: strReturn = "Complex Long" 'dbComplexLong
Case 105&: strReturn = "Complex Single" 'dbComplexSingle
Case 106&: strReturn = "Complex Double" 'dbComplexDouble
Case 107&: strReturn = "Complex GUID" 'dbComplexGUID
Case 108&: strReturn = "Complex Decimal" 'dbComplexDecimal
Case 109&: strReturn = "Complex Text" 'dbComplexText
Case Else: strReturn = "Field type " & fld.Type & " unknown"
End Select
FieldTypeName = strReturn
End Function
Function GetDescrip(obj As Object) As String
On Error Resume Next
GetDescrip = obj.Properties("Description")
End Function