-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_acc_BasicDbRoutines.bas
executable file
·980 lines (819 loc) · 31.6 KB
/
mod_acc_BasicDbRoutines.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
Attribute VB_Name = "mod_acc_BasicDbRoutines"
Option Compare Database
Option Explicit
' error handling tag
Const cStrModuleName As String = "mod_acc_BasicDbRoutines"
' REFERENCES
' c:\program Files\Common Files\System\ado\msado15.dll
' 130911.AMG check data types are valid in xxxIngnoreNulls functions and extend error handling
' 130822.AMG added structured error handling
' The second half of this file contains code copied in from mod_acc_DataMisc
' (GENERIC DATA ACCESS CODE - 100414.AMG from 060906.AMG )
' which still needs to have redundant and non-generic code removed
' and have other parts of the object model properly refactored into other modules
Const cStrEnvironmentTable = "z3d_Environment"
Public g_ADOBackEndConn As ADODB.Connection
Public Function strGetEnvVar(strVarToGet As String) As String
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "strGetEnvVar"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
Dim sSQL As String
Dim rst As DAO.Recordset
sSQL = "SELECT e.EnvironmentValue " _
& " FROM " & cStrEnvironmentTable & " AS e " _
& " WHERE e.EnvironmentAttribute='" & strVarToGet & "' " _
& " ;"
Set rst = CurrentDb.OpenRecordset(sSQL)
If rst.RecordCount = 0 Then Exit Function
strGetEnvVar = rst(0)
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function strGetADOConn()
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "strGetADOConn"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
' just use the full string from the current connection
strGetADOConn = CurrentProject.Connection.ConnectionString
' ' just lift some details from the current connection ...
' Dim strProvider, strDataSource As String
' strProvider = CurrentProject.Connection.Provider
' strDataSource = CurrentProject.Connection.Properties("Data Source")
' ' and add any specific properties required
' strGetADOConn = "" _
' & "Provider=" & strProvider & ";" _
' & "Data Source=" & strDataSource & ";" _
' & " Jet OLEDB:Database Password=xxxxxxxxxx;"
'
'
'
'
'' ' BE_Provider
'' sProvider = strGetEnvVar("BE_Provider")
'' sCurrent = strGetEnvVar("DB_Env_Current")
'' sPath = strGetEnvVar("DBPath_" & sCurrent)
'' sBE = strGetEnvVar("BE_Name")
''
'' sDataSource = sPath & "\" & sBE & ".accdb"
''
'' ' with password
'' strGetADOConn = "Provider=" & sProvider & ";" _
'' & "Data Source=" & sDataSource & ";" _
'' & " Jet OLEDB:Database Password=xxxxxxxxxx;"
''
'' ' without password
'' 'GetADOConnStr = "Provider=" & sProvider & ";" _
'' & "Data Source=" & sDataSource & ";" _
'' & " Persist Security Info=False"
'
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function GetBackEndDBPathAndFileName()
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "GetBackEndDBPathAndFileName"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
Dim sCurrent, sPath, sBE, sDataSource As String
sCurrent = strGetEnvVar("DB_Env_Current")
sPath = strGetEnvVar("DBPath_" & sCurrent)
sBE = strGetEnvVar("BE_Name")
GetBackEndDBPathAndFileName = sPath & "\" & sBE & ".accdb"
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function dbTableDropAndRecreate(strTableName As String, strSQLTableDef As String)
On Error GoTo JustCreate
If CurrentDb.TableDefs(strTableName).Fields.Count <> 0 Then
ExecuteWithDAO "DROP TABLE " & strTableName & ";"
End If
' Dim td As TableDef
' For Each td In CurrentDb.TableDefs
' If td.Name = strTableName Then
' ExecuteWithDAO "DROP TABLE " & strTableName & ";"
' End If
' Next
JustCreate:
On Error GoTo 0
ExecuteWithDAO "CREATE TABLE " & strTableName & " ( " & strSQLTableDef & ");"
End Function
Public Function dbTableAddHyperlinkField(strTableName As String, strFieldName As String)
' according to reliable sources like Allen Browne and Susan Dorey
' there is no way to use DDL to create Hyperlink fields
' nor any way to programmatically _change_ a field type to hyperlink with DAO
' therefore this function will ADD hyperlink fields to a table once
' you have created the rest with dbTableDropAndRecreate
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "dbTableAddHyperlinkField"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
Dim db As Database
Set db = CurrentDb
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set tdf = db.TableDefs(strTableName)
' example > http://stackoverflow.com/questions/1133523/what-is-the-ms-access-sql-syntax-to-create-a-field-of-type-hyperlink
Set fld = tdf.CreateField(Name:=strFieldName, Type:=dbMemo)
fld.Attributes = dbHyperlinkField + dbVariableField
tdf.Fields.Append fld
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function QueryRecordset(sSQL As String) As DAO.Recordset
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "QueryRecordset"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
' ADO fails in current locking mode so stick to DAO
' **** Uncomment to diagnose query ****
Debug.Print cStrProcedureName & ": " & sSQL; ""
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset)
Set QueryRecordset = rst
' **** Uncomment to diagnose returned results ****
Dim f ' as variant
Debug.Print rst.Fields.Count & " fields:"
For Each f In rst.Fields
Debug.Print f.Name
Next
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function ExecuteActionQuery(sSQL As String, Optional conADOBackEndConn As ADODB.Connection) As Integer
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "ExecuteActionQuery"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
Dim rs As ADODB.Recordset
Dim iRecsAff As Integer
Dim conADOConn As ADODB.Connection
If conADOBackEndConn Is Nothing Then
If g_ADOBackEndConn Is Nothing Then
Set g_ADOBackEndConn = New ADODB.Connection
End If
Set conADOConn = g_ADOBackEndConn
Else
Set conADOConn = conADOBackEndConn
End If
' With conADOConn
' .Open strGetADOConn
' .Execute sSQL, iRecsAff
' .Close
' End With
' ADO fails in current locking mode so fall back to DAO
Dim rst As DAO.Recordset
CurrentDb.Execute sSQL
iRecsAff = CurrentDb.RecordsAffected
ExecuteActionQuery = iRecsAff
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function SetQueryDefinition(strQueryDefName As String, strSQL As String)
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "SetQueryDefinition"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
' had some issues with using currentdb directly
' which magically disappear when using an object variable!
Dim db As Database
Set db = CurrentDb
Dim qdf As QueryDef
On Error Resume Next
Set qdf = db.QueryDefs(strQueryDefName)
On Error GoTo ErrorHandler
If qdf Is Nothing Then
db.CreateQueryDef strQueryDefName, strSQL
Else
qdf.SQL = strSQL
End If
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
' ********** ********** ********** start of COPIED FROM mod_acc_DataMisc ********** ********** **********
' Attribute VB_Name = "mod_acc_DataMisc"
' Option Compare Database
' Option Explicit
'************************************************
' GENERIC DATA ACCESS CODE
' 100414.AMG from 060906.AMG
'************************************************
Public Function ExecuteAgainstDB(strSQL As String) As Long
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "ExecuteAgainstDB"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
With Application.CurrentProject.Connection
.Execute CommandText:=strSQL, _
RecordsAffected:=ExecuteAgainstDB
End With
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function ExecuteWithDAO(strSQL As String) As Long
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "ExecuteWithDAO"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
CurrentDb.Execute strSQL
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function RecordsetFromDB(strSQL As String) As ADODB.Recordset
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "RecordsetFromDB"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
Set RecordsetFromDB = New ADODB.Recordset
With RecordsetFromDB
.Open Source:=strSQL, _
ActiveConnection:=CurrentProject.Connection, _
CursorType:=adOpenForwardOnly, _
LockType:=adLockReadOnly
End With
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function ExecuteAgainstDBReturnID(strSQL As String) As Long
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "ExecuteAgainstDBReturnID"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
ExecuteAgainstDBReturnID = -1
ExecuteAgainstDB (strSQL)
With RecordsetFromDB("SELECT @@IDENTITY;")
If Not .EOF Then
ExecuteAgainstDBReturnID = .Fields(0).Value
End If
End With
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function DBreturnLong(strSQL As String) As Long
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "DBreturnLong"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
DBreturnLong = 0
On Error Resume Next
With RecordsetFromDB(strSQL)
If Not .EOF Then
DBreturnLong = lngIgnoreNulls(.Fields(0).Value)
End If
End With
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function DBreturnString(strSQL As String) As String
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "DBreturnString"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
DBreturnString = ""
On Error Resume Next
With RecordsetFromDB(strSQL)
If Not .EOF Then
DBreturnString = strIgnoreNulls(.Fields(0).Value)
End If
End With
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
' ----------------------------------------
' Preparing values to pass via SQL strings
' ----------------------------------------
'
' These functions return strings containing
' valid SQL expression fragments
' where VB variables have been correctly converted
' ready for interpretation by a SQL database engine
Public Function CSql(var As Variant) As String
' format any type as a string in the format Jet SQL expects
' numerics - in US format (not localised)
' dates - in US date format enclosed by hashes
' strings containing single quotes or apostrophes - enclosed in double quotes
' all other strings - simply enclosed in single quotes
If IsNumeric(var) Then
CSql = Str(var)
ElseIf IsDate(var) Then
CSql = Format(var, "\#MM/DD/YYYY\#")
ElseIf InStr(var, "'") > 0 Then
CSql = """" & var & """"
Else
CSql = "'" & var & "'"
End If
End Function
Public Function CSqlFld(strFieldName As String) As String
CSqlFld = "[" & strFieldName & "]"
End Function
Public Function strSqlPartialMatch(strFieldName As String, varValue As Variant) As String
strSqlPartialMatch = CSqlFld(strFieldName) & " LIKE '*" & varValue & "*'"
End Function
Public Function strSqlExactMatch(strFieldName As String, varValue As Variant) As String
strSqlExactMatch = CSqlFld(strFieldName) & " = " & CSql(varValue)
End Function
' -------------------------
' Preparing returned values
' -------------------------
'
' These functions get around "invalid use of Null" errors
Public Function strIgnoreNulls(varString As Variant) As String
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "strIgnoreNulls"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
If IsNull(varString) Then
strIgnoreNulls = ""
Else
strIgnoreNulls = CStr(varString)
End If
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function lngIgnoreNulls(varString As Variant) As Long
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "lngIgnoreNulls"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
If IsNull(varString) Then
lngIgnoreNulls = 0
Else
If Not IsNumeric(varString) Then
lngIgnoreNulls = 0
Else
lngIgnoreNulls = CLng(varString)
End If
End If
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function datIgnoreNulls(varString As Variant) As Long
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "datIgnoreNulls"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
If IsNull(varString) Then
datIgnoreNulls = 0
Else
If Not IsDate(varString) Then
datIgnoreNulls = 0
Else
datIgnoreNulls = CDate(varString)
End If
End If
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Public Function dblIgnoreNulls(varString As Variant) As Long
''' standard procedure error handler begin initialise 130808.AMG '''
Const cStrProcedureName As String = "dblIgnoreNulls"
DoCmd.SetWarnings False
On Error GoTo ErrorHandler
''' standard procedure error handler end initialise '''
If IsNull(varString) Then
dblIgnoreNulls = 0
Else
If Not IsNumeric(varString) Then
dblIgnoreNulls = 0
Else
dblIgnoreNulls = CDbl(varString)
End If
End If
''' standard procedure error handler begin terminate 130808.AMG '''
Proc_Exit:
DoCmd.SetWarnings True
Exit Function
ErrorHandler:
DoCmd.SetWarnings True
MsgBox "Error occured in procedure '" & cStrModuleName & "." & cStrProcedureName & "'" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description
Err.Raise (Err.Number)
Resume Proc_Exit
''' standard procedure error handler end terminate '''
End Function
Function strChooseFileToOpen(Optional strTitle As String) As String
' The Excel version would use...
' strFileName = Application.GetOpenFilename("Excel Worksbooks (*.xls), *.xls", , "Please select the GDC Move workbook")
' There is a long winded code to do somehting similar at
' sample > http://www.mvps.org/access/api/api0001.htm
' but the simple way is...
' Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
' making sure References includes Microsoft Office xx.0 Object Library
' credit > http://www.ozgrid.com/forum/showthread.php?t=28754
' credit > http://support.microsoft.com/kb/288543
Dim dlgOpen As FileDialog
Dim vrtSelectedItem As Variant ' need variant to extract choices from list
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
With dlgOpen
If Not IsMissing(strTitle) Then
.Title = strTitle
End If
.AllowMultiSelect = False
If .Show = -1 Then ' if the user DIDN'T cancel
For Each vrtSelectedItem In .SelectedItems
strChooseFileToOpen = vrtSelectedItem
Next vrtSelectedItem
Else
strChooseFileToOpen = ""
End If
End With
Set dlgOpen = Nothing
End Function
Sub DbImportXls(strTableName As String, strExcelFilename As String, Optional strTableDef As String, Optional strRange As String)
On Error Resume Next
ExecuteAgainstDB "DROP TABLE " & strTableName
On Error GoTo 0
If Not IsMissing(strTableDef) Then
If strTableDef <> "" Then
ExecuteAgainstDB "CREATE TABLE " & strTableName & " ( " & strTableDef & " );"
End If
End If
If IsMissing(strRange) Then
DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadsheetType:=acSpreadsheetTypeExcel9, _
tablename:=strTableName, _
FileName:=strExcelFilename, _
HasFieldNames:=True
' Range:="", _
' UseOA:=False
Else
DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadsheetType:=acSpreadsheetTypeExcel9, _
tablename:=strTableName, _
FileName:=strExcelFilename, _
HasFieldNames:=True, _
Range:=strRange
' UseOA:=False
End If
End Sub
Sub sample_multi_sheet_import()
' credit > http://blogs.technet.com/heyscriptingguy/archive/2008/01/21/how-can-i-import-multiple-worksheets-into-an-access-database.aspx
' NB: this is VB script
' Const acImport = 0
' Const acSpreadsheetTypeExcel9 = 8
'
' Set objAccess = CreateObject("Access.Application")
' objAccess.OpenCurrentDatabase "C:\Scripts\Personnel.mdb"
'
' Set objExcel = CreateObject("Excel.Application")
' objExcel.Visible = True
'
' strFileName = "C:\Scripts\ImportData.xls"
'
' Set objWorkbook = objExcel.Workbooks.Open(strFileName)
' Set colWorksheets = objWorkbook.Worksheets
'
' For Each objWorksheet In colWorksheets
' Set objRange = objWorksheet.UsedRange
' strWorksheetName = objWorksheet.Name & "!" & objRange.Address(False, False)
' objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
' "Employees", strFileName, True, strWorksheetName
' Next
End Sub
' ####### Relationship Management and Database Definition ########
' ################################################################
'
' This section tends to use DAO as I have found it simpler so far
' to use Database Definition Language in Access through DAO than ADO
' When you want to Import Records into a relational database
' call this function before doing your INSERT INTO x SELECT y FROM z;
' So that the relevant Lookup values are there and do not
' cause integrity issues or dropped inserts
Public Function DBAddDependentRecords _
(strImportTable As String _
, strImportField As String _
, strLookupTable As String _
, strLookupField As String _
, strLookupId As String _
, Optional strUpDateField As String _
, Optional strSourceField As String _
, Optional strSourceString As String _
)
Dim strSQL As String
strSQL = _
"INSERT INTO " & strLookupTable _
& " SELECT " & strLookupField
If Not IsMissing(strUpDateField) Then
strSQL = strSQL _
& " , Now() AS " & CSqlFld(strUpDateField) & " "
End If
If Not IsMissing(strSourceString) Then
strSQL = strSQL _
& " , " & CSql(strSourceString) & " AS " & CSqlFld(strSourceField) & " "
End If
strSQL = strSQL _
& " FROM ( " _
& " SELECT DISTINCT " & strImportTable & "." & strImportField & " AS " & strLookupField _
& " FROM " & strImportTable & " LEFT JOIN " & strLookupTable _
& " ON " & strImportTable & "." & strImportField _
& " = " & strLookupTable & "." & strLookupField _
& " GROUP BY " & strImportTable & "." & strImportField _
& " HAVING (Count(" & strLookupTable & "." & strLookupId & ")=0) " _
& " AND NOT (" & strImportTable & "." & strImportField & " Is Null) " _
& " ) ;"
' haven't worked out yet why this query fails with ADO - seems ok with DAO
ExecuteWithDAO strSQL
End Function
' Old column manipulation stuff - not fully tested
' ' tidy from import XLS - rename erroneous column name and remove cols after 30
' ' credit > http://forums.devx.com/showthread.php?t=50878
' With CurrentDb.TableDefs("GDC_Not_moving_List")
' .Fields("Numbr of CPU").Name = "Number of CPU"
' 'While .Fields.Count > 30
' ' .Fields.Delete (.Fields(30).Name)
' 'Wend
' End With
' Use this to insert a record if it doesn't already exist
' Very useful with lookup tables
'
' NB: FAILS IF THE TABLE IS EMPTY - THERE MUST BE AT LEAST ONE ROW
'
' Thanks to Marco De Luca ([email protected])
' for saving me from having to work the logic out from scratch
'
' The plain SQL code is...
'
' INSERT INTO LookupTable
' (LookupField, DetailField)
' SELECT DISTINCT
' 'Lookup Value' as LookupField,
' 'Detail Value' as DetailField
' FROM LookupTable
' WHERE 'Lookup Value' NOT In
' (SELECT LookupField from LookupTable);
Public Function InsertIfNotExists _
(strLookupTable As String _
, strLookupField As String _
, strLookupValue As String _
, Optional strDetailField As String _
, Optional strDetailValue As String _
) As Long
Dim strSQL As String
strSQL = _
"INSERT INTO " & strLookupTable _
& " ( " & strLookupField
If Not IsMissing(strDetailValue) Then ' only add the field if the value is there too
strSQL = strSQL _
& " , " & strDetailField
End If
strSQL = strSQL _
& " ) " _
& " SELECT DISTINCT " _
& CSql(strLookupValue) & " AS " & strLookupField
If Not IsMissing(strDetailValue) Then
strSQL = strSQL _
& " , " & CSql(strDetailValue) & " AS " & strDetailField
End If
strSQL = strSQL _
& " FROM " & strLookupTable _
& " WHERE " & CSql(strLookupValue) & " NOT IN " _
& " (SELECT " & strLookupField _
& " FROM " & strLookupTable _
& " ) ;"
InsertIfNotExists = ExecuteAgainstDBReturnID(strSQL)
End Function
Public Sub CreateQueryFromString(strQryName As String, strSQL As String)
On Error Resume Next
If CurrentDb.QueryDefs(strQryName).SQL <> strSQL Then
CurrentDb.QueryDefs(strQryName).SQL = strSQL
End If
If Err.Number = 3265 Then ' Error: Object not found in this collection
Err.Clear
CurrentDb.CreateQueryDef strQryName, strSQL
If Err.Number <> 0 Then
MsgBox "Could not create query" & vbCrLf & vbCrLf _
& strQryName & vbCrLf & vbCrLf _
& "Error " & Err.Number & " - " & Err.Description, _
vbCritical, _
"Error creating Query!"
End If
ElseIf Err.Number = 3012 Then ' Error: Object <name> already exists
On Error GoTo 0
CurrentDb.QueryDefs(strQryName).SQL = strSQL
ElseIf Err.Number <> 0 Then
MsgBox "Could not recreate query" & vbCrLf & vbCrLf _
& strQryName & vbCrLf & vbCrLf _
& "Error " & Err.Number & " - " & Err.Description, _
vbCritical, _
"Error recreating Query!"
End If
On Error GoTo 0
End Sub
'
' ####### Deprecated ##########################
' #############################################
'
' The following code may not be very generic, so may be of little value...
'
'
' This is used to modify the tables we link to from the interface
' It uses DAO to find the linked location of the named table
' It then accesses the linked database directly via ADO to
' make the modification
'
Public Sub UpgradeDB(strSQL As String)
On Error GoTo ErrorHandler
Dim strDAOConnect As String
Dim strADODBConnectionString As String
Dim cnn As ADODB.Connection
' For now we can use any table in the database,
' as they are all in the smae location, but
' if ever the back end was split, the calling
' function would have to pass the table name
Const strTableName As String = "Audits"
strDAOConnect = CurrentDb.TableDefs(strTableName).Connect
If Left(strDAOConnect, 10) <> ";DATABASE=" Then
MsgBox "Cannot correctly identify data source location" & vbCrLf & vbCrLf _
& "DAO.TableDef.Connect = """ & strDAOConnect & """", _
vbCritical + vbOKOnly, _
"Database upgrade Error"
Exit Sub
End If
strADODBConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Mid(strDAOConnect, 11) & ";" _
Set cnn = New ADODB.Connection
cnn.Open strADODBConnectionString
cnn.Execute strSQL
cnn.Close
Set cnn = Nothing
ErrorHandler:
Select Case Err.Number
Case 0: ' no action required
Case Else
MsgBox "We had not made contingencies for this error..." & vbCrLf & vbCrLf _
& "Number: " & Err.Number & vbCrLf _
& "Descxription: " & Err.Description & vbCrLf _
& "Source: " & Err.Source & vbCrLf & vbCrLf _
& "Procedure: ""UpgradeDB""", _
vbCritical + vbOKOnly, _
"Unanticipated Error"
End Select
End Sub
' ********** ********** ********** end of COPIED FROM mod_acc_DataMisc ********** ********** **********