1
|
Option Compare Database
|
2
|
Option Explicit
|
3
|
|
4
|
|
5
|
''This functionality requires a set of tables that stores the values of the closed lists in the current database
|
6
|
''the tables are named like: aux_table_field where table is the name of the table and field is the name of the field that has a closed list
|
7
|
''There must also exist a Z_FieldDescription table in the database which documents which fields have closed lists
|
8
|
''As well as a query Z_FieldDescriptionOrd, which includes a table: misc_SQL_2AccTypes which translates "access" data types into "SQL" types
|
9
|
Function checkAux_tbls()
|
10
|
'checks that aux_ tables have corresponding fields which access them in the Z_Field table
|
11
|
''and writes XML doc that describes the closed lists for vegBank
|
12
|
Dim tdfCheck As Object
|
13
|
Dim dbs As Object
|
14
|
Set dbs = CurrentDb
|
15
|
Dim strTbl As String, strFld As String, strTbl_Fld As String
|
16
|
Dim cnnLocal As Connection
|
17
|
Set cnnLocal = CurrentProject.Connection
|
18
|
Dim rstCurr As New ADODB.Recordset
|
19
|
|
20
|
Dim fs2 As Object
|
21
|
Dim objWritefile As Object
|
22
|
Dim objWriteRels As Object
|
23
|
Set fs2 = CreateObject("Scripting.FileSystemObject")
|
24
|
Dim strValues As String
|
25
|
'creates file for writing XML
|
26
|
Set objWritefile = fs2.CreateTextFile("C:\temp\closedLists.xml", True)
|
27
|
Set objWriteRels = fs2.CreateTextFile("C:\temp\closedRelSQL.sql", True)
|
28
|
'write header for XML
|
29
|
objWritefile.writeLine "<!-- created with MS ACCESS VBA by Michael Lee " & Now() & "-->"
|
30
|
objWritefile.writeLine "<tableDefns>"
|
31
|
Dim intCount As Integer
|
32
|
intCount = 0
|
33
|
For Each tdfCheck In dbs.tabledefs
|
34
|
If Left(tdfCheck.Name, 4) = "aux_" Then
|
35
|
strTbl_Fld = Right(tdfCheck.Name, Len(tdfCheck.Name) - 4)
|
36
|
If InStr(strTbl_Fld, "_") = 0 Then
|
37
|
If strTbl_Fld <> "Aux_Role" Then ''aux_role just is different
|
38
|
MsgBox "no '_' in field name, error! " & tdfCheck.Name
|
39
|
End If
|
40
|
Else 'has "_"
|
41
|
strTbl = Left(strTbl_Fld, InStr(strTbl_Fld, "_") - 1)
|
42
|
strFld = Right(strTbl_Fld, Len(strTbl_Fld) - InStr(strTbl_Fld, "_"))
|
43
|
'Debug.Print strTbl_Fld
|
44
|
rstCurr.Open "SELECT tableName, FieldName, SQLtype, FieldSize FROM Z_FieldDescriptionORD WHERE tableName=""" & strTbl & _
|
45
|
""" AND fieldName = """ & strFld & """;", cnnLocal, , , adCmdText
|
46
|
Dim strDTFull As String
|
47
|
strDTFull = ""
|
48
|
'start SQL for this table
|
49
|
Dim strSQLref As String
|
50
|
intCount = intCount + 1
|
51
|
objWriteRels.writeLine "ALTER TABLE [" & strTbl & "]"
|
52
|
objWriteRels.writeLine " ADD CONSTRAINT auxRel_" & intCount & strTbl_Fld & " FOREIGN KEY ([" & strFld & "]) "
|
53
|
objWriteRels.writeLine " REFERENCES [" & tdfCheck.Name & "] (values); "
|
54
|
objWriteRels.writeLine ""
|
55
|
|
56
|
'start XML for this table:
|
57
|
objWritefile.writeLine " <list>"
|
58
|
objWritefile.writeLine " <sourceTableName>" & tdfCheck.Name & "</sourceTableName>"
|
59
|
objWritefile.writeLine " <TableName>" & strTbl & "</TableName>"
|
60
|
objWritefile.writeLine " <FieldName>" & strFld & "</FieldName>"
|
61
|
If rstCurr.EOF And rstCurr.BOF Then
|
62
|
'no record matches
|
63
|
Debug.Print strTbl_Fld & " has no records"
|
64
|
Else
|
65
|
'matches, write XML for type
|
66
|
|
67
|
If rstCurr!SQLType = "varchar" Then
|
68
|
'add length
|
69
|
strDTFull = "varchar (" & rstCurr!FieldSize & ")"
|
70
|
Else
|
71
|
strDTFull = rstCurr!SQLType
|
72
|
End If
|
73
|
objWritefile.writeLine " <tableDataType>" & strDTFull & "</tableDataType>"
|
74
|
End If
|
75
|
|
76
|
|
77
|
|
78
|
rstCurr.Close
|
79
|
'open actual table to get values and print to XML
|
80
|
rstCurr.Open tdfCheck.Name, cnnLocal, , , adCmdTable
|
81
|
|
82
|
|
83
|
Do Until rstCurr.EOF
|
84
|
strValues = rstCurr!values
|
85
|
'vars for description and sort order
|
86
|
Dim strDesc As String, lngSort As Long
|
87
|
If fieldExistOnTbl("ValueDescription", tdfCheck.Name) Then
|
88
|
strDesc = Nz(rstCurr!ValueDescription, "")
|
89
|
Else
|
90
|
strDesc = ""
|
91
|
End If
|
92
|
If fieldExistOnTbl("SortOrd", tdfCheck.Name) Then
|
93
|
lngSort = Nz(rstCurr!SortOrd, 0)
|
94
|
Else
|
95
|
lngSort = 0
|
96
|
End If
|
97
|
If InStr(strValues, "&") > 0 Then
|
98
|
'must replace & with "&", avoid recursive loop by replacing first with @amp;@
|
99
|
strValues = substTextForText(strValues, "&", "@amp;@")
|
100
|
'replace temp @amp;@ with &
|
101
|
strValues = substTextForText(strValues, "@amp;@", "&")
|
102
|
End If
|
103
|
If InStr(strValues, "<") > 0 Then
|
104
|
'must replace < with "<"
|
105
|
strValues = substTextForText(strValues, "<", "<")
|
106
|
End If
|
107
|
If InStr(strValues, ">") > 0 Then
|
108
|
'must replace < with "<"
|
109
|
strValues = substTextForText(strValues, ">", ">")
|
110
|
End If
|
111
|
''--description
|
112
|
If InStr(strDesc, "&") > 0 Then
|
113
|
'must replace & with "&", avoid recursive loop by replacing first with @amp;@
|
114
|
strDesc = substTextForText(strDesc, "&", "@amp;@")
|
115
|
'replace temp @amp;@ with &
|
116
|
strDesc = substTextForText(strDesc, "@amp;@", "&")
|
117
|
End If
|
118
|
If InStr(strDesc, "<") > 0 Then
|
119
|
'must replace < with "<"
|
120
|
strDesc = substTextForText(strDesc, "<", "<")
|
121
|
End If
|
122
|
If InStr(strDesc, ">") > 0 Then
|
123
|
'must replace < with "<"
|
124
|
strDesc = substTextForText(strDesc, ">", ">")
|
125
|
End If
|
126
|
objWritefile.writeLine " <record>"
|
127
|
objWritefile.writeLine " <values>" & strValues & "</values>"
|
128
|
objWritefile.writeLine " <valueDescription>" & strDesc & "</valueDescription>"
|
129
|
objWritefile.writeLine " <sortOrd>" & lngSort & "</sortOrd>"
|
130
|
objWritefile.writeLine " </record>"
|
131
|
rstCurr.MoveNext
|
132
|
Loop
|
133
|
objWritefile.writeLine " </list>"
|
134
|
|
135
|
rstCurr.Close
|
136
|
|
137
|
End If '"_"
|
138
|
End If
|
139
|
Next tdfCheck
|
140
|
objWritefile.writeLine "</tableDefns>"
|
141
|
|
142
|
End Function
|
143
|
|
144
|
|
145
|
|
146
|
Public Function substTextForText(strSource, strFind, strReplace) As String
|
147
|
'function takes a string (strSource), searches it for strFind
|
148
|
' and replaces strFind with strReplace
|
149
|
Dim intLoop As Integer, intWhere As Integer, intLenWhole As Integer, intLenfind As Integer
|
150
|
If (Nz(Len(strSource), 0) = 0) Or (Nz(Len(strSource), 0) = 0) Then
|
151
|
'MsgBox "A search element is 0, error!"
|
152
|
Exit Function
|
153
|
End If
|
154
|
If InStr(strReplace, strFind) <> 0 Then
|
155
|
MsgBox "error, your replace element contains the search element!"
|
156
|
Exit Function
|
157
|
End If
|
158
|
intLenfind = Len(strFind)
|
159
|
intLoop = 0 'counter
|
160
|
'Debug.Print strSource
|
161
|
Do Until InStr(strSource, strFind) = 0
|
162
|
intLoop = intLoop + 1
|
163
|
'instr does not = 0
|
164
|
intWhere = InStr(strSource, strFind)
|
165
|
intLenWhole = Len(strSource)
|
166
|
If intLenWhole = 0 Then GoTo Err0
|
167
|
strSource = Left(strSource, intWhere - 1) & strReplace & _
|
168
|
Right(strSource, intLenWhole - (intWhere + intLenfind) + 1)
|
169
|
'Debug.Print strSource
|
170
|
If intLoop / 500 = Int(intLoop / 500) Then
|
171
|
'got out of control somehow
|
172
|
Dim intGetAnswer As Integer
|
173
|
intGetAnswer = MsgBox(intLoop & " iterations, not done, probably infinited loop. Stop?", vbYesNo)
|
174
|
If intGetAnswer = vbYes Then
|
175
|
Exit Function
|
176
|
End If
|
177
|
End If
|
178
|
Loop
|
179
|
'"succeeded. " & intloop & " replaced. Now is:" & Chr(13) & strSource
|
180
|
substTextForText = strSource
|
181
|
Exit Function
|
182
|
Err0: MsgBox "length is somehow 0"
|
183
|
End Function
|
184
|
|
185
|
Public Function fieldExistOnTbl(strFld As String, strTbl As String) As Boolean
|
186
|
''returns true if field is found on table/qry def
|
187
|
On Error GoTo Failed
|
188
|
Dim rstME As New ADODB.Recordset
|
189
|
rstME.Open "SELECT [" & strFld & "] FROM [" & strTbl & "];", _
|
190
|
CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly, adCmdText
|
191
|
'success!
|
192
|
fieldExistOnTbl = True
|
193
|
Exit Function
|
194
|
Failed:
|
195
|
fieldExistOnTbl = False
|
196
|
End Function
|