-
Notifications
You must be signed in to change notification settings - Fork 3
/
M_omMSAccessFunctions.def
356 lines (311 loc) · 12.7 KB
/
M_omMSAccessFunctions.def
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
Option Compare Database
Option Explicit
Public Function FormExists(FormName As String) As Boolean
FormExists = False
On Error Resume Next
FormExists = (CurrentProject.AllForms(FormName).Name = FormName)
End Function
Public Function ReportExists(ReportName As String) As Boolean
ReportExists = False
On Error Resume Next
ReportExists = (CurrentProject.AllReports(ReportName).Name = ReportName)
End Function
Public Function TableExists(tableName As String) As Boolean
TableExists = False
On Error Resume Next
TableExists = (CurrentDb.TableDefs(tableName).Name = tableName)
End Function
Public Function QueryExists(queryName As String) As Boolean
QueryExists = False
On Error Resume Next
QueryExists = (CurrentDb.QueryDefs(queryName).Name = queryName)
End Function
'Public Function QueryExists(queryName As String) As Boolean
' QueryExists = NotIsNullOrEmpty(GetQuerySQL(queryName))
'End Function
Public Sub DeleteQuery(queryName As String)
If QueryExists(queryName) Then
CurrentDb.QueryDefs.Delete queryName
End If
End Sub
Public Sub CreatePassthroughQuery(queryName As String, sql As String, connection As String)
Dim qd As QueryDef
DeleteQuery queryName
Set qd = CurrentDb.CreateQueryDef(queryName)
qd.Connect = connection
qd.sql = sql
CurrentDb.QueryDefs.Refresh
End Sub
Public Function GetQuerySQL(queryName As String) As String
On Error Resume Next
GetQuerySQL = CurrentDb.QueryDefs(queryName).sql
End Function
Public Sub HideNavigationPane()
On Error Resume Next
DoCmd.NavigateTo "acNavigationCategoryObjectType" 'Select Navigation Pane
DoCmd.RunCommand acCmdWindowHide
End Sub
Public Sub MinimizeNavigationPane()
On Error Resume Next
DoCmd.NavigateTo "acNavigationCategoryObjectType" 'Select Navigation Pane
DoCmd.Minimize
End Sub
Public Sub UnhideNavigationPane()
On Error Resume Next
DoCmd.SelectObject acTable, , True
End Sub
Public Sub ExportAllTables()
Dim accObj As AccessObject
For Each accObj In CurrentData.AllTables
On Error Resume Next
omExportFunctions.ExportQuery accObj.Name
Next
End Sub
Public Sub ConvertToLocalTables(Optional databaseType As String = ".accdb")
Dim rs As New ADODB.Recordset
rs.Open "SELECT Name,Database,lv FROM MsysObjects WHERE [Type]=6 AND LV IS NOT NULL", CurrentProject.connection, adOpenForwardOnly, adLockReadOnly
While Not rs.EOF
If (omStringFunctions.IsNullOrEmpty(databaseType) Or InStr(1, rs("Database"), databaseType) > 0) And InStr(1, rs("Name"), "~") = 0 Then
'On Error Resume Next
Debug.Print rs("Name"), rs("database")
DoCmd.SelectObject acTable, rs("Name"), True
DoCmd.RunCommand acCmdConvertLinkedTableToLocal
End If
rs.MoveNext
DoEvents
Wend
rs.Close
Set rs = Nothing
End Sub
Public Sub MakeLinkedTablesLocal()
Dim i As Long
Dim tbl As TableDef
For i = 0 To CurrentDb.TableDefs.Count - 1
If CurrentDb.TableDefs(i).Connect <> "" And Left(CurrentDb.TableDefs(i).Name, 4) <> "MSys" And Left(CurrentDb.TableDefs(i).Name, 1) <> "~" Then
If Right(CurrentProject.Name, 1) = "b" Then
gLogging.WriteToFile Description:="MakeLinkedTables > Right(CurrentProject.Name, 1) = b"
DoCmd.SelectObject acTable, CurrentDb.TableDefs(i).Name, True
gLogging.WriteToFile Description:="MakeLinkedTables > DoCmd.SelectObject acTable"
DoCmd.RunCommand acCmdConvertLinkedTableToLocal
gLogging.WriteToFile Description:="MakeLinkedTables > DoCmd.RunCommand acCmdConvertLinkedTableToLocal"
Else
gLogging.WriteToFile Description:="MakeLinkedTables > Right(CurrentProject.Name, 1) <> b"
MakeLinkedTableLocal CurrentDb.TableDefs(i).Name
gLogging.WriteToFile Description:="MakeLinkedTables > MakeLinkedTableLocal CurrentDb.TableDefs(i).Name"
End If
End If
Next
End Sub
Public Sub MakeLinkedTableLocal(tableName As String, Optional structureOnly As Boolean = False)
Dim tempTableName As String
Dim tbl As DAO.TableDef
Dim idx As DAO.index
'http://www.geeksengine.com/article/duplicate-access-table.html
'CurrentProject.Connection.Execute "SELECT * INTO T_Accounts FROM Accounts"
'DoCmd.CopyObject , "T_Accounts", acTable, "Accounts"
'DoCmd.TransferDatabase acExport, "Microsoft Access", CurrentDb.Name, acTable, "Accounts", "T_Accounts", StructureOnly:=True
tempTableName = "TCOPY_" & tableName
omMSAccessFunctions.DeleteTable tempTableName
gLogging.WriteToFile Description:="MakeLinkedTable > DeleteTable tempTableName"
CurrentProject.connection.Execute "SELECT * INTO [" & tempTableName & "] FROM [" & tableName & "]"
gLogging.WriteToFile Description:="MakeLinkedTable > SELECT INTO FROM"
' copy existing indexes
Workspaces(0).Databases(0).TableDefs.Refresh
Set tbl = Workspaces(0).Databases(0).TableDefs(tableName)
For Each idx In tbl.Indexes
CreateIndexUsingDAO tempTableName, idx.Name, Mid(Replace(Replace(idx.Fields, ";+", "],["), "+", "][") & "]", 2), idx.Primary, idx.Unique, Not idx.IgnoreNulls ' not implemented,idx.Clustered ,idx.Foreign ,idx.Required
gLogging.WriteToFile Description:="MakeLinkedTable > CreateIndex"
Next
DoCmd.DeleteObject acTable, tableName
gLogging.WriteToFile Description:="MakeLinkedTable > DoCmd.DeleteObject acTable, tableName"
'DoCmd.Rename tableName, acTable, tempTableName ' Does not work in ACCDR/ACCDE
If structureOnly Then
DoCmd.TransferDatabase acExport, "Microsoft Access", CurrentDb.Name, acTable, tempTableName, tableName, structureOnly:=True
Else
DoCmd.CopyObject , tableName, acTable, tempTableName
End If
DoCmd.DeleteObject acTable, tempTableName
gLogging.WriteToFile Description:="MakeLinkedTable > DoCmd.Rename tableName, acTable, tempTableName"
CurrentDb.TableDefs.Refresh
End Sub
Public Sub DropIndexUsingDAO(tableName As String, indexName As String)
On Error Resume Next
Workspaces(0).Databases(0).TableDefs(tableName).Indexes.Delete indexName
End Sub
Public Sub CreateIndexUsingDAO(tableName As String, indexName As String, fieldNames As String, Optional setPrimary As Boolean = False, Optional setUnique As Boolean = False, Optional setDisallowNull As Boolean = False)
Dim tbl As DAO.TableDef
Dim idx As DAO.index
Dim fld As DAO.Field
Dim fldNames() As String
Dim fldName As Variant
Set tbl = Workspaces(0).Databases(0).TableDefs(tableName)
Set idx = tbl.CreateIndex(indexName)
idx.Primary = setPrimary
idx.Unique = setUnique
fldNames = omStringFunctions.StringSplit(fieldNames, ",")
For Each fldName In fldNames
idx.Fields.Append idx.CreateField(Replace(Replace(fldName, "[", ""), "]", ""))
Next
DropIndexUsingDAO tableName, indexName
Workspaces(0).Databases(0).TableDefs(tableName).Indexes.Append idx
Workspaces(0).Databases(0).TableDefs(tableName).Indexes.Refresh
End Sub
Public Sub DropIndexUsingSQL(tableName As String, indexName As String)
On Error Resume Next
CurrentProject.connection.Execute StringFormat("DROP INDEX [{0}] ON [{1}]", indexName, tableName)
End Sub
Public Sub CreateIndexUsingSQL(tableName As String, indexName As String, fieldNames As String, Optional setPrimary As Boolean = False, Optional setUnique As Boolean = False, Optional setDisallowNull As Boolean = False)
Dim sql As String
sql = StringFormat("CREATE" & IIf(setPrimary Or setUnique, " UNIQUE", "") & " INDEX [{0}] ON {1} ({2})", indexName, tableName, fieldNames)
If setPrimary Then
sql = sql & " WITH PRIMARY"
ElseIf setDisallowNull Then
sql = sql & " WITH DISALLOW NULL"
End If
DropIndexUsingSQL tableName, indexName
gLogging.WriteToFile Description:="CreateIndexUsingSQL > DropIndexUsingSQL tableName, indexName"
CurrentProject.connection.Execute sql
gLogging.WriteToFile Description:="CreateIndexUsingSQL > CurrentProject.Connection.Execute sql"
End Sub
Public Sub DeleteTables(tablePrefix As String)
Dim i As Long
For i = CurrentDb.TableDefs.Count - 1 To 0 Step -1
If Left(CurrentDb.TableDefs(i).Name, Len(tablePrefix)) = tablePrefix Then
DoCmd.DeleteObject acTable, CurrentDb.TableDefs(i).Name
End If
Next i
End Sub
Public Sub SetAccessProperty(propertyName As String, Value As Variant, Optional propertyType As DAO.DataTypeEnum = dbText)
Dim prp As DAO.Property
On Error Resume Next
Set prp = CurrentDb.CreateProperty(propertyName, propertyType, Value)
CurrentDb.Properties.Append prp
If Err = 3367 Then
CurrentDb.Properties(propertyName) = Value
End If
On Error GoTo 0
End Sub
Public Sub DeleteTable(tableName As String)
If TableExists(tableName) Then
DoCmd.DeleteObject acTable, tableName
End If
End Sub
Public Function IsTableLocal(tableName As String) As Boolean
If TableExists(tableName) Then
IsTableLocal = (CurrentDb.TableDefs(tableName).Connect = "")
End If
End Function
Public Sub FormFields_Extract()
Dim rsMSysObject As New ADODB.Recordset
Dim rsFields As New ADODB.Recordset
Dim ObjectTemp As Object
Dim ctl As Control
On Error GoTo FormFields_Extract_Error
rsMSysObject.Open "SELECT Name, Type FROM msysobjects WHERE (((Type)=-32768 Or (Type)=-32764))", CurrentProject.connection, adOpenDynamic, adLockOptimistic
rsFields.Open "Fields", CurrentProject.connection, adOpenDynamic, adLockOptimistic
While Not rsMSysObject.EOF
Select Case rsMSysObject("Type")
Case -32768 ' Form
DoCmd.OpenForm rsMSysObject("Name"), acDesign, , , , acHidden
Set ObjectTemp = Forms(rsMSysObject("Name"))
Case -32764 ' Report
DoCmd.OpenReport rsMSysObject("Name"), acViewDesign
DoCmd.Minimize
Set ObjectTemp = Reports(rsMSysObject("Name"))
End Select
For Each ctl In ObjectTemp.Controls
With ctl
If .ControlType = acLabel Or .ControlType = acCommandButton Then
rsFields.AddNew
rsFields("Field_ID") = newField_ID
rsFields("Field_Name") = .Name
rsFields.Update
End If
End With
Next
rsFields.AddNew
rsFields("Field_ID") = newField_ID
rsFields("Field_Name") = ObjectTemp.Name
rsFields.Update
Select Case rsMSysObject("Type")
Case -32768 ' Form
DoCmd.Close acForm, ObjectTemp.Name
Case -32764 ' Report
DoCmd.Close acReport, ObjectTemp.Name
End Select
rsMSysObject.MoveNext
Wend
rsMSysObject.Close
rsFields.Close
Set rsMSysObject = Nothing
Set rsFields = Nothing
Exit Sub
FormFields_Extract_Error:
Select Case Err
Case 3022
Resume Next
Case Else
Exit Sub
End Select
End Sub
Public Sub cmdBarFields_Extract(strcmdBar As String, ctrl As Object)
Dim rsFields As New ADODB.Recordset
Dim ctl As Object
On Error GoTo cmdBarFields_Extract_Error
rsFields.Open "Fields", CurrentProject.connection, adOpenDynamic, adLockOptimistic
If strcmdBar <> "" Then
For Each ctl In CommandBars(strcmdBar).Controls
With ctl
rsFields.AddNew
rsFields("Field_ID") = newField_ID
rsFields("Field_Name") = .tag
rsFields.Update
If .Type = 10 Then
cmdBarFields_Extract "", ctl
End If
End With
Next
Else
For Each ctl In ctrl.Controls
With ctl
rsFields.AddNew
rsFields("Field_ID") = newField_ID
rsFields("Field_Name") = .tag
rsFields.Update
If .Type = 10 Then
cmdBarFields_Extract "", ctl
End If
End With
Next
End If
rsFields.Close
Set rsFields = Nothing
Exit Sub
cmdBarFields_Extract_Error:
Select Case Err
Case 3022
Resume Next
Case Else
Exit Sub
End Select
End Sub
Public Function newField_ID() As Long
If IsNull(DMax("Field_ID", "Fields")) Then
newField_ID = 1
Else
newField_ID = DMax("Field_ID", "Fields") + 1
End If
End Function
Public Sub RegisterCurrentLocation()
'Dim c As New cRegistry
' With c
' .ClassKey = HKEY_CURRENT_USER
' .SectionKey = "Software\Microsoft\Office\16.0\Access\Security\Trusted Locations\YourTrustedLocationName"
' .ValueKey = "Path"
' .ValueType = REG_DWORD
' .value = CurrentProject.path
' End With
End Sub
Public Function IsRuntimeMode() As Boolean
IsRuntimeMode = SysCmd(acSysCmdRuntime)
End Function