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">REM =======================================================================================================================
4 REM === The Access2Base library is a part of the LibreOffice project. ===
5 REM === Full documentation is available on http://www.access2base.com ===
6 REM =======================================================================================================================
13 REM MODULE NAME
<> COLLECTION (seems a reserved name ?)
15 REM -----------------------------------------------------------------------------------------------------------------------
16 REM --- CLASS ROOT FIELDS ---
17 REM -----------------------------------------------------------------------------------------------------------------------
19 Private _Type As String
' Must be COLLECTION
20 Private _CollType As String
21 Private _ParentType As String
22 Private _ParentName As String
' Name or shortcut
23 Private _ParentDatabase As Object
24 Private _Count As Long
26 REM -----------------------------------------------------------------------------------------------------------------------
27 REM --- CONSTRUCTORS / DESTRUCTORS ---
28 REM -----------------------------------------------------------------------------------------------------------------------
29 Private Sub Class_Initialize()
31 _CollType =
""
32 _ParentType =
""
33 _ParentName =
""
34 Set _ParentDatabase = Nothing
36 End Sub
' Constructor
38 REM -----------------------------------------------------------------------------------------------------------------------
39 Private Sub Class_Terminate()
40 On Local Error Resume Next
41 Call Class_Initialize()
42 End Sub
' Destructor
44 REM -----------------------------------------------------------------------------------------------------------------------
46 Call Class_Terminate()
47 End Sub
' Explicit destructor
49 REM -----------------------------------------------------------------------------------------------------------------------
50 REM --- CLASS GET/LET/SET PROPERTIES ---
51 REM -----------------------------------------------------------------------------------------------------------------------
53 Property Get Count() As Long
54 Count = _PropertyGet(
"Count
")
55 End Property
' Count (get)
57 REM -----------------------------------------------------------------------------------------------------------------------
58 Property Get Item(ByVal Optional pvItem As Variant) As Variant
59 'Return property value.
60 'pvItem either numeric index or property name
62 Const cstThisSub =
"Collection.getItem
"
63 Utils._SetCalledSub(cstThisSub)
64 If IsMissing(pvItem) Then Goto Exit_Function
' To allow object watching in Basic IDE, do not generate error
66 Case COLLCOMMANDBARCONTROLS
' Have no name
67 If Not Utils._CheckArgument(pvItem,
1, Utils._AddNumeric()) Then Goto Exit_Function
69 If Not Utils._CheckArgument(pvItem,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
72 Dim vNames() As Variant, oProperty As Object
77 Set Item = Application.AllDialogs(pvItem)
79 Set Item = Application.AllForms(pvItem)
81 Set Item = Application.CommandBars(pvItem)
82 Case COLLCOMMANDBARCONTROLS
83 Set Item = Application.CommandBars(_ParentName).CommandBarControls(pvItem)
85 Select Case _ParentType
86 Case OBJCONTROL, OBJSUBFORM
87 Set Item = getObject(_ParentName).Controls(pvItem)
89 Set Item = Application.AllDialogs(_ParentName).Controls(pvItem)
91 Set Item = Application.Forms(_ParentName).Controls(pvItem)
96 Set Item = Application.Forms(pvItem)
98 Select Case _ParentType
100 Set Item = _ParentDatabase.QueryDefs(_ParentName).Fields(pvItem)
102 Set Item = _ParentDatabase.Recordsets(_ParentName).Fields(pvItem)
104 Set Item = _ParentDatabase.TableDefs(_ParentName).Fields(pvItem)
107 Select Case _ParentType
108 Case OBJCONTROL, OBJSUBFORM
109 Set Item = getObject(_ParentName).Properties(pvItem)
111 Set Item = _ParentDatabase.Properties(pvItem)
113 Set Item = Application.AllDialogs(_ParentName).Properties(pvItem)
115 vNames() = Split(_ParentName,
"/
")
116 Select Case vNames(
0)
118 Set Item = _ParentDatabase.QueryDefs(vNames(
1)).Fields(vNames(
2)).Properties(pvItem)
120 Set Item = _ParentDatabase.Recordsets(vNames(
1)).Fields(vNames(
2)).Properties(pvItem)
122 Set Item = _ParentDatabase.TableDefs(vNames(
1)).Fields(vNames(
2)).Properties(pvItem)
125 Set Item = Application.Forms(_ParentName).Properties(pvItem)
127 Set Item = _ParentDatabase.QueryDefs(_ParentName).Properties(pvItem)
129 Set Item = _ParentDatabase.Recordsets(_ParentName).Properties(pvItem)
131 Set Item = _ParentDatabase.TableDefs(_ParentName).Properties(pvItem)
132 Case OBJCOLLECTION, OBJEVENT, OBJOPTIONGROUP, OBJPROPERTY
136 Set Item = _ParentDatabase.QueryDefs(pvItem)
138 Set Item = _ParentDatabase.Recordsets(pvItem)
140 Set Item = _ParentDatabase.TableDefs(pvItem)
142 Set Item = Application.TempVars(pvItem)
147 Utils._ResetCalledSub(cstThisSub)
150 TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
153 End Property
' V1.1
.0
155 REM -----------------------------------------------------------------------------------------------------------------------
156 Property Get ObjectType() As String
157 ObjectType = _PropertyGet(
"ObjectType
")
158 End Property
' ObjectType (get)
160 REM -----------------------------------------------------------------------------------------------------------------------
161 Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
163 ' a Collection object if pvIndex absent
164 ' a Property object otherwise
166 Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
167 vPropertiesList = _PropertiesList()
168 sObject = Utils._PCase(_Type)
169 If IsMissing(pvIndex) Then
170 vProperty = PropertiesGet._Properties(sObject, _ParentName, vPropertiesList)
172 vProperty = PropertiesGet._Properties(sObject, _ParentName, vPropertiesList, pvIndex)
173 vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
177 Set Properties = vProperty
179 End Function
' Properties
181 REM -----------------------------------------------------------------------------------------------------------------------
182 REM --- CLASS METHODS ---
183 REM -----------------------------------------------------------------------------------------------------------------------
185 Public Function Add(Optional pvNew As Variant, Optional pvValue As Variant) As Boolean
186 ' Append a new TableDef or TempVar object to the TableDefs/TempVars collections
188 Const cstThisSub =
"Collection.Add
"
189 Utils._SetCalledSub(cstThisSub)
190 If _ErrorHandler() Then On Local Error Goto Error_Function
192 Dim odbDatabase As Object, oConnection As Object, oTables As Object, oTable As Object
193 Dim vObject As Variant, oTempVar As Object
195 If IsMissing(pvNew) Then Call _TraceArguments()
197 Select Case _CollType
199 If Not Utils._CheckArgument(pvNew,
1, vbObject) Then Goto Exit_Function
202 Set odbDatabase = ._ParentDatabase
203 If odbDatabase._DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
204 Set oConnection = odbDatabase.Connection
205 If IsNull(.TableDescriptor) Or .TableFieldsCount =
0 Then Goto Error_Sequence
206 Set oTables = oConnection.getTables()
207 oTables.appendByDescriptor(.TableDescriptor)
208 Set .Table = oTables.getByName(._Name)
209 .TableDescriptor.dispose()
210 Set .TableDescriptor = Nothing
211 .TableFieldsCount =
0
215 If Not Utils._CheckArgument(pvNew,
1, vbString) Then Goto Exit_Function
216 If pvNew =
"" Then Goto Error_Name
217 If IsMissing(pvValue) Then Call _TraceArguments()
218 If _A2B_.hasItem(COLLTEMPVARS, pvNew) Then Goto Error_Name
219 Set oTempVar = New TempVar
220 oTempVar._Name = pvNew
221 oTempVar._Value = pvValue
222 _A2B_.TempVars.Add(oTempVar, UCase(pvNew))
224 Goto Error_NotApplicable
231 Utils._ResetCalledSub(cstThisSub)
234 TraceError(TRACEABORT, Err, cstThisSub, Erl)
237 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
240 TraceError(TRACEFATAL, ERRTABLECREATION, Utils._CalledSub(),
0,
1, vObject._Name)
243 TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), False, ,Array(
1, pvNew))
246 End Function
' Add V1.1
.0
248 REM -----------------------------------------------------------------------------------------------------------------------
249 Public Function Delete(ByVal Optional pvName As Variant) As Boolean
250 ' Delete a TableDef or QueryDef object in the TableDefs/QueryDefs collections
252 Const cstThisSub =
"Collection.Delete
"
253 Utils._SetCalledSub(cstThisSub)
254 If _ErrorHandler() Then On Local Error Goto Error_Function
256 Dim odbDatabase As Object, oColl As Object, vName As Variant
258 If IsMissing(pvName) Then pvName =
""
259 If Not Utils._CheckArgument(pvName,
1, vbString) Then Goto Exit_Function
260 If pvName =
"" Then Call _TraceArguments()
262 Select Case _CollType
263 Case COLLTABLEDEFS, COLLQUERYDEFS
264 If _A2B_.CurrentDocIndex()
<> 0 Then Goto Error_NotApplicable
265 Set odbDatabase = Application._CurrentDb()
266 If odbDatabase._DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
267 If _CollType = COLLTABLEDEFS Then Set oColl = odbDatabase.Connection.getTables() Else Set oColl = odbDatabase.Connection.getQueries()
269 vName = _InList(pvName, .getElementNames(), True)
270 If vName = False Then Goto trace_NotFound
273 odbDatabase.Document.store()
275 Goto Error_NotApplicable
282 Utils._ResetCalledSub(cstThisSub)
285 TraceError(TRACEABORT, Err, cstThisSub, Erl)
288 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
291 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(Left(_CollType,
5)), pvName))
293 End Function
' Delete V1.1
.0
295 REM -----------------------------------------------------------------------------------------------------------------------
296 Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
297 ' Return property value of psProperty property name
299 Utils._SetCalledSub(
"Collection.getProperty
")
300 If IsMissing(pvProperty) Then Call _TraceArguments()
301 getProperty = _PropertyGet(pvProperty)
302 Utils._ResetCalledSub(
"Collection.getProperty
")
304 End Function
' getProperty
306 REM -----------------------------------------------------------------------------------------------------------------------
307 Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
308 ' Return True if object has a valid property called pvProperty (case-insensitive comparison !)
310 If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
313 End Function
' hasProperty
315 REM -----------------------------------------------------------------------------------------------------------------------
316 Public Function Remove(ByVal Optional pvName As Variant) As Boolean
317 ' Remove a TempVar from the TempVars collection
319 Const cstThisSub =
"Collection.Remove
"
320 Utils._SetCalledSub(cstThisSub)
321 If _ErrorHandler() Then On Local Error Goto Error_Function
323 Dim oColl As Object, vName As Variant
325 If IsMissing(pvName) Then pvName =
""
326 If Not Utils._CheckArgument(pvName,
1, vbString) Then Goto Exit_Function
327 If pvName =
"" Then Call _TraceArguments()
329 Select Case _CollType
331 If Not _A2B_.hasItem(COLLTEMPVARS, pvName) Then Goto Error_Name
332 _A2B_.TempVars.Remove(UCase(pvName))
334 Goto Error_NotApplicable
341 Utils._ResetCalledSub(cstThisSub)
344 TraceError(TRACEABORT, Err, cstThisSub, Erl)
347 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
350 TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), False, ,Array(
1, pvName))
353 End Function
' Remove V1.2
.0
355 REM -----------------------------------------------------------------------------------------------------------------------
356 Public Function RemoveAll() As Boolean
357 ' Remove the whole TempVars collection
359 Const cstThisSub =
"Collection.Remove
"
360 Utils._SetCalledSub(cstThisSub)
361 If _ErrorHandler() Then On Local Error Goto Error_Function
363 Select Case _CollType
365 Set _A2B_.TempVars = New Collection
368 Goto Error_NotApplicable
372 Utils._ResetCalledSub(cstThisSub)
375 TraceError(TRACEABORT, Err, cstThisSub, Erl)
378 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
380 End Function
' RemoveAll V1.2
.0
382 REM -----------------------------------------------------------------------------------------------------------------------
383 REM --- PRIVATE FUNCTIONS ---
384 REM -----------------------------------------------------------------------------------------------------------------------
385 Private Function _PropertiesList() As Variant
386 _PropertiesList = Array(
"Count
",
"Item
",
"ObjectType
")
387 End Function
' _PropertiesList
389 REM -----------------------------------------------------------------------------------------------------------------------
390 Private Function _PropertyGet(ByVal psProperty As String) As Variant
391 ' Return property value of the psProperty property name
393 If _ErrorHandler() Then On Local Error Goto Error_Function
394 Utils._SetCalledSub(
"Collection.get
" & psProperty)
395 _PropertyGet = Nothing
397 Select Case UCase(psProperty)
398 Case UCase(
"Count
")
399 _PropertyGet = _Count
400 Case UCase(
"Item
")
401 Case UCase(
"ObjectType
")
408 Utils._ResetCalledSub(
"Collection.get
" & psProperty)
411 TraceError(TRACEWARNING, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
412 _PropertyGet = Nothing
415 TraceError(TRACEABORT, Err,
"Collection._PropertyGet
", Erl)
416 _PropertyGet = Nothing
418 End Function
' _PropertyGet