/
DoCmd.xba
2662 lines (2411 loc) · 116 KB
/
DoCmd.xba
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="DoCmd" script:language="StarBasic">
REM =======================================================================================================================
REM === The Access2Base library is a part of the LibreOffice project. ===
REM === Full documentation is available on http://www.access2base.com ===
REM =======================================================================================================================
Option Explicit
Type _FindParams
FindRecord As Integer ' Set to 1 at first invocation of FindRecord
FindWhat As Variant
Match As Integer
MatchCase As Boolean
Search As Integer
SearchAsFormatted As Boolean ' Must be False
FindFirst As Boolean
OnlyCurrentField As Integer
Form As String ' Shortcut
GridControl As String ' Shortcut
Target As String ' Shortcut
LastRow As Long ' Last row explored - 0 = before first
LastColumn As Integer ' Last column explored - 0 ... N-1 index in next arrays; 0 if OnlyCurrentField = acCurrent
ColumnNames() As String ' Array of column names in grid with boundfield and of same type as FindWhat
ResultSetIndex() As Integer ' Array of column numbers in ResultSet
End Type
Type _Window
Frame As Object ' com.sun.star.comp.framework.Frame
_Name As String ' Object Name
WindowType As Integer ' One of the object types
DocumentType As String ' Writer, Calc, ... - Only if WindowType = acDocument
End Type
REM VBA allows call to actions with missing arguments e.g. OpenForm("aaa",,"[field]=2")
REM in StarBasic IsMissing requires Variant parameters
REM -----------------------------------------------------------------------------------------------------------------------
Public Function ApplyFilter( _
ByVal Optional pvFilter As Variant _
, ByVal Optional pvSQL As Variant _
, ByVal Optional pvControlName As Variant _
) As Boolean
' Set filter on open table, query, form or subform (if pvControlName present)
If _ErrorHandler() Then On Local Error Goto Error_Function
Const cstThisSub = "ApplyFilter"
Utils._SetCalledSub(cstThisSub)
ApplyFilter = False
If IsMissing(pvFilter) And IsMissing(pvSQL) Then Call _TraceArguments()
If IsMissing(pvFilter) Then pvFilter = ""
If Not Utils._CheckArgument(pvFilter, 1, vbString) Then Goto Exit_Function
If IsMissing(pvSQL) Then pvSQL = ""
If Not Utils._CheckArgument(pvSQL, 1, vbString) Then Goto Exit_Function
If IsMissing(pvControlName) Then pvControlName = ""
If Not Utils._CheckArgument(pvControlName, 1, vbString) Then Goto Exit_Function
Dim sFilter As String, oWindow As Object, oDatabase As Object, oTarget As Object
Set oDatabase = Application._CurrentDb()
If oDatabase._DbConnect <> DBCONNECTBASE Then Goto Error_NotApplicable
If pvSQL <> "" _
Then sFilter = oDatabase._ReplaceSquareBrackets(pvSQL) _
Else sFilter = oDatabase._ReplaceSquareBrackets(pvFilter)
Set oWindow = _SelectWindow()
With oWindow
Select Case .WindowType
Case acForm
Set oTarget = _DatabaseForm(._Name, pvControlName)
Case acQuery, acTable
If pvControlName <> "" Then Goto Exit_Function
If IsNull(.Frame.Controller.FormOperations) Then Goto Error_NotApplicable
' FormOperations returns <Null> in OpenOffice
Set oTarget = .Frame.Controller.FormOperations.Cursor
Case Else ' Ignore action
Goto Exit_Function
End Select
End With
With oTarget
.Filter = sFilter
.ApplyFilter = True
.reload()
End With
ApplyFilter = True
Exit_Function:
Utils._ResetCalledSub(cstThisSub)
Exit Function
Error_NotApplicable:
TraceError(TRACEFATAL, ERRACTION, Utils._CalledSub(), 0, 1, cstThisSub)
Goto Exit_Function
Error_Function:
TraceError(TRACEABORT, Err, cstThisSub, Erl)
GoTo Exit_Function
End Function ' ApplyFilter V1.2.0
REM -----------------------------------------------------------------------------------------------------------------------
Public Function mClose(Optional ByVal pvObjectType As Variant _
, Optional ByVal pvObjectName As Variant _
, Optional ByVal pvSave As Variant _
) As Boolean
If _ErrorHandler() Then On Local Error Goto Error_Function
Const cstThisSub = "Close"
Utils._SetCalledSub(cstThisSub)
mClose = False
If IsMissing(pvObjectType) Or IsMissing(pvObjectName) Then Call _TraceArguments()
If IsMissing(pvSave) Then pvSave = acSavePrompt
If Not (Utils._CheckArgument(pvObjectType, 1, Utils._AddNumeric(), _
Array(acTable, acQuery, acForm, acReport)) _
And Utils._CheckArgument(pvObjectName, 2, vbString) _
And Utils._CheckArgument(pvSave, 3, Utils._AddNumeric(), Array(acSavePrompt)) _
) Then Goto Exit_Function
Dim sObjects() As String, sObjectName As String, oController As Object, oObject As Object
Dim i As Integer, bFound As Boolean, lComponent As Long
Dim oDatabase As Object
Set oDatabase = Application._CurrentDb()
If oDatabase._DbConnect <> DBCONNECTBASE Then Goto Error_NotApplicable
' Check existence of object and find its exact (case-sensitive) name
Select Case pvObjectType
Case acForm
sObjects = Application._GetAllHierarchicalNames()
lComponent = com.sun.star.sdb.application.DatabaseObject.FORM
Case acTable
sObjects = oDatabase.Connection.getTables.ElementNames()
lComponent = com.sun.star.sdb.application.DatabaseObject.TABLE
Case acQuery
sObjects = oDatabase.Connection.getQueries.ElementNames()
lComponent = com.sun.star.sdb.application.DatabaseObject.QUERY
Case acReport
sObjects = oDatabase.Document.getReportDocuments.ElementNames()
lComponent = com.sun.star.sdb.application.DatabaseObject.REPORT
End Select
bFound = False
For i = 0 To UBound(sObjects)
If UCase(pvObjectName) = UCase(sObjects(i)) Then
sObjectName = sObjects(i)
bFound = True
Exit For
End If
Next i
If Not bFound Then Goto Trace_NotFound
Select Case pvObjectType
Case acForm
Set oController = oDatabase.Document.getFormDocuments.getByHierarchicalName(sObjectName)
mClose = oController.close()
Case acTable, acQuery ' Not optimal but it works !!
Set oController = oDatabase.Document.CurrentController
Set oObject = oController.loadComponent(lComponent, sObjectName, False)
oObject.frame.close(False)
mClose = True
Case acReport
Set oController = oDatabase.Document.getReportDocuments.getByName(sObjectName)
mClose = oController.close()
End Select
Exit_Function:
Set oObject = Nothing
Set oController = Nothing
Utils._ResetCalledSub(cstThisSub)
Exit Function
Error_Function:
TraceError(TRACEABORT, Err, "Close", Erl)
GoTo Exit_Function
Trace_Error:
TraceError(TRACEFATAL, ERRCLOSEOBJECT, Utils._CalledSub(), 0, , Array(_GetLabel(Array("Table", "Query", "Form", "Report")(pvObjectType)), pvObjectName))
Goto Exit_Function
Trace_NotFound:
TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel(Array("Table", "Query", "Form", "Report")(pvObjectType)), pvObjectName))
Goto Exit_Function
Error_NotApplicable:
TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
Goto Exit_Function
End Function ' (m)Close V1.1.0
REM -----------------------------------------------------------------------------------------------------------------------
Public Function CopyObject(ByVal Optional pvSourceDatabase As Variant _
, ByVal Optional pvNewName As Variant _
, ByVal Optional pvSourceType As Variant _
, ByVal Optional pvSourceName As Variant _
) As Boolean
' Copies tables and queries into identical (new) objects
If _ErrorHandler() Then On Local Error Goto Error_Function
Const cstThisSub = "CopyObject"
Utils._SetCalledSub(cstThisSub)
CopyObject = False
If IsMissing(pvSourceDatabase) Then pvSourceDatabase = ""
If VarType(pvSourceDatabase) <> vbString Then
If Not Utils._CheckArgument(pvSourceDatabase, 1, OBJDATABASE) Then Goto Exit_Function
End If
If IsMissing(pvNewName) Then Call _TraceArguments()
If Not Utils._CheckArgument(pvNewName, 2, vbString) Then Goto Exit_Function
If IsMissing(pvSourceType) Then Call _TraceArguments()
If Not Utils._CheckArgument(pvSourceType, 1, Utils._AddNumeric(), Array(acQuery, acTable) _
) Then Goto Exit_Function
If IsMissing(pvSourceName) Then Call _TraceArguments()
If Not Utils._CheckArgument(pvSourceName, 2, vbString) Then Goto Exit_Function
Dim oSource As Object, oSourceDatabase As Object, oTarget As Object, oDatabase As Object, bSameDatabase As Boolean
Dim oSourceTable As Object, oSourceColumns As Object, oSourceCol As Object, oTargetCol As Object, iRDBMS As Integer
Dim oSourceKeys As Object, oSourceKey As Object, oTargetKey As Object
Dim i As Integer, j As Integer, sSql As String, vPrimaryKeys() As Variant
Dim vNameComponents() As Variant, iNames As Integer, sSurround As String
Dim vInputField As Variant, vFieldBinary() As Variant, vOutputField As Variant
Dim oInput as Object, oOutput As Object, iNbFields As Integer, vValue As Variant
Dim vBinary As Variant, lInputSize As Long, lOutputSize As Long
Dim lInputRecs As Long, lInputMax As Long, vField As Variant, bProgressMeter As Boolean, sFile As String
Const cstMaxBinlength = 2 * 65535
Const cstChunkSize = 2 * 65535
Const cstProgressMeterLimit = 100
Set oDatabase = Application._CurrentDb()
bSameDatabase = False
If VarType(pvSourceDatabase) = vbString Then
If pvSourceDatabase = "" Then
Set oSourceDatabase = oDatabase
bSameDatabase = True
Else
Set oSourceDatabase = Application.OpenDatabase(ConvertToUrl(pvSourceDatabase), "", "", True)
If IsNull(oSourceDatabase) Then Goto Exit_Function
End If
Else
Set oSourceDatabase = pvSourceDatabase
End If
With oDatabase
iRDBMS = ._RDBMS
If ._DbConnect <> DBCONNECTBASE Then Goto Error_NotApplicable
Select Case pvSourceType
Case acQuery
Set oSource = oSourceDatabase.QueryDefs(pvSourceName, True)
If IsNull(oSource) Then Goto Error_NotFound
Set oTarget = .QueryDefs(pvNewName, True)
If Not IsNull(oTarget) Then .Connection.getQueries.dropByName(oTarget.Name) ' a query with same name exists already ... drop it
If oSource.Query.EscapeProcessing Then
Set oTarget = .CreateQueryDef(pvNewName, oSource.SQL)
Else
Set oTarget = .CreateQueryDef(pvNewName, oSource.SQL, dbSQLPassThrough)
End If
' Save .odb document
.Document.store()
Case acTable
Set oSource = oSourceDatabase.TableDefs(pvSourceName, True)
If IsNull(oSource) Then Goto Error_NotFound
Set oTarget = .TableDefs(pvNewName, True)
' A table with same name exists already ... drop it
If Not IsNull(oTarget) Then .Connection.getTables.dropByName(oTarget.Name)
' Copy source table columns
Set oSourceTable = oSource.Table
Set oTarget = .Connection.getTables.createDataDescriptor
oTarget.Description = oSourceTable.Description
vNameComponents = Split(pvNewName, ".")
iNames = UBound(vNameComponents)
If iNames >= 2 Then oTarget.CatalogName = vNameComponents(iNames - 2) Else oTarget.CatalogName = ""
If iNames >= 1 Then oTarget.SchemaName = vNameComponents(iNames - 1) Else oTarget.SchemaName = ""
oTarget.Name = vNameComponents(iNames)
oTarget.Type = oSourceTable.Type
Set oSourceColumns = oSourceTable.Columns
Set oTargetCol = oTarget.Columns.createDataDescriptor
For i = 0 To oSourceColumns.getCount() - 1
' Append each individual column to the table descriptor
Set oSourceCol = oSourceColumns.getByIndex(i)
_ConvertDataDescriptor oSourceCol, oSourceDatabase._RDBMS, oTargetCol, oDatabase
oTarget.Columns.appendByDescriptor(oTargetCol)
Next i
' Copy keys
Set oSourceKeys = oSourceTable.Keys
Set oTargetKey = oTarget.Keys.createDataDescriptor()
For i = 0 To oSourceKeys.getCount() - 1
' Append each key to table descriptor
Set oSourceKey = oSourceKeys.getByIndex(i)
oTargetKey.DeleteRule = oSourceKey.DeleteRule
oTargetKey.Name = oSourceKey.Name
oTargetKey.ReferencedTable = oSourceKey.ReferencedTable
oTargetKey.Type = oSourceKey.Type
oTargetKey.UpdateRule = oSourceKey.UpdateRule
Set oTargetCol = oTargetKey.Columns.createDataDescriptor()
For j = 0 To oSourceKey.Columns.getCount() - 1
Set oSourceCol = oSourceKey.Columns.getByIndex(j)
_ConvertDataDescriptor oSourceCol, oSourceDatabase._RDBMS, oTargetCol, oDatabase, True
oTargetKey.Columns.appendByDescriptor(oTargetCol)
Next j
oTarget.Keys.appendByDescriptor(oTargetKey)
Next i
' Duplicate table whole design
.Connection.getTables.appendByDescriptor(oTarget)
' Copy data
Select Case bSameDatabase
Case True
' Build SQL statement to copy data
sSurround = Utils._Surround(oSource.Name)
sSql = "INSERT INTO " & Utils._Surround(pvNewName) & " SELECT " & sSurround & ".* FROM " & sSurround
DoCmd.RunSQL(sSql)
Case False
' Copy data row by row and field by field
' As it is slow ... display a progress meter
Set oInput = oSourceDatabase.OpenRecordset(oSource.Name, , , dbReadOnly)
Set oOutput = .Openrecordset(pvNewName)
With oInput
If Not ( ._BOF And ._EOF ) Then
.MoveLast
lInputMax = .RecordCount
lInputRecs = 0
.MoveFirst
bProgressMeter = ( lInputMax > cstProgressMeterLimit )
iNbFields = .Fields().Count - 1
vFieldBinary = Array()
ReDim vFieldBinary(0 To iNbFields)
For i = 0 To iNbFields
vFieldBinary(i) = Utils._IsBinaryType(.Fields(i).Column.Type)
Next i
Else
bProgressMeter = False
End If
If bProgressMeter Then Application.SysCmd acSysCmdInitMeter, pvNewName & " 0 %", lInputMax
Do While Not .EOF()
oOutput.RowSet.moveToInsertRow()
oOutput._EditMode = dbEditAdd
For i = 0 To iNbFields
Set vInputField = .Fields(i)
Set vOutputField = oOutput.Fields(i)
If vFieldBinary(i) Then
lInputSize = vInputField.FieldSize
If lInputSize <= cstMaxBinlength Then
vField = Utils._getResultSetColumnValue(.RowSet, i + 1, True)
Utils._updateResultSetColumnValue(iRDBMS, oOutput.RowSet, i + 1, vField)
ElseIf oDatabase._BinaryStream Then
' Typically for SQLite where binary fields are limited
If lInputSize > vOutputField._Precision Then
TraceError(TRACEWARNING, ERRPRECISION, Utils._CalledSub(), 0, 1, Array(vOutputField._Name, lInputRecs + 1))
Utils._updateResultSetColumnValue(iRDBMS, oOutput.RowSet, i + 1, Null)
Else
sFile = Utils._GetRandomFileName("BINARY")
vInputField._WriteAll(sFile, "WriteAllBytes")
vOutputField._ReadAll(sFile, "ReadAllBytes")
Kill ConvertToUrl(sFile)
End If
End If
Else
vField = Utils._getResultSetColumnValue(.RowSet, i + 1)
If VarType(vField) = vbString Then
If Len(vField) > vOutputField._Precision Then
TraceError(TRACEWARNING, ERRPRECISION, Utils._CalledSub(), 0, 1, Array(vOutputField._Name, lInputRecs + 1))
End If
End If
' Update is done anyway, if too long, with truncation
Utils._updateResultSetColumnValue(iRDBMS, oOutput.RowSet, i + 1, vField)
End If
Next i
If oOutput.RowSet.IsNew And oOutput.RowSet.IsModified Then oOutput.RowSet.insertRow()
oOutput._EditMode = dbEditNone
lInputRecs = lInputRecs + 1
If bProgressMeter Then
If lInputRecs Mod (lInputMax / 100) = 0 Then
Application.SysCmd acSysCmdUpdateMeter, pvNewName & " " & CStr(CLng(lInputRecs * 100 / lInputMax)) & "%", lInputRecs
End If
End If
.MoveNext
Loop
End With
oOutput.mClose()
Set oOutput = Nothing
oInput.mClose()
Set oInput = Nothing
if bProgressMeter Then Application.SysCmd acSysCmdClearStatus
End Select
Case Else
End Select
End With
CopyObject = True
Exit_Function:
' Avoid closing the current database or the database object given as source argument
If VarType(pvSourceDatabase) = vbString And Not bSameDatabase Then
If Not IsNull(oSourceDatabase) Then oSourceDatabase.mClose()
End If
Set oSourceDatabase = Nothing
If Not IsNull(oOutput) Then oOutput.mClose()
Set oOutput = Nothing
If Not IsNull(oInput) Then oInput.mClose()
Set oInput = Nothing
Set oSourceCol = Nothing
Set oSourceKey = Nothing
Set oSourceKeys = Nothing
Set oSource = Nothing
Set oSourceTable = Nothing
Set oSourceColumns = Nothing
Set oTargetCol = Nothing
Set oTargetKey = Nothing
Set oTarget = Nothing
Utils._ResetCalledSub(cstThisSub)
Exit Function
Error_NotFound:
TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(Iif(pvSourceType = acQuery, _GetLabel("QUERY"), _GetLabel("TABLE")), pvSourceName))
Goto Exit_Function
Error_NotApplicable:
TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
Goto Exit_Function
Error_Function:
TraceError(TRACEABORT, Err, cstThisSub, Erl)
GoTo Exit_Function
End Function ' CopyObject V1.1.0
REM -----------------------------------------------------------------------------------------------------------------------
Public Function FindNext() As Boolean
' Must be called after a FindRecord
' Execute instructions set in FindRecord object
If _ErrorHandler() Then On Local Error Goto Error_Function
FindNext = False
Utils._SetCalledSub("FindNext")
Dim ofForm As Object, ocGrid As Object
Dim i As Integer, lInitialRow As Long, lFindRow As Long
Dim bFound As Boolean, b2ndRound As Boolean, bStop As Boolean
Dim vFindValue As Variant, oFindrecord As Object
Set oFindRecord = _A2B_.FindRecord
If IsNull(oFindRecord) Then GoTo Error_FindRecord
With oFindRecord
If .FindRecord = 0 Then Goto Error_FindRecord
.FindRecord = 0
Set ofForm = getObject(.Form)
If ofForm._Type = OBJCONTROL Then Set ofForm = ofForm.Form ' Bug Tombola
Set ocGrid = getObject(.GridControl)
' Move cursor to the initial row. Operation based on last FindRecord, not on user interactions done inbetween
If ofForm.DatabaseForm.RowCount <= 0 then Goto Exit_Function ' Dataset is empty
lInitialRow = .LastRow ' Used if Search = acSearchAll
bFound = False
lFindRow = .LastRow
b2ndRound = False
Do
' Last column ? Go to next row
If .LastColumn >= UBound(.ColumnNames) Then
bStop = False
If ofForm.DatabaseForm.isAfterLast() And .Search = acUp Then
ofForm.DatabaseForm.last()
ElseIf ofForm.DatabaseForm.isLast() And .Search = acSearchAll Then
ofForm.DatabaseForm.first()
b2ndRound = True
ElseIf ofForm.DatabaseForm.isBeforeFirst() And (.Search = acDown Or .Search = acSearchAll) Then
ofForm.DatabaseForm.first()
ElseIf ofForm.DatabaseForm.isFirst() And .search = acUp Then
ofForm.DatabaseForm.beforeFirst()
bStop = True
ElseIf ofForm.DatabaseForm.isLast() And .search = acDown Then
ofForm.DatabaseForm.afterLast()
bStop = True
ElseIf .Search = acUp Then
ofForm.DatabaseForm.previous()
Else
ofForm.DatabaseForm.next()
End If
lFindRow = ofForm.DatabaseForm.getRow()
If bStop Or (.Search = acSearchAll And lFindRow >= lInitialRow And b2ndRound) Then
ofForm.DatabaseForm.absolute(lInitialRow)
Exit Do
End If
.LastColumn = 0
Else
.LastColumn = .LastColumn + 1
End If
' Examine column contents
If .LastColumn <= UBound(.ColumnNames) Then
For i = .LastColumn To UBound(.ColumnNames)
vFindValue = Utils._getResultSetColumnValue(ofForm.DatabaseForm.createResultSet(), .ResultSetIndex(i))
Select Case VarType(.FindWhat)
Case vbDate, vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbBigint, vbDecimal
bFound = ( .FindWhat = vFindValue )
Case vbString
If VarType(vFindValue) = vbString Then
Select Case .Match
Case acStart
If .MatchCase Then
bFound = ( Left(.FindWhat, Len(.FindWhat)) = vFindValue )
Else
bFound = ( UCase(Left(.FindWhat, Len(.FindWhat))) = UCase(vFindValue) )
End If
Case acAnyWhere
If .MatchCase Then
bFound = ( InStr(1, vFindValue, .FindWhat, 0) > 0 )
Else
bFound = ( InStr(vFindValue, .FindWhat) > 0 )
End If
Case acEntire
If .MatchCase Then
bFound = ( .FindWhat = vFindValue )
Else
bFound = ( UCase(.FindWhat) = UCase(vFindValue) )
End If
End Select
Else
bFound = False
End If
End Select
If bFound Then
.LastColumn = i
Exit For
End If
Next i
End If
Loop While Not bFound
.LastRow = lFindRow
If bFound Then
ocGrid.Controls(.ColumnNames(.LastColumn)).setFocus()
.FindRecord = 1
FindNext = True
End If
End With
Exit_Function:
Utils._ResetCalledSub("FindNext")
Exit Function
Error_Function:
TraceError(TRACEABORT, Err, "FindNext", Erl)
GoTo Exit_Function
Error_FindRecord:
TraceError(TRACEERRORS, ERRFINDRECORD, Utils._CalledSub(), 0)
Goto Exit_Function
End Function ' FindNext V1.1.0
REM -----------------------------------------------------------------------------------------------------------------------
Public Function FindRecord(Optional ByVal pvFindWhat As Variant _
, Optional ByVal pvMatch As Variant _
, Optional ByVal pvMatchCase As Variant _
, Optional ByVal pvSearch As Variant _
, Optional ByVal pvSearchAsFormatted As Variant _
, Optional ByVal pvTargetedField As Variant _
, Optional ByVal pvFindFirst As Variant _
) As Boolean
'Find a value (string or other) in the underlying data of a gridcontrol
'Search in all columns or only in one single control
' see pvTargetedField = acAll or acCurrent
' pvTargetedField may also be a shortcut to a GridControl or one of its subcontrols
'Initialize _Findrecord structure in Database root and call FindNext() to set cursor on found value
If _ErrorHandler() Then On Local Error Goto Error_Function
FindRecord = False
Utils._SetCalledSub("FindRecord")
If IsMissing(pvFindWhat) Or pvFindWhat = "" Then Call _TraceArguments()
If IsMissing(pvMatch) Then pvMatch = acEntire
If IsMissing(pvMatchCase) Then pvMatchCase = False
If IsMissing(pvSearch) Then pvSearch = acSearchAll
If IsMissing(pvSearchAsFormatted) Then pvSearchAsFormatted = False ' Anyway only False supported
If IsMissing(pvTargetedField) Then pvTargetedField = acCurrent
If IsMissing(pvFindFirst) Then pvFindFirst = True
If Not (Utils._CheckArgument(pvFindWhat, 1, Utils._AddNumeric(Array(vbString, vbDate))) _
And Utils._CheckArgument(pvMatch, 2, Utils._AddNumeric(), Array(acAnywhere, acEntire, acStart)) _
And Utils._CheckArgument(pvMatchCase, 3, vbBoolean) _
And Utils._CheckArgument(pvSearch, 4, Utils._AddNumeric(), Array(acDown, acSearchAll, acUp)) _
And Utils._CheckArgument(pvSearchAsFormatted, 5, vbBoolean, Array(False)) _
And Utils._CheckArgument(pvTargetedField, 6, Utils._AddNumeric(vbString)) _
And Utils._CheckArgument(pvFindFirst, 7, vbBoolean) _
) Then Exit Function
If VarType(pvTargetedField) <> vbString Then
If Not Utils._CheckArgument(pvTargetedField, 6, Utils._AddNumeric(), Array(acAll, acCurrent)) Then Exit Function
End If
Dim ocTarget As Object, i As Integer, j As Integer, vNames() As Variant, iCount As Integer, vIndexes() As Variant
Dim vColumn As Variant, vDataField As Variant, ofParentForm As Variant, oColumns As Object, vParentGrid As Object
Dim bFound As Boolean, ocGridControl As Object, iFocus As Integer
Dim oFindRecord As _FindParams
With oFindRecord
.FindRecord = 0
.FindWhat = pvFindWhat
.Match = pvMatch
.MatchCase = pvMatchCase
.Search = pvSearch
.SearchAsFormatted = pvSearchAsFormatted
.FindFirst = pvFindFirst
' Determine target
' Either: pvTargetedField = Grid => search all fields
' pvTargetedField = Control in Grid => search only in that column
' pvTargetedField = acAll or acCurrent => determine focus
Select Case True
Case VarType(pvTargetedField) = vbString
Set ocTarget = getObject(pvTargetedField)
If ocTarget.SubType = CTLGRIDCONTROL Then
.OnlyCurrentField = acAll
.GridControl = ocTarget._Shortcut
.Target = .GridControl
ofParentForm = getObject(_getUpperShortcut(ocTarget._Shortcut, ocTarget._Name))
If IsNull(ofParentForm.DatabaseForm) Then Goto Error_DatabaseForm
Set oColumns = ofParentForm.DatabaseForm.createResultSet().Columns
iCount = -1
For i = 0 To ocTarget.ControlModel.Count - 1
Set vColumn = ocTarget.ControlModel.getByIndex(i)
Set vDataField = vColumn.BoundField ' examine field type
If Not IsNull(vDataField) Then
If _CheckColumnType(pvFindWhat, vDataField) Then
iCount = iCount + 1
ReDim Preserve vNames(0 To iCount)
vNames(iCount) = vColumn.Name
ReDim Preserve vIndexes(0 To iCount)
For j = 0 To oColumns.Count - 1
If vDataField.Name = oColumns.ElementNames(j) Then
vIndexes(iCount) = j + 1
Exit For
End If
Next j
End If
End If
Next i
ElseIf ocTarget._Type = OBJCONTROL Then ' Control within a grid tbc
If IsNull(ocTarget.ControlModel.BoundField) Then Goto Error_Target ' Control MUST be bound to a database record or query
' BoundField is in ControlModel, thanks PASTIM !
.OnlyCurrentField = acCurrent
vParentGrid = getObject(_getUpperShortcut(ocTarget._Shortcut, ocTarget._Name))
If vParentGrid.SubType <> CTLGRIDCONTROL Then Goto Error_Target
.GridControl = vParentGrid._Shortcut
ofParentForm = getObject(_getUpperShortcut(vParentGrid._Shortcut, vParentGrid._Name))
If ofParentForm._Type = OBJCONTROL Then Set ofParentForm = ofParentForm.Form ' Bug Tombola
If IsNull(ofParentForm.DatabaseForm) Then Goto Error_DatabaseForm
.Target = ocTarget._Shortcut
Set vDataField = ocTarget.ControlModel.BoundField
If Not _CheckColumnType(pvFindWhat, vDataField) Then Goto Error_Target
ReDim vNames(0), vIndexes(0)
vNames(0) = ocTarget._Name
Set oColumns = ofParentForm.DatabaseForm.createResultSet().Columns
For j = 0 To oColumns.Count - 1
If vDataField.Name = oColumns.ElementNames(j) Then
vIndexes(0) = j + 1
Exit For
End If
Next j
End If
Case Else ' Determine focus
iCount = Application.Forms()._Count
If iCount = 0 Then Goto Error_ActiveForm
bFound = False
For i = 0 To iCount - 1 ' Determine form having the focus
Set ofParentForm = Application.Forms(i)
If ofParentForm.Component.CurrentController.Frame.IsActive() Then
bFound = True
Exit For
End If
Next i
If Not bFound Then Goto Error_ActiveForm
If IsNull(ofParentForm.DatabaseForm) Then Goto Error_DatabaseForm
iCount = ofParentForm.Controls().Count
bFound = False
For i = 0 To iCount - 1
Set ocGridControl = ofParentForm.Controls(i)
If ocGridControl.SubType = CTLGRIDCONTROL Then
bFound = True
Exit For
End If
Next i
If Not bFound Then Goto Error_NoGrid
.GridControl= ocGridControl._Shortcut
iFocus = -1
iFocus = ocGridControl.ControlView.getCurrentColumnPosition() ' Deprecated but no alternative found !!
If pvTargetedField = acAll Or iFocus < 0 Or iFocus >= ocGridControl.ControlModel.Count Then ' Has a control within the grid the focus ? NO
.OnlyCurrentField = acAll
Set oColumns = ofParentForm.DatabaseForm.createResultSet().Columns
iCount = -1
For i = 0 To ocGridControl.ControlModel.Count - 1
Set vColumn = ocGridControl.ControlModel.getByIndex(i)
Set vDataField = vColumn.BoundField ' examine field type
If Not IsNull(vDataField) Then
If _CheckColumnType(pvFindWhat, vDataField) Then
iCount = iCount + 1
ReDim Preserve vNames(0 To iCount)
vNames(iCount) = vColumn.Name
ReDim Preserve vIndexes(0 To iCount)
For j = 0 To oColumns.Count - 1
If vDataField.Name = oColumns.ElementNames(j) Then
vIndexes(iCount) = j + 1
Exit For
End If
Next j
End If
End If
Next i
Else ' Has a control within the grid the focus ? YES
.OnlyCurrentField = acCurrent
Set vColumn = ocGridControl.ControlModel.getByIndex(iFocus)
Set ocTarget = ocGridControl.Controls(vColumn.Name)
.Target = ocTarget._Shortcut
Set vDataField = ocTarget.ControlModel.BoundField
If IsNull(vDataField) Then Goto Error_Target ' Control MUST be bound to a database record or query
If Not _CheckColumnType(pvFindWhat, vDataField) Then Goto Error_Target
ReDim vNames(0), vIndexes(0)
vNames(0) = ocTarget._Name
Set oColumns = ofParentForm.DatabaseForm.createResultSet().Columns
For j = 0 To oColumns.Count - 1
If vDataField.Name = oColumns.ElementNames(j) Then
vIndexes(0) = j + 1
Exit For
End If
Next j
End If
End Select
.Form = ofParentForm._Shortcut
.LastColumn = UBound(vNames)
.ColumnNames = vNames
.ResultSetIndex = vIndexes
If pvFindFirst Then
Select Case pvSearch
Case acDown, acSearchAll
ofParentForm.DatabaseForm.beforeFirst()
.LastRow = 0
Case acUp
ofParentForm.DatabaseForm.afterLast()
.LastRow = ofParentForm.DatabaseForm.RowCount + 1
End Select
Else
Select Case True
Case ofParentForm.DatabaseForm.isBeforeFirst And (pvSearch = acSearchAll Or pvSearch = acDown)
.LastRow = 0
Case ofParentForm.DatabaseForm.isAfterLast And pvSearch = acUp
ofParentForm.DatabaseForm.last() ' RowCount produces a wrong value as long as last record has not been reached
.LastRow = ofParentForm.DatabaseForm.RowCount + 1
Case Else
.LastRow = ofParentForm.DatabaseForm.getRow()
End Select
End If
.FindRecord = 1
End With
Set _A2B_.FindRecord = oFindRecord
FindRecord = DoCmd.Findnext()
Exit_Function:
Utils._ResetCalledSub("FindRecord")
Exit Function
Error_Function:
TraceError(TRACEABORT, Err, "FindRecord", Erl)
GoTo Exit_Function
Error_ActiveForm:
TraceError(TRACEERRORS, ERRNOACTIVEFORM, Utils._CalledSub(), 0)
Goto Exit_Function
Error_DatabaseForm:
TraceError(TRACEFATAL, ERRDATABASEFORM, Utils._CalledSub(), 0, 1, vParentForm._Name)
Goto Exit_Function
Error_Target:
TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), 0, 1, Array(6, pvTargetedField))
Goto Exit_Function
Error_NoGrid:
TraceError(TRACEFATAL, ERRNOGRIDINFORM, Utils._CalledSub(), 0, 1, vParentForm._Name)
Goto Exit_Function
End Function ' FindRecord V1.1.0
REM -----------------------------------------------------------------------------------------------------------------------
Public Function GetHiddenAttribute(ByVal Optional pvObjectType As Variant _
, ByVal Optional pvObjectName As Variant _
) As Boolean
If _ErrorHandler() Then On Local Error Goto Error_Function
Const cstThisSub = "GetHiddenAttribute"
Utils._SetCalledSub(cstThisSub)
If IsMissing(pvObjectType) Then Call _TraceArguments()
If Not Utils._CheckArgument(pvObjectType, 1, Utils._AddNumeric(), _
Array(acDiagram, acForm, acQuery, acTable, acReport, acBasicIDE, acDatabaseWindow, acDocument) _
) Then Goto Exit_Function
If IsMissing(pvObjectName) Then
Select Case pvObjectType
Case acForm, acQuery, acTable, acReport, acDocument : Call _TraceArguments()
Case Else
End Select
pvObjectName = ""
Else
If Not Utils._CheckArgument(pvObjectName, 2, vbString) Then Goto Exit_Function
End If
Dim oWindow As Object
Set oWindow = _SelectWindow(pvObjectType, pvObjectName)
If IsNull(oWindow.Frame) Then Goto Error_NotFound
GetHiddenAttribute = Not oWindow.Frame.ContainerWindow.isVisible()
Exit_Function:
Utils._ResetCalledSub(cstThisSub)
Exit Function
Error_NotFound:
TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel("OBJECT"), pvObjectName))
Goto Exit_Function
Error_Function:
TraceError(TRACEABORT, Err, cstThisSub, Erl)
GoTo Exit_Function
End Function ' GetHiddenAttribute V1.1.0
REM -----------------------------------------------------------------------------------------------------------------------
Public Function GoToControl(Optional ByVal pvControlName As variant) As Boolean
' Set the focus on the named control on the active form.
' Return False if the control does not exist or is disabled,
If _ErrorHandler() Then On Local Error Goto Error_Function
Utils._SetCalledSub("GoToControl")
If IsMissing(pvControlName) Then Call _TraceArguments()
If Not Utils._CheckArgument(pvControlName, 1, vbString) Then Goto Exit_Function
GoToControl = False
Dim oWindow As Object, ofForm As Object, ocControl As Object
Dim i As Integer, iCount As Integer
Set oWindow = _SelectWindow()
If oWindow.WindowType = acForm Then
Set ofForm = Application.Forms(oWindow._Name)
iCount = ofForm.Controls().Count
For i = 0 To iCount - 1
ocControl = ofForm.Controls(i)
If UCase(ocControl._Name) = UCase(pvControlName) Then
If Methods.hasProperty(ocControl, "Enabled") Then
If ocControl.Enabled Then
ocControl.setFocus()
GoToControl = True
Exit For
End If
End If
End If
Next i
End If
Exit_Function:
Utils._ResetCalledSub("GoToControl")
Exit Function
Error_Function:
TraceError(TRACEABORT, Err, "GoToControl", Erl)
GoTo Exit_Function
End Function ' GoToControl V0.9.0
REM -----------------------------------------------------------------------------------------------------------------------
Public Function GoToRecord(Optional ByVal pvObjectType As Variant _
, Optional ByVal pvObjectName As Variant _
, Optional ByVal pvRecord As Variant _
, Optional ByVal pvOffset As Variant _
) As Boolean
'Move to record indicated by pvRecord/pvOffset in the window designated by pvObjectType and pvObjectName
If _ErrorHandler() Then On Local Error Goto Error_Function
GoToRecord = False
Const cstThisSub = "GoTorecord"
Utils._SetCalledSub(cstThisSub)
If IsMissing(pvObjectName) Then pvObjectName = ""
If IsMissing(pvObjectType) Then pvObjectType = acActiveDataObject
If IsMissing(pvRecord) Then pvRecord = acNext
If IsMissing(pvOffset) Then pvOffset = 1
If Not (Utils._CheckArgument(pvObjectType, 1, Utils._AddNumeric() _
, Array(acActiveDataObject, acDataForm, acDataQuery, acDataTable)) _
And Utils._CheckArgument(pvObjectName, 2, vbString) _
And Utils._CheckArgument(pvRecord, 3, Utils._AddNumeric() _
, Array(acFirst, acGoTo, acLast, acNewRec, acNext, acPrevious)) _
And Utils._CheckArgument(pvOffset, 4, Utils._AddNumeric()) _
) Then Goto Exit_Function
If pvObjectType = acActiveDataObject And pvObjectName <> "" Then Goto Error_Target
If pvOffset < 0 And pvRecord <> acGoTo Then Goto Error_Offset
Dim ofForm As Object, oGeneric As Object, oResultSet As Object, oWindow As Object
Dim i As Integer, iCount As Integer, bFound As Boolean, lOffset As Long
Dim sObjectName, iLengthName As Integer
Select Case pvObjectType
Case acActiveDataObject
Set oWindow = _SelectWindow()
With oWindow
Select Case .WindowType
Case acForm
Set oResultSet = _DatabaseForm(._Name, "")
Case acQuery, acTable
If IsNull(.Frame.Controller.FormOperations) Then Goto Error_NotApplicable
' FormOperations returns <Null> in OpenOffice
Set oResultSet = .Frame.Controller.FormOperations.Cursor
Case Else ' Ignore action
Goto Exit_Function
End Select
End With
Case acDataForm
' pvObjectName can be "myForm", "Forms!myForm", "Forms!myForm!mySubform" or "Forms!myForm!mySubform.Form"
sObjectName = UCase(pvObjectName)
iLengthName = Len(sObjectName)
Select Case True
Case iLengthName > 6 And Left(sObjectName, 6) = "FORMS!" And Right(sObjectName, 5) = ".FORM"
Set ofForm = getObject(pvObjectName)
If ofForm._Type <> OBJSUBFORM Then Goto Error_Target
Case iLengthName > 6 And Left(sObjectName, 6) = "FORMS!"
Set oGeneric = getObject(pvObjectName)
If oGeneric._Type = OBJFORM Or oGeneric._Type = OBJSUBFORM Then
Set ofForm = oGeneric
ElseIf oGeneric.SubType = CTLSUBFORM Then
Set ofForm = oGeneric.Form
Else Goto Error_Target
End If
Case sObjectName = ""
Call _TraceArguments()
Case Else
Set ofForm = Application.Forms(pvObjectName)
End Select
Set oResultSet = ofForm.DatabaseForm
Case acDataQuery
Set oWindow = _SelectWindow(acQuery, pvObjectName)
If IsNull(oWindow.Frame.Controller.FormOperations) Then Goto Error_NotApplicable
' FormOperations returns <Null> in OpenOffice
Set oResultSet = oWindow.Frame.Controller.FormOperations.Cursor
Case acDataTable
Set oWindow = _SelectWindow(acTable, pvObjectName)
If IsNull(oWindow.Frame.Controller.FormOperations) Then Goto Error_NotApplicable
Set oResultSet = oWindow.Frame.Controller.FormOperations.Cursor
Case Else
End Select
' Check if current row updated => Save it
If oResultSet.IsNew Then
oResultSet.insertRow()
ElseIf oResultSet.IsModified Then
oResultSet.updateRow()
End If
lOffset = pvOffset
Select Case pvRecord
Case acFirst : GoToRecord = oResultSet.first()
Case acGoTo : GoToRecord = oResultSet.absolute(lOffset)
Case acLast : GoToRecord = oResultSet.last()
Case acNewRec
oResultSet.last() ' To simulate the behaviour in the UI
oResultSet.moveToInsertRow()
GoToRecord = True
Case acNext
If lOffset = 1 Then
GoToRecord = oResultSet.next()
Else
GoToRecord = oResultSet.relative(lOffset)
End If
Case acPrevious
If lOffset = 1 Then
GoToRecord = oResultSet.previous()
Else
GoToRecord = oResultSet.relative(- lOffset)
End If
End Select
Exit_Function:
Utils._ResetCalledSub(cstThisSub)
Exit Function
Error_Function:
TraceError(TRACEABORT, Err, cstThisSub, Erl)
GoTo Exit_Function
Error_Target:
TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), 0, 1, Array(2, pvObjectName))
Goto Exit_Function
Error_Offset:
TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), 0, 1, Array(4, pvOffset))
Goto Exit_Function
Error_NotApplicable:
TraceError(TRACEFATAL, ERRACTION, Utils._CalledSub(), 0, 1, cstThisSub)
Goto Exit_Function
End Function ' GoToRecord
REM -----------------------------------------------------------------------------------------------------------------------
Public Function Maximize() As Boolean
' Maximize the window having the focus
Utils._SetCalledSub("Maximize")
Dim oWindow As Object
Maximize = False
Set oWindow = _SelectWindow()
If Not IsNull(oWindow.Frame) Then
If Utils._hasUNOProperty(oWindow.Frame.ContainerWindow, "IsMaximized") Then oWindow.Frame.ContainerWindow.IsMaximized = True ' Ignored when <= OO3.2
Maximize = True
End If