Collect.xba 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 =======================================================================================================================
  8. Option Compatible
  9. Option ClassModule
  10. Option Explicit
  11. REM MODULE NAME &lt;&gt; COLLECTION (is a reserved name for ... collections)
  12. REM -----------------------------------------------------------------------------------------------------------------------
  13. REM --- CLASS ROOT FIELDS ---
  14. REM -----------------------------------------------------------------------------------------------------------------------
  15. Private _Type As String &apos; Must be COLLECTION
  16. Private _This As Object &apos; Workaround for absence of This builtin function
  17. Private _CollType As String
  18. Private _Parent As Object
  19. Private _Count As Long
  20. REM -----------------------------------------------------------------------------------------------------------------------
  21. REM --- CONSTRUCTORS / DESTRUCTORS ---
  22. REM -----------------------------------------------------------------------------------------------------------------------
  23. Private Sub Class_Initialize()
  24. _Type = OBJCOLLECTION
  25. Set _This = Nothing
  26. _CollType = &quot;&quot;
  27. Set _Parent = Nothing
  28. _Count = 0
  29. End Sub &apos; Constructor
  30. REM -----------------------------------------------------------------------------------------------------------------------
  31. Private Sub Class_Terminate()
  32. On Local Error Resume Next
  33. Call Class_Initialize()
  34. End Sub &apos; Destructor
  35. REM -----------------------------------------------------------------------------------------------------------------------
  36. Public Sub Dispose()
  37. Call Class_Terminate()
  38. End Sub &apos; Explicit destructor
  39. REM -----------------------------------------------------------------------------------------------------------------------
  40. REM --- CLASS GET/LET/SET PROPERTIES ---
  41. REM -----------------------------------------------------------------------------------------------------------------------
  42. Property Get Count() As Long
  43. Count = _PropertyGet(&quot;Count&quot;)
  44. End Property &apos; Count (get)
  45. REM -----------------------------------------------------------------------------------------------------------------------
  46. Function Item(ByVal Optional pvItem As Variant) As Variant
  47. &apos;Return property value.
  48. &apos;pvItem either numeric index or property name
  49. Const cstThisSub = &quot;Collection.getItem&quot;
  50. If _ErrorHandler() Then On Local Error Goto Error_Function
  51. Utils._SetCalledSub(cstThisSub)
  52. If IsMissing(pvItem) Then Goto Exit_Function &apos; To allow object watching in Basic IDE, do not generate error
  53. Select Case _CollType
  54. Case COLLCOMMANDBARCONTROLS &apos; Have no name
  55. If Not Utils._CheckArgument(pvItem, 1, Utils._AddNumeric()) Then Goto Exit_Function
  56. Case Else
  57. If Not Utils._CheckArgument(pvItem, 1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
  58. End Select
  59. Dim vNames() As Variant, oProperty As Object
  60. Set Item = Nothing
  61. Select Case _CollType
  62. Case COLLALLDIALOGS
  63. Set Item = Application.AllDialogs(pvItem)
  64. Case COLLALLFORMS
  65. Set Item = Application.AllForms(pvItem)
  66. Case COLLALLMODULES
  67. Set Item = Application.AllModules(pvItem)
  68. Case COLLCOMMANDBARS
  69. Set Item = Application.CommandBars(pvItem)
  70. Case COLLCOMMANDBARCONTROLS
  71. If IsNull(_Parent) Then GoTo Error_Parent
  72. Set Item = _Parent.CommandBarControls(pvItem)
  73. Case COLLCONTROLS
  74. If IsNull(_Parent) Then GoTo Error_Parent
  75. Set Item = _Parent.Controls(pvItem)
  76. Case COLLFORMS
  77. Set Item = Application.Forms(pvItem)
  78. Case COLLFIELDS
  79. If IsNull(_Parent) Then GoTo Error_Parent
  80. Set Item = _Parent.Fields(pvItem)
  81. Case COLLPROPERTIES
  82. If IsNull(_Parent) Then GoTo Error_Parent
  83. Select Case _Parent._Type
  84. Case OBJCONTROL, OBJSUBFORM, OBJDATABASE, OBJDIALOG, OBJFIELD _
  85. , OBJFORM, OBJQUERYDEF, OBJRECORDSET, OBJTABLEDEF
  86. Set Item = _Parent.Properties(pvItem)
  87. Case OBJCOLLECTION, OBJEVENT, OBJOPTIONGROUP, OBJPROPERTY
  88. &apos; NOT SUPPORTED
  89. End Select
  90. Case COLLQUERYDEFS
  91. Set Item = _Parent.QueryDefs(pvItem)
  92. Case COLLRECORDSETS
  93. Set Item = _Parent.Recordsets(pvItem)
  94. Case COLLTABLEDEFS
  95. Set Item = _Parent.TableDefs(pvItem)
  96. Case COLLTEMPVARS
  97. Set Item = Application.TempVars(pvItem)
  98. Case Else
  99. End Select
  100. Exit_Function:
  101. Utils._ResetCalledSub(cstThisSub)
  102. Exit Function
  103. Error_Function:
  104. TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
  105. Set Item = Nothing
  106. GoTo Exit_Function
  107. Error_Parent:
  108. TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, True, Array(_GetLabel(&quot;OBJECT&quot;), _GetLabel(&quot;PARENT&quot;)))
  109. Set Item = Nothing
  110. GoTo Exit_Function
  111. End Function &apos; Item V1.1.0
  112. REM -----------------------------------------------------------------------------------------------------------------------
  113. Property Get ObjectType() As String
  114. ObjectType = _PropertyGet(&quot;ObjectType&quot;)
  115. End Property &apos; ObjectType (get)
  116. REM -----------------------------------------------------------------------------------------------------------------------
  117. Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
  118. &apos; Return
  119. &apos; a Collection object if pvIndex absent
  120. &apos; a Property object otherwise
  121. Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
  122. vPropertiesList = _PropertiesList()
  123. sObject = Utils._PCase(_Type)
  124. If IsMissing(pvIndex) Then
  125. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList)
  126. Else
  127. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList, pvIndex)
  128. vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
  129. End If
  130. Exit_Function:
  131. Set Properties = vProperty
  132. Exit Function
  133. End Function &apos; Properties
  134. REM -----------------------------------------------------------------------------------------------------------------------
  135. REM --- CLASS METHODS ---
  136. REM -----------------------------------------------------------------------------------------------------------------------
  137. Public Function Add(Optional pvNew As Variant, Optional pvValue As Variant) As Boolean
  138. &apos; Append a new TableDef or TempVar object to the TableDefs/TempVars collections
  139. Const cstThisSub = &quot;Collection.Add&quot;
  140. Utils._SetCalledSub(cstThisSub)
  141. If _ErrorHandler() Then On Local Error Goto Error_Function
  142. Dim odbDatabase As Object, oConnection As Object, oTables As Object, oTable As Object
  143. Dim vObject As Variant, oTempVar As Object
  144. Add = False
  145. If IsMissing(pvNew) Then Call _TraceArguments()
  146. Select Case _CollType
  147. Case COLLTABLEDEFS
  148. If Not Utils._CheckArgument(pvNew, 1, vbObject) Then Goto Exit_Function
  149. Set vObject = pvNew
  150. With vObject
  151. Set odbDatabase = ._ParentDatabase
  152. If odbDatabase._DbConnect &lt;&gt; DBCONNECTBASE Then Goto Error_NotApplicable
  153. Set oConnection = odbDatabase.Connection
  154. If IsNull(.TableDescriptor) Or .TableFieldsCount = 0 Then Goto Error_Sequence
  155. Set oTables = oConnection.getTables()
  156. oTables.appendByDescriptor(.TableDescriptor)
  157. Set .Table = oTables.getByName(._Name)
  158. .CatalogName = .Table.CatalogName
  159. .SchemaName = .Table.SchemaName
  160. .TableName = .Table.Name
  161. .TableDescriptor.dispose()
  162. Set .TableDescriptor = Nothing
  163. .TableFieldsCount = 0
  164. .TableKeysCount = 0
  165. End With
  166. Case COLLTEMPVARS
  167. If Not Utils._CheckArgument(pvNew, 1, vbString) Then Goto Exit_Function
  168. If pvNew = &quot;&quot; Then Goto Error_Name
  169. If IsMissing(pvValue) Then Call _TraceArguments()
  170. If _A2B_.hasItem(COLLTEMPVARS, pvNew) Then Goto Error_Name
  171. Set oTempVar = New TempVar
  172. oTempVar._This = oTempVar
  173. oTempVar._Name = pvNew
  174. oTempVar._Value = pvValue
  175. _A2B_.TempVars.Add(oTempVar, UCase(pvNew))
  176. Case Else
  177. Goto Error_NotApplicable
  178. End Select
  179. _Count = _Count + 1
  180. Add = True
  181. Exit_Function:
  182. Utils._ResetCalledSub(cstThisSub)
  183. Exit Function
  184. Error_Function:
  185. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  186. GoTo Exit_Function
  187. Error_NotApplicable:
  188. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  189. Goto Exit_Function
  190. Error_Sequence:
  191. TraceError(TRACEFATAL, ERRTABLECREATION, Utils._CalledSub(), 0, 1, vObject._Name)
  192. Goto Exit_Function
  193. Error_Name:
  194. TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), False, ,Array(1, pvNew))
  195. AddItem = False
  196. Goto Exit_Function
  197. End Function &apos; Add V1.1.0
  198. REM -----------------------------------------------------------------------------------------------------------------------
  199. Public Function Delete(ByVal Optional pvName As Variant) As Boolean
  200. &apos; Delete a TableDef or QueryDef object in the TableDefs/QueryDefs collections
  201. Const cstThisSub = &quot;Collection.Delete&quot;
  202. Utils._SetCalledSub(cstThisSub)
  203. If _ErrorHandler() Then On Local Error Goto Error_Function
  204. Dim odbDatabase As Object, oColl As Object, vName As Variant
  205. Delete = False
  206. If IsMissing(pvName) Then pvName = &quot;&quot;
  207. If Not Utils._CheckArgument(pvName, 1, vbString) Then Goto Exit_Function
  208. If pvName = &quot;&quot; Then Call _TraceArguments()
  209. Select Case _CollType
  210. Case COLLTABLEDEFS, COLLQUERYDEFS
  211. If _A2B_.CurrentDocIndex() &lt;&gt; 0 Then Goto Error_NotApplicable
  212. Set odbDatabase = Application._CurrentDb()
  213. If odbDatabase._DbConnect &lt;&gt; DBCONNECTBASE Then Goto Error_NotApplicable
  214. If _CollType = COLLTABLEDEFS Then Set oColl = odbDatabase.Connection.getTables() Else Set oColl = odbDatabase.Connection.getQueries()
  215. With oColl
  216. vName = _InList(pvName, .getElementNames(), True)
  217. If vName = False Then Goto trace_NotFound
  218. .dropByName(vName)
  219. End With
  220. odbDatabase.Document.store()
  221. Case Else
  222. Goto Error_NotApplicable
  223. End Select
  224. _Count = _Count - 1
  225. Delete = True
  226. Exit_Function:
  227. Utils._ResetCalledSub(cstThisSub)
  228. Exit Function
  229. Error_Function:
  230. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  231. GoTo Exit_Function
  232. Error_NotApplicable:
  233. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  234. Goto Exit_Function
  235. Trace_NotFound:
  236. TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel(Left(_CollType, 5)), pvName))
  237. Goto Exit_Function
  238. End Function &apos; Delete V1.1.0
  239. REM -----------------------------------------------------------------------------------------------------------------------
  240. Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
  241. &apos; Return property value of psProperty property name
  242. Utils._SetCalledSub(&quot;Collection.getProperty&quot;)
  243. If IsMissing(pvProperty) Then Call _TraceArguments()
  244. getProperty = _PropertyGet(pvProperty)
  245. Utils._ResetCalledSub(&quot;Collection.getProperty&quot;)
  246. End Function &apos; getProperty
  247. REM -----------------------------------------------------------------------------------------------------------------------
  248. Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
  249. &apos; Return True if object has a valid property called pvProperty (case-insensitive comparison !)
  250. If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
  251. Exit Function
  252. End Function &apos; hasProperty
  253. REM -----------------------------------------------------------------------------------------------------------------------
  254. Public Function Remove(ByVal Optional pvName As Variant) As Boolean
  255. &apos; Remove a TempVar from the TempVars collection
  256. Const cstThisSub = &quot;Collection.Remove&quot;
  257. Utils._SetCalledSub(cstThisSub)
  258. If _ErrorHandler() Then On Local Error Goto Error_Function
  259. Dim oColl As Object, vName As Variant
  260. Remove = False
  261. If IsMissing(pvName) Then pvName = &quot;&quot;
  262. If Not Utils._CheckArgument(pvName, 1, vbString) Then Goto Exit_Function
  263. If pvName = &quot;&quot; Then Call _TraceArguments()
  264. Select Case _CollType
  265. Case COLLTEMPVARS
  266. If Not _A2B_.hasItem(COLLTEMPVARS, pvName) Then Goto Error_Name
  267. _A2B_.TempVars.Remove(UCase(pvName))
  268. Case Else
  269. Goto Error_NotApplicable
  270. End Select
  271. _Count = _Count - 1
  272. Remove = True
  273. Exit_Function:
  274. Utils._ResetCalledSub(cstThisSub)
  275. Exit Function
  276. Error_Function:
  277. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  278. GoTo Exit_Function
  279. Error_NotApplicable:
  280. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  281. Goto Exit_Function
  282. Error_Name:
  283. TraceError(TRACEFATAL, ERRWRONGARGUMENT, Utils._CalledSub(), False, ,Array(1, pvName))
  284. AddItem = False
  285. Goto Exit_Function
  286. End Function &apos; Remove V1.2.0
  287. REM -----------------------------------------------------------------------------------------------------------------------
  288. Public Function RemoveAll() As Boolean
  289. &apos; Remove the whole TempVars collection
  290. Const cstThisSub = &quot;Collection.Remove&quot;
  291. Utils._SetCalledSub(cstThisSub)
  292. If _ErrorHandler() Then On Local Error Goto Error_Function
  293. Select Case _CollType
  294. Case COLLTEMPVARS
  295. Set _A2B_.TempVars = New Collection
  296. _Count = 0
  297. Case Else
  298. Goto Error_NotApplicable
  299. End Select
  300. Exit_Function:
  301. Utils._ResetCalledSub(cstThisSub)
  302. Exit Function
  303. Error_Function:
  304. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  305. GoTo Exit_Function
  306. Error_NotApplicable:
  307. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  308. Goto Exit_Function
  309. End Function &apos; RemoveAll V1.2.0
  310. REM -----------------------------------------------------------------------------------------------------------------------
  311. REM --- PRIVATE FUNCTIONS ---
  312. REM -----------------------------------------------------------------------------------------------------------------------
  313. Private Function _PropertiesList() As Variant
  314. _PropertiesList = Array(&quot;Count&quot;, &quot;Item&quot;, &quot;ObjectType&quot;)
  315. End Function &apos; _PropertiesList
  316. REM -----------------------------------------------------------------------------------------------------------------------
  317. Private Function _PropertyGet(ByVal psProperty As String) As Variant
  318. &apos; Return property value of the psProperty property name
  319. If _ErrorHandler() Then On Local Error Goto Error_Function
  320. Utils._SetCalledSub(&quot;Collection.get&quot; &amp; psProperty)
  321. _PropertyGet = Nothing
  322. Select Case UCase(psProperty)
  323. Case UCase(&quot;Count&quot;)
  324. _PropertyGet = _Count
  325. Case UCase(&quot;Item&quot;)
  326. Case UCase(&quot;ObjectType&quot;)
  327. _PropertyGet = _Type
  328. Case Else
  329. Goto Trace_Error
  330. End Select
  331. Exit_Function:
  332. Utils._ResetCalledSub(&quot;Collection.get&quot; &amp; psProperty)
  333. Exit Function
  334. Trace_Error:
  335. TraceError(TRACEWARNING, ERRPROPERTY, Utils._CalledSub(), 0, , psProperty)
  336. _PropertyGet = Nothing
  337. Goto Exit_Function
  338. Error_Function:
  339. TraceError(TRACEABORT, Err, &quot;Collection._PropertyGet&quot;, Erl)
  340. _PropertyGet = Nothing
  341. GoTo Exit_Function
  342. End Function &apos; _PropertyGet
  343. </script:module>