1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"Collect" script:
language=
"StarBasic">
4 REM =======================================================================================================================
5 REM === The Access2Base library is a part of the LibreOffice project. ===
6 REM === Full documentation is available on http://www.access2base.com ===
7 REM =======================================================================================================================
14 REM MODULE NAME
<> COLLECTION (is a reserved name for ... collections)
16 REM -----------------------------------------------------------------------------------------------------------------------
17 REM --- CLASS ROOT FIELDS ---
18 REM -----------------------------------------------------------------------------------------------------------------------
20 Private _Type As String
' Must be COLLECTION
21 Private _This As Object
' Workaround for absence of This builtin function
22 Private _CollType As String
23 Private _Parent As Object
24 Private _Count As Long
26 REM -----------------------------------------------------------------------------------------------------------------------
27 REM --- CONSTRUCTORS / DESTRUCTORS ---
28 REM -----------------------------------------------------------------------------------------------------------------------
29 Private Sub Class_Initialize()
32 _CollType =
""
35 End Sub
' Constructor
37 REM -----------------------------------------------------------------------------------------------------------------------
38 Private Sub Class_Terminate()
39 On Local Error Resume Next
40 Call Class_Initialize()
41 End Sub
' Destructor
43 REM -----------------------------------------------------------------------------------------------------------------------
45 Call Class_Terminate()
46 End Sub
' Explicit destructor
48 REM -----------------------------------------------------------------------------------------------------------------------
49 REM --- CLASS GET/LET/SET PROPERTIES ---
50 REM -----------------------------------------------------------------------------------------------------------------------
52 Property Get Count() As Long
53 Count = _PropertyGet(
"Count
")
54 End Property
' Count (get)
56 REM -----------------------------------------------------------------------------------------------------------------------
57 Function Item(ByVal Optional pvItem As Variant) As Variant
58 'Return property value.
59 'pvItem either numeric index or property name
61 Const cstThisSub =
"Collection.getItem
"
63 If _ErrorHandler() Then On Local Error Goto Error_Function
65 Utils._SetCalledSub(cstThisSub)
66 If IsMissing(pvItem) Then Goto Exit_Function
' To allow object watching in Basic IDE, do not generate error
68 Case COLLCOMMANDBARCONTROLS
' Have no name
69 If Not Utils._CheckArgument(pvItem,
1, Utils._AddNumeric()) Then Goto Exit_Function
71 If Not Utils._CheckArgument(pvItem,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
74 Dim vNames() As Variant, oProperty As Object
79 Set Item = Application.AllDialogs(pvItem)
81 Set Item = Application.AllForms(pvItem)
83 Set Item = Application.AllModules(pvItem)
85 Set Item = Application.CommandBars(pvItem)
86 Case COLLCOMMANDBARCONTROLS
87 If IsNull(_Parent) Then GoTo Error_Parent
88 Set Item = _Parent.CommandBarControls(pvItem)
90 If IsNull(_Parent) Then GoTo Error_Parent
91 Set Item = _Parent.Controls(pvItem)
93 Set Item = Application.Forms(pvItem)
95 If IsNull(_Parent) Then GoTo Error_Parent
96 Set Item = _Parent.Fields(pvItem)
98 If IsNull(_Parent) Then GoTo Error_Parent
99 Select Case _Parent._Type
100 Case OBJCONTROL, OBJSUBFORM, OBJDATABASE, OBJDIALOG, OBJFIELD _
101 , OBJFORM, OBJQUERYDEF, OBJRECORDSET, OBJTABLEDEF
102 Set Item = _Parent.Properties(pvItem)
103 Case OBJCOLLECTION, OBJEVENT, OBJOPTIONGROUP, OBJPROPERTY
107 Set Item = _Parent.QueryDefs(pvItem)
109 Set Item = _Parent.Recordsets(pvItem)
111 Set Item = _Parent.TableDefs(pvItem)
113 Set Item = Application.TempVars(pvItem)
118 Utils._ResetCalledSub(cstThisSub)
121 TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
125 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, True, Array(_GetLabel(
"OBJECT
"), _GetLabel(
"PARENT
")))
128 End Function
' Item V1.1
.0
130 REM -----------------------------------------------------------------------------------------------------------------------
131 Property Get ObjectType() As String
132 ObjectType = _PropertyGet(
"ObjectType
")
133 End Property
' ObjectType (get)
135 REM -----------------------------------------------------------------------------------------------------------------------
136 Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
138 ' a Collection object if pvIndex absent
139 ' a Property object otherwise
141 Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
142 vPropertiesList = _PropertiesList()
143 sObject = Utils._PCase(_Type)
144 If IsMissing(pvIndex) Then
145 vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList)
147 vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList, pvIndex)
148 vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
152 Set Properties = vProperty
154 End Function
' Properties
156 REM -----------------------------------------------------------------------------------------------------------------------
157 REM --- CLASS METHODS ---
158 REM -----------------------------------------------------------------------------------------------------------------------
160 Public Function Add(Optional pvNew As Variant, Optional pvValue As Variant) As Boolean
161 ' Append a new TableDef or TempVar object to the TableDefs/TempVars collections
163 Const cstThisSub =
"Collection.Add
"
164 Utils._SetCalledSub(cstThisSub)
165 If _ErrorHandler() Then On Local Error Goto Error_Function
167 Dim odbDatabase As Object, oConnection As Object, oTables As Object, oTable As Object
168 Dim vObject As Variant, oTempVar As Object
170 If IsMissing(pvNew) Then Call _TraceArguments()
172 Select Case _CollType
174 If Not Utils._CheckArgument(pvNew,
1, vbObject) Then Goto Exit_Function
177 Set odbDatabase = ._ParentDatabase
178 If odbDatabase._DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
179 Set oConnection = odbDatabase.Connection
180 If IsNull(.TableDescriptor) Or .TableFieldsCount =
0 Then Goto Error_Sequence
181 Set oTables = oConnection.getTables()
182 oTables.appendByDescriptor(.TableDescriptor)
183 Set .Table = oTables.getByName(._Name)
184 .CatalogName = .Table.CatalogName
185 .SchemaName = .Table.SchemaName
186 .TableName = .Table.Name
187 .TableDescriptor.dispose()
188 Set .TableDescriptor = Nothing
189 .TableFieldsCount =
0
193 If Not Utils._CheckArgument(pvNew,
1, vbString) Then Goto Exit_Function
194 If pvNew =
"" Then Goto Error_Name
195 If IsMissing(pvValue) Then Call _TraceArguments()
196 If _A2B_.hasItem(COLLTEMPVARS, pvNew) Then Goto Error_Name
197 Set oTempVar = New TempVar
198 oTempVar._This = oTempVar
199 oTempVar._Name = pvNew
200 oTempVar._Value = pvValue
201 _A2B_.TempVars.Add(oTempVar, UCase(pvNew))
203 Goto Error_NotApplicable
210 Utils._ResetCalledSub(cstThisSub)
213 TraceError(TRACEABORT, Err, cstThisSub, Erl)
216 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
219 TraceError(TRACEFATAL, ERRTABLECREATION, Utils._CalledSub(),
0,
1, vObject._Name)
222 TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), False, ,Array(
1, pvNew))
225 End Function
' Add V1.1
.0
227 REM -----------------------------------------------------------------------------------------------------------------------
228 Public Function Delete(ByVal Optional pvName As Variant) As Boolean
229 ' Delete a TableDef or QueryDef object in the TableDefs/QueryDefs collections
231 Const cstThisSub =
"Collection.Delete
"
232 Utils._SetCalledSub(cstThisSub)
233 If _ErrorHandler() Then On Local Error Goto Error_Function
235 Dim odbDatabase As Object, oColl As Object, vName As Variant
237 If IsMissing(pvName) Then pvName =
""
238 If Not Utils._CheckArgument(pvName,
1, vbString) Then Goto Exit_Function
239 If pvName =
"" Then Call _TraceArguments()
241 Select Case _CollType
242 Case COLLTABLEDEFS, COLLQUERYDEFS
243 If _A2B_.CurrentDocIndex()
<> 0 Then Goto Error_NotApplicable
244 Set odbDatabase = Application._CurrentDb()
245 If odbDatabase._DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
246 If _CollType = COLLTABLEDEFS Then Set oColl = odbDatabase.Connection.getTables() Else Set oColl = odbDatabase.Connection.getQueries()
248 vName = _InList(pvName, .getElementNames(), True)
249 If vName = False Then Goto trace_NotFound
252 odbDatabase.Document.store()
254 Goto Error_NotApplicable
261 Utils._ResetCalledSub(cstThisSub)
264 TraceError(TRACEABORT, Err, cstThisSub, Erl)
267 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
270 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(Left(_CollType,
5)), pvName))
272 End Function
' Delete V1.1
.0
274 REM -----------------------------------------------------------------------------------------------------------------------
275 Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
276 ' Return property value of psProperty property name
278 Utils._SetCalledSub(
"Collection.getProperty
")
279 If IsMissing(pvProperty) Then Call _TraceArguments()
280 getProperty = _PropertyGet(pvProperty)
281 Utils._ResetCalledSub(
"Collection.getProperty
")
283 End Function
' getProperty
285 REM -----------------------------------------------------------------------------------------------------------------------
286 Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
287 ' Return True if object has a valid property called pvProperty (case-insensitive comparison !)
289 If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
292 End Function
' hasProperty
294 REM -----------------------------------------------------------------------------------------------------------------------
295 Public Function Remove(ByVal Optional pvName As Variant) As Boolean
296 ' Remove a TempVar from the TempVars collection
298 Const cstThisSub =
"Collection.Remove
"
299 Utils._SetCalledSub(cstThisSub)
300 If _ErrorHandler() Then On Local Error Goto Error_Function
302 Dim oColl As Object, vName As Variant
304 If IsMissing(pvName) Then pvName =
""
305 If Not Utils._CheckArgument(pvName,
1, vbString) Then Goto Exit_Function
306 If pvName =
"" Then Call _TraceArguments()
308 Select Case _CollType
310 If Not _A2B_.hasItem(COLLTEMPVARS, pvName) Then Goto Error_Name
311 _A2B_.TempVars.Remove(UCase(pvName))
313 Goto Error_NotApplicable
320 Utils._ResetCalledSub(cstThisSub)
323 TraceError(TRACEABORT, Err, cstThisSub, Erl)
326 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
329 TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), False, ,Array(
1, pvName))
332 End Function
' Remove V1.2
.0
334 REM -----------------------------------------------------------------------------------------------------------------------
335 Public Function RemoveAll() As Boolean
336 ' Remove the whole TempVars collection
338 Const cstThisSub =
"Collection.Remove
"
339 Utils._SetCalledSub(cstThisSub)
340 If _ErrorHandler() Then On Local Error Goto Error_Function
342 Select Case _CollType
344 Set _A2B_.TempVars = New Collection
347 Goto Error_NotApplicable
351 Utils._ResetCalledSub(cstThisSub)
354 TraceError(TRACEABORT, Err, cstThisSub, Erl)
357 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
359 End Function
' RemoveAll V1.2
.0
361 REM -----------------------------------------------------------------------------------------------------------------------
362 REM --- PRIVATE FUNCTIONS ---
363 REM -----------------------------------------------------------------------------------------------------------------------
364 Private Function _PropertiesList() As Variant
365 _PropertiesList = Array(
"Count
",
"Item
",
"ObjectType
")
366 End Function
' _PropertiesList
368 REM -----------------------------------------------------------------------------------------------------------------------
369 Private Function _PropertyGet(ByVal psProperty As String) As Variant
370 ' Return property value of the psProperty property name
372 If _ErrorHandler() Then On Local Error Goto Error_Function
373 Utils._SetCalledSub(
"Collection.get
" & psProperty)
374 _PropertyGet = Nothing
376 Select Case UCase(psProperty)
377 Case UCase(
"Count
")
378 _PropertyGet = _Count
379 Case UCase(
"Item
")
380 Case UCase(
"ObjectType
")
387 Utils._ResetCalledSub(
"Collection.get
" & psProperty)
390 TraceError(TRACEWARNING, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
391 _PropertyGet = Nothing
394 TraceError(TRACEABORT, Err,
"Collection._PropertyGet
", Erl)
395 _PropertyGet = Nothing
397 End Function
' _PropertyGet