Database.xba 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  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="Database" 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 -----------------------------------------------------------------------------------------------------------------------
  12. REM --- CLASS ROOT FIELDS ---
  13. REM -----------------------------------------------------------------------------------------------------------------------
  14. Private _Type As String &apos; Must be DATABASE
  15. Private _This As Object &apos; Workaround for absence of This builtin function
  16. Private _Parent As Object
  17. Private _DbConnect As Integer &apos; DBCONNECTxxx constants
  18. Private Title As String
  19. Private Document As Object &apos; com.sun.star.comp.dba.ODatabaseDocument or SwXTextDocument or ScModelObj
  20. Private Connection As Object &apos; com.sun.star.sdbc.drivers.OConnectionWrapper or com.sun.star.sdbc.XConnection
  21. Private URL As String
  22. Private Location As String &apos; Different from URL for registered databases
  23. Private _ReadOnly As Boolean
  24. Private MetaData As Object &apos; interface XDatabaseMetaData
  25. Private _RDBMS As Integer &apos; DBMS constants
  26. Private _ColumnTypes() As Variant &apos; Part of Metadata.GetTypeInfo()
  27. Private _ColumnTypeNames() As Variant
  28. Private _ColumnPrecisions() As Variant
  29. Private _ColumnTypesReference() As Variant
  30. Private _ColumnTypesAlias() As Variant &apos; To what should a field whose origin is another DBMS be converted ? See DataTypes By RDBMS.ods
  31. Private _BinaryStream As Boolean &apos; False = binary fields must NOT be streamed f.i. via ReadAllBytes or WriteAllBytes
  32. Private Form As Object &apos; com.sun.star.form.XForm
  33. Private FormName As String
  34. Private RecordsetMax As Long &apos; To make unique names in Collection below (See bug # 121342)
  35. Private RecordsetsColl As Object &apos; Collection of active recordsets
  36. REM -----------------------------------------------------------------------------------------------------------------------
  37. REM --- CONSTRUCTORS / DESTRUCTORS ---
  38. REM -----------------------------------------------------------------------------------------------------------------------
  39. Private Sub Class_Initialize()
  40. _Type = OBJDATABASE
  41. Set _This = Nothing
  42. Set _Parent = Nothing
  43. _DbConnect = 0
  44. Title = &quot;&quot;
  45. Set Document = Nothing
  46. Set Connection = Nothing
  47. URL = &quot;&quot;
  48. _ReadOnly = False
  49. Set MetaData = Nothing
  50. _RDBMS = DBMS_UNKNOWN
  51. _ColumnTypes = Array()
  52. _ColumnTypeNames = Array()
  53. _ColumnPrecisions = Array()
  54. _ColumnTypesReference = Array()
  55. _ColumnTypesAlias() = Array()
  56. _BinaryStream = False
  57. Set Form = Nothing
  58. FormName = &quot;&quot;
  59. RecordsetMax = 0
  60. Set RecordsetsColl = New Collection
  61. End Sub &apos; Constructor
  62. REM -----------------------------------------------------------------------------------------------------------------------
  63. Private Sub Class_Terminate()
  64. On Local Error Resume Next
  65. Call CloseAllRecordsets()
  66. If _DbConnect &lt;&gt; DBCONNECTANY Then
  67. If Not IsNull(Connection) Then
  68. Connection.close()
  69. Connection.dispose()
  70. Set Connection = Nothing
  71. End If
  72. Else
  73. mClose()
  74. End If
  75. Call Class_Initialize()
  76. End Sub &apos; Destructor
  77. REM -----------------------------------------------------------------------------------------------------------------------
  78. Public Sub Dispose()
  79. Call Class_Terminate()
  80. End Sub &apos; Explicit destructor
  81. REM -----------------------------------------------------------------------------------------------------------------------
  82. REM --- CLASS GET/LET/SET PROPERTIES ---
  83. REM -----------------------------------------------------------------------------------------------------------------------
  84. Property Get Connect() As String
  85. Connect = _PropertyGet(&quot;Connect&quot;)
  86. End Property &apos; Connect (get)
  87. REM -----------------------------------------------------------------------------------------------------------------------
  88. Property Get Name() As String
  89. Name = _PropertyGet(&quot;Name&quot;)
  90. End Property &apos; Name (get)
  91. REM -----------------------------------------------------------------------------------------------------------------------
  92. Property Get ObjectType() As String
  93. ObjectType = _PropertyGet(&quot;ObjectType&quot;)
  94. End Property &apos; ObjectType (get)
  95. REM -----------------------------------------------------------------------------------------------------------------------
  96. Property Get OnCreate() As String
  97. OnCreate = _PropertyGet(&quot;OnCreate&quot;)
  98. End Property &apos; OnCreate (get)
  99. REM -----------------------------------------------------------------------------------------------------------------------
  100. Property Get OnFocus() As String
  101. OnFocus = _PropertyGet(&quot;OnFocus&quot;)
  102. End Property &apos; OnFocus (get)
  103. REM -----------------------------------------------------------------------------------------------------------------------
  104. Property Get OnLoad() As String
  105. OnLoad = _PropertyGet(&quot;OnLoad&quot;)
  106. End Property &apos; OnLoad (get)
  107. REM -----------------------------------------------------------------------------------------------------------------------
  108. Property Get OnLoadFinished() As String
  109. OnLoadFinished = _PropertyGet(&quot;OnLoadFinished&quot;)
  110. End Property &apos; OnLoadFinished (get)
  111. REM -----------------------------------------------------------------------------------------------------------------------
  112. Property Get OnModifyChanged() As String
  113. OnModifyChanged = _PropertyGet(&quot;OnModifyChanged&quot;)
  114. End Property &apos; OnModifyChanged (get)
  115. REM -----------------------------------------------------------------------------------------------------------------------
  116. Property Get OnNew() As String
  117. OnNew = _PropertyGet(&quot;OnNew&quot;)
  118. End Property &apos; OnNew (get)
  119. REM -----------------------------------------------------------------------------------------------------------------------
  120. Property Get OnPrepareUnload() As String
  121. OnPrepareUnload = _PropertyGet(&quot;OnPrepareUnload&quot;)
  122. End Property &apos; OnPrepareUnload (get)
  123. REM -----------------------------------------------------------------------------------------------------------------------
  124. Property Get OnPrepareViewClosing() As String
  125. OnPrepareViewClosing = _PropertyGet(&quot;OnPrepareViewClosing&quot;)
  126. End Property &apos; OnPrepareViewClosing (get)
  127. REM -----------------------------------------------------------------------------------------------------------------------
  128. Property Get OnSave() As String
  129. OnSave = _PropertyGet(&quot;OnSave&quot;)
  130. End Property &apos; OnSave (get)
  131. REM -----------------------------------------------------------------------------------------------------------------------
  132. Property Get OnSaveAs() As String
  133. OnSaveAs = _PropertyGet(&quot;OnSaveAs&quot;)
  134. End Property &apos; OnSaveAs (get)
  135. REM -----------------------------------------------------------------------------------------------------------------------
  136. Property Get OnSaveAsDone() As String
  137. OnSaveAsDone = _PropertyGet(&quot;OnSaveAsDone&quot;)
  138. End Property &apos; OnSaveAsDone (get)
  139. REM -----------------------------------------------------------------------------------------------------------------------
  140. Property Get OnSaveAsFailed() As String
  141. OnSaveAsFailed = _PropertyGet(&quot;OnSaveAsFailed&quot;)
  142. End Property &apos; OnSaveAsFailed (get)
  143. REM -----------------------------------------------------------------------------------------------------------------------
  144. Property Get OnSaveDone() As String
  145. OnSaveDone = _PropertyGet(&quot;OnSaveDone&quot;)
  146. End Property &apos; OnSaveDone (get)
  147. REM -----------------------------------------------------------------------------------------------------------------------
  148. Property Get OnSaveFailed() As String
  149. OnSaveFailed = _PropertyGet(&quot;OnSaveFailed&quot;)
  150. End Property &apos; OnSaveFailed (get)
  151. REM -----------------------------------------------------------------------------------------------------------------------
  152. Property Get OnSubComponentClosed() As String
  153. OnSubComponentClosed = _PropertyGet(&quot;OnSubComponentClosed&quot;)
  154. End Property &apos; OnSubComponentClosed (get)
  155. REM -----------------------------------------------------------------------------------------------------------------------
  156. Property Get OnSubComponentOpened() As String
  157. OnSubComponentOpened = _PropertyGet(&quot;OnSubComponentOpened&quot;)
  158. End Property &apos; OnSubComponentOpened (get)
  159. REM -----------------------------------------------------------------------------------------------------------------------
  160. Property Get OnTitleChanged() As String
  161. OnTitleChanged = _PropertyGet(&quot;OnTitleChanged&quot;)
  162. End Property &apos; OnTitleChanged (get)
  163. REM -----------------------------------------------------------------------------------------------------------------------
  164. Property Get OnUnfocus() As String
  165. OnUnfocus = _PropertyGet(&quot;OnUnfocus&quot;)
  166. End Property &apos; OnUnfocus (get)
  167. REM -----------------------------------------------------------------------------------------------------------------------
  168. Property Get OnUnload() As String
  169. OnUnload = _PropertyGet(&quot;OnUnload&quot;)
  170. End Property &apos; OnUnload (get)
  171. REM -----------------------------------------------------------------------------------------------------------------------
  172. Property Get OnViewClosed() As String
  173. OnViewClosed = _PropertyGet(&quot;OnViewClosed&quot;)
  174. End Property &apos; OnViewClosed (get)
  175. REM -----------------------------------------------------------------------------------------------------------------------
  176. Property Get OnViewCreated() As String
  177. OnViewCreated = _PropertyGet(&quot;OnViewCreated&quot;)
  178. End Property &apos; OnViewCreated (get)
  179. REM -----------------------------------------------------------------------------------------------------------------------
  180. Property Get Version() As String
  181. Version = _PropertyGet(&quot;Version&quot;)
  182. End Property &apos; Version (get)
  183. REM -----------------------------------------------------------------------------------------------------------------------
  184. REM --- CLASS METHODS ---
  185. REM -----------------------------------------------------------------------------------------------------------------------
  186. REM -----------------------------------------------------------------------------------------------------------------------
  187. Public Function mClose() As Variant
  188. &apos; Close the database
  189. If _ErrorHandler() Then On Local Error Goto Error_Function
  190. Const cstThisSub = &quot;Database.Close&quot;
  191. Utils._SetCalledSub(cstThisSub)
  192. mClose = False
  193. If _DbConnect &lt;&gt; DBCONNECTANY Then Goto Error_NotApplicable
  194. With Connection
  195. If Utils._hasUNOMethod(Connection, &quot;flush&quot;) Then .flush
  196. .close()
  197. .dispose()
  198. End With
  199. Set Connection = Nothing
  200. mClose = True
  201. Exit_Function:
  202. Utils._ResetCalledSub(cstThisSub)
  203. Exit Function
  204. Error_NotApplicable:
  205. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  206. Goto Exit_Function
  207. Error_Function:
  208. TraceError(TRACEABORT, Err, Utils._CalledSub(), Erl)
  209. GoTo Exit_Function
  210. End Function &apos; (m)Close
  211. REM -----------------------------------------------------------------------------------------------------------------------
  212. Public Sub CloseAllRecordsets()
  213. &apos; Clean all recordsets for housekeeping
  214. Dim sRecordsets() As String, i As Integer, oRecordset As Object
  215. On Local Error Goto Exit_Sub
  216. If IsNull(RecordsetsColl) Then Exit Sub
  217. If RecordsetsColl.Count &lt; 1 Then Exit Sub
  218. For i = 1 To RecordsetsColl.Count
  219. Set oRecordset = RecordsetsColl.Item(i)
  220. oRecordset.mClose(False) &apos; Do not remove entry in collection
  221. Next i
  222. Set RecordsetsColl = New Collection
  223. RecordsetMax = 0
  224. Exit_Sub:
  225. Exit Sub
  226. End Sub &apos; CloseAllRecordsets V0.9.5
  227. REM -----------------------------------------------------------------------------------------------------------------------
  228. Public Function CreateQueryDef(ByVal Optional pvQueryName As Variant _
  229. , ByVal Optional pvSql As Variant _
  230. , ByVal Optional pvOption As Variant _
  231. ) As Object
  232. &apos;Return a (new) QueryDef object based on SQL statement
  233. Const cstThisSub = &quot;Database.CreateQueryDef&quot;
  234. Utils._SetCalledSub(cstThisSub)
  235. Const cstNull = -1
  236. Dim oQuery As Object, oQueries As Object, i As Integer, sQueryName As String
  237. If _ErrorHandler() Then On Local Error Goto Error_Function
  238. Set CreateQueryDef = Nothing
  239. If _DbConnect &lt;&gt; DBCONNECTBASE Then Goto Error_NotApplicable
  240. If IsMissing(pvQueryName) Then Call _TraceArguments()
  241. If IsMissing(pvSql) Then Call _TraceArguments()
  242. If IsMissing(pvOption) Then pvOption = cstNull
  243. If Not Utils._CheckArgument(pvQueryName, 1, vbString) Then Goto Exit_Function
  244. If pvQueryName = &quot;&quot; Then Call _TraceArguments()
  245. If Not Utils._CheckArgument(pvSql, 2, vbString) Then Goto Exit_Function
  246. If pvSql = &quot;&quot; Then Call _TraceArguments()
  247. If Not Utils._CheckArgument(pvOption, 3, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
  248. If _ReadOnly Then Goto Error_NoUpdate
  249. Set oQuery = CreateUnoService(&quot;com.sun.star.sdb.QueryDefinition&quot;)
  250. oQuery.rename(pvQueryName)
  251. oQuery.Command = _ReplaceSquareBrackets(pvSql)
  252. oQuery.EscapeProcessing = Not ( pvOption = dbSQLPassThrough )
  253. Set oQueries = Document.DataSource.getQueryDefinitions()
  254. With oQueries
  255. For i = 0 To .getCount() - 1
  256. sQueryName = .getByIndex(i).Name
  257. If UCase(sQueryName) = UCase(pvQueryName) Then
  258. TraceError(TRACEWARNING, ERRQUERYDEFDELETED, Utils._CalledSub(), 0, False, sQueryName)
  259. .removeByName(sQueryName)
  260. Exit For
  261. End If
  262. Next i
  263. .insertByName(pvQueryName, oQuery)
  264. End With
  265. Set CreateQueryDef = QueryDefs(pvQueryName)
  266. Exit_Function:
  267. Utils._ResetCalledSub(cstThisSub)
  268. Exit Function
  269. Error_NotApplicable:
  270. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  271. Goto Exit_Function
  272. Error_NoUpdate:
  273. TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(), 0)
  274. Goto Exit_Function
  275. Error_Function:
  276. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  277. GoTo Exit_Function
  278. End Function &apos; CreateQueryDef V1.1.0
  279. REM -----------------------------------------------------------------------------------------------------------------------
  280. Public Function CreateTableDef(ByVal Optional pvTableName As Variant) As Object
  281. &apos;Return a (new/empty) TableDef object
  282. Const cstThisSub = &quot;Database.CreateTableDef&quot;
  283. Utils._SetCalledSub(cstThisSub)
  284. Dim oTable As Object, oTables As Object, sTables() As String
  285. Dim i As Integer, sTableName As String, oNewTable As Object
  286. Dim vNameComponents() As Variant, iNames As Integer
  287. If _ErrorHandler() Then On Local Error Goto Error_Function
  288. Set CreateTableDef = Nothing
  289. If _DbConnect &lt;&gt; DBCONNECTBASE Then Goto Error_NotApplicable
  290. If IsMissing(pvTableName) Then Call _TraceArguments()
  291. If Not Utils._CheckArgument(pvTableName, 1, vbString) Then Goto Exit_Function
  292. If pvTableName = &quot;&quot; Then Call _TraceArguments()
  293. If _ReadOnly Then Goto Error_NoUpdate
  294. Set oTables = Connection.getTables
  295. With oTables
  296. sTables = .ElementNames()
  297. &apos; Check existence of object and find its exact (case-sensitive) name
  298. For i = 0 To UBound(sTables)
  299. If UCase(pvTableName) = UCase(sTables(i)) Then
  300. sTableName = sTables(i)
  301. TraceError(TRACEWARNING, ERRTABLEDEFDELETED, Utils._CalledSub(), 0, False, sTableName)
  302. .dropByName(sTableName)
  303. Exit For
  304. End If
  305. Next i
  306. Set oNewTable = New DataDef
  307. Set oNewTable._This = oNewTable
  308. oNewTable._Type = OBJTABLEDEF
  309. oNewTable._Name = pvTableName
  310. vNameComponents = Split(pvTableName, &quot;.&quot;)
  311. iNames = UBound(vNameComponents)
  312. If iNames &gt;= 2 Then oNewtable.CatalogName = vNameComponents(iNames - 2) Else oNewTable.CatalogName = &quot;&quot;
  313. If iNames &gt;= 1 Then oNewtable.SchemaName = vNameComponents(iNames - 1) Else oNewTable.SchemaName = &quot;&quot;
  314. oNewtable.TableName = vNameComponents(iNames)
  315. Set oNewTable._ParentDatabase = _This
  316. Set oNewTable.TableDescriptor = .createDataDescriptor()
  317. oNewTable.TableDescriptor.CatalogName = oNewTable.CatalogName
  318. oNewTable.TableDescriptor.SchemaName = oNewTable.SchemaName
  319. oNewTable.TableDescriptor.Name = oNewTable.TableName
  320. oNewTable.TableDescriptor.Type = &quot;TABLE&quot;
  321. End With
  322. Set CreateTabledef = oNewTable
  323. Exit_Function:
  324. Utils._ResetCalledSub(cstThisSub)
  325. Exit Function
  326. Error_NotApplicable:
  327. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  328. Goto Exit_Function
  329. Error_NoUpdate:
  330. TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(), 0)
  331. Goto Exit_Function
  332. Error_Function:
  333. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  334. GoTo Exit_Function
  335. End Function &apos; CreateTableDef V1.1.0
  336. REM -----------------------------------------------------------------------------------------------------------------------
  337. Public Function DAvg( _
  338. ByVal Optional psExpr As String _
  339. , ByVal Optional psDomain As String _
  340. , ByVal Optional pvCriteria As Variant _
  341. ) As Variant
  342. &apos; Return average of scope
  343. Const cstThisSub = &quot;Database.DAvg&quot;
  344. Utils._SetCalledSub(cstThisSub)
  345. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  346. DAvg = _DFunction(&quot;AVG&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;)
  347. Utils._ResetCalledSub(cstThisSub)
  348. End Function &apos; DAvg
  349. REM -----------------------------------------------------------------------------------------------------------------------
  350. Public Function DCount( _
  351. ByVal Optional psExpr As String _
  352. , ByVal Optional psDomain As String _
  353. , ByVal Optional pvCriteria As Variant _
  354. ) As Variant
  355. &apos; Return # of occurrences of scope
  356. Const cstThisSub = &quot;Database.DCount&quot;
  357. Utils._SetCalledSub(cstThisSub)
  358. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  359. DCount = _DFunction(&quot;COUNT&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;)
  360. Utils._ResetCalledSub(cstThisSub)
  361. End Function &apos; DCount
  362. REM -----------------------------------------------------------------------------------------------------------------------
  363. Public Function DLookup( _
  364. ByVal Optional psExpr As String _
  365. , ByVal Optional psDomain As String _
  366. , ByVal Optional pvCriteria As Variant _
  367. , ByVal Optional pvOrderClause As Variant _
  368. ) As Variant
  369. &apos; Return a value within a table
  370. &apos;Arguments: psExpr: an SQL expression
  371. &apos; psDomain: a table- or queryname
  372. &apos; pvCriteria: an optional WHERE clause
  373. &apos; pcOrderClause: an optional order clause incl. &quot;DESC&quot; if relevant
  374. &apos;Return: Value of the psExpr if found, else Null.
  375. &apos;Author: inspired from Allen Browne. http://allenbrowne.com/ser-42.html
  376. &apos;Examples:
  377. &apos; 1. To find the last value, include DESC in the OrderClause, e.g.:
  378. &apos; DLookup(&quot;[Surname] &amp; [FirstName]&quot;, &quot;tblClient&quot;, , &quot;ClientID DESC&quot;)
  379. &apos; 2. To find the lowest non-null value of a field, use the Criteria, e.g.:
  380. &apos; DLookup(&quot;ClientID&quot;, &quot;tblClient&quot;, &quot;Surname Is Not Null&quot; , &quot;Surname&quot;)
  381. Const cstThisSub = &quot;Database.DLookup&quot;
  382. Utils._SetCalledSub(cstThisSub)
  383. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  384. DLookup = _DFunction(&quot;&quot;, psExpr, psDomain _
  385. , Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria) _
  386. , Iif(IsMissing(pvOrderClause), &quot;&quot;, pvOrderClause) _
  387. )
  388. Utils._ResetCalledSub(cstThisSub)
  389. End Function &apos; DLookup
  390. REM -----------------------------------------------------------------------------------------------------------------------
  391. Public Function DMax( _
  392. ByVal Optional psExpr As String _
  393. , ByVal Optional psDomain As String _
  394. , ByVal Optional pvCriteria As Variant _
  395. ) As Variant
  396. &apos; Return maximum of scope
  397. Const cstThisSub = &quot;Database.DMax&quot;
  398. Utils._SetCalledSub(cstThisSub)
  399. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  400. DMax = _DFunction(&quot;MAX&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;)
  401. Utils._ResetCalledSub(cstThisSub)
  402. End Function &apos; DMax
  403. REM -----------------------------------------------------------------------------------------------------------------------
  404. Public Function DMin( _
  405. ByVal Optional psExpr As String _
  406. , ByVal Optional psDomain As String _
  407. , ByVal Optional pvCriteria As Variant _
  408. ) As Variant
  409. &apos; Return minimum of scope
  410. Const cstThisSub = &quot;Database.DMin&quot;
  411. Utils._SetCalledSub(cstThisSub)
  412. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  413. DMin = _DFunction(&quot;MIN&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;)
  414. Utils._ResetCalledSub(cstThisSub)
  415. End Function &apos; DMin
  416. REM -----------------------------------------------------------------------------------------------------------------------
  417. Public Function DStDev( _
  418. ByVal Optional psExpr As String _
  419. , ByVal Optional psDomain As String _
  420. , ByVal Optional pvCriteria As Variant _
  421. ) As Variant
  422. &apos; Return standard deviation of scope
  423. Const cstThisSub = &quot;Database.DStDev&quot;
  424. Utils._SetCalledSub(cstThisSub)
  425. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  426. DStDev = _DFunction(&quot;STDDEV_SAMP&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;) &apos; STDDEV not STDEV !
  427. Utils._ResetCalledSub(cstThisSub)
  428. End Function &apos; DStDev
  429. REM -----------------------------------------------------------------------------------------------------------------------
  430. Public Function DStDevP( _
  431. ByVal Optional psExpr As String _
  432. , ByVal Optional psDomain As String _
  433. , ByVal Optional pvCriteria As Variant _
  434. ) As Variant
  435. &apos; Return standard deviation of scope
  436. Const cstThisSub = &quot;Database.DStDevP&quot;
  437. Utils._SetCalledSub(cstThisSub)
  438. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  439. DStDevP = _DFunction(&quot;STDDEV_POP&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;) &apos; STDDEV not STDEV !
  440. Utils._ResetCalledSub(cstThisSub)
  441. End Function &apos; DStDevP
  442. REM -----------------------------------------------------------------------------------------------------------------------
  443. Public Function DSum( _
  444. ByVal Optional psExpr As String _
  445. , ByVal Optional psDomain As String _
  446. , ByVal Optional pvCriteria As Variant _
  447. ) As Variant
  448. &apos; Return sum of scope
  449. Const cstThisSub = &quot;Database.DSum&quot;
  450. Utils._SetCalledSub(cstThisSub)
  451. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  452. DSum = _DFunction(&quot;SUM&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;)
  453. Utils._ResetCalledSub(cstThisSub)
  454. End Function &apos; DSum
  455. REM -----------------------------------------------------------------------------------------------------------------------
  456. Public Function DVar( _
  457. ByVal Optional psExpr As String _
  458. , ByVal Optional psDomain As String _
  459. , ByVal Optional pvCriteria As Variant _
  460. ) As Variant
  461. &apos; Return variance of scope
  462. Const cstThisSub = &quot;Database.DVar&quot;
  463. Utils._SetCalledSub(cstThisSub)
  464. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  465. DVar = _DFunction(&quot;VAR_SAMP&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;)
  466. Utils._ResetCalledSub(cstThisSub)
  467. End Function &apos; DVar
  468. REM -----------------------------------------------------------------------------------------------------------------------
  469. Public Function DVarP( _
  470. ByVal Optional psExpr As String _
  471. , ByVal Optional psDomain As String _
  472. , ByVal Optional pvCriteria As Variant _
  473. ) As Variant
  474. &apos; Return variance of scope
  475. Const cstThisSub = &quot;Database.DVarP&quot;
  476. Utils._SetCalledSub(cstThisSub)
  477. If IsMissing(psExpr) Or IsMissing(psDomain) Then Call _TraceArguments()
  478. DVarP = _DFunction(&quot;VAR_POP&quot;, psExpr, psDomain, Iif(IsMissing(pvCriteria), &quot;&quot;, pvCriteria), &quot;&quot;)
  479. Utils._ResetCalledSub(cstThisSub)
  480. End Function &apos; DVarP
  481. REM -----------------------------------------------------------------------------------------------------------------------
  482. Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
  483. &apos; Return property value of psProperty property name
  484. Utils._SetCalledSub(&quot;Database.getProperty&quot;)
  485. If IsMissing(pvProperty) Then Call _TraceArguments()
  486. getProperty = _PropertyGet(pvProperty)
  487. Utils._ResetCalledSub(&quot;Database.getProperty&quot;)
  488. End Function &apos; getProperty
  489. REM -----------------------------------------------------------------------------------------------------------------------
  490. Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
  491. &apos; Return True if object has a valid property called pvProperty (case-insensitive comparison !)
  492. If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
  493. Exit Function
  494. End Function &apos; hasProperty
  495. REM -----------------------------------------------------------------------------------------------------------------------
  496. Public Function OpenRecordset(ByVal Optional pvSource As Variant _
  497. , ByVal Optional pvType As Variant _
  498. , ByVal Optional pvOptions As Variant _
  499. , ByVal Optional pvLockEdit As Variant _
  500. ) As Object
  501. &apos;Return a Recordset object based on Source (= SQL, table or query name)
  502. Const cstThisSub = &quot;Database.OpenRecordset&quot;
  503. Utils._SetCalledSub(cstThisSub)
  504. Const cstNull = -1
  505. Dim lCommandType As Long, sCommand As String, oObject As Object
  506. Dim sSource As String, i As Integer, iCount As Integer
  507. Dim sObjects() As String, bFound As Boolean, oTables As Object, oQueries As Object
  508. Dim iType As Integer, iOptions As Integer, iLockEdit As Integer
  509. If _ErrorHandler() Then On Local Error Goto Error_Function
  510. Set oObject = Nothing
  511. If IsMissing(pvSource) Then Call _TraceArguments()
  512. If pvSource = &quot;&quot; Then Call _TraceArguments()
  513. If VarType(pvType) = vbError Then
  514. iType = cstNull
  515. ElseIf IsMissing(pvType) Then
  516. iType = cstNull
  517. Else
  518. If Not Utils._CheckArgument(pvType, 2, Utils._AddNumeric(), Array(cstNull, dbOpenForwardOnly)) Then Goto Exit_Function
  519. iType = pvType
  520. End If
  521. If VarType(pvOptions) = vbError Then
  522. iOptions = cstNull
  523. ElseIf IsMissing(pvOptions) Then
  524. iOptions = cstNull
  525. Else
  526. If Not Utils._CheckArgument(pvOptions, 3, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
  527. iOptions = pvOptions
  528. End If
  529. If VarType(pvLockEdit) = vbError Then
  530. iLockEdit = cstNull
  531. ElseIf IsMissing(pvLockEdit) Then
  532. iLockEdit = cstNull
  533. Else
  534. If Not Utils._CheckArgument(pvLockEdit, 4, Utils._AddNumeric(), Array(cstNull, dbReadOnly)) Then Goto Exit_Function
  535. iLockEdit = pvLockEdit
  536. End If
  537. sSource = Split(UCase(Trim(pvSource)), &quot; &quot;)(0)
  538. Select Case True
  539. Case sSource = &quot;SELECT&quot;
  540. lCommandType = com.sun.star.sdb.CommandType.COMMAND
  541. sCommand = _ReplaceSquareBrackets(pvSource)
  542. Case Else
  543. sSource = UCase(Trim(pvSource))
  544. REM Explore tables
  545. Set oTables = Connection.getTables
  546. sObjects = oTables.ElementNames()
  547. bFound = False
  548. For i = 0 To UBound(sObjects)
  549. If sSource = UCase(sObjects(i)) Then
  550. sCommand = sObjects(i)
  551. bFound = True
  552. Exit For
  553. End If
  554. Next i
  555. If bFound Then
  556. lCommandType = com.sun.star.sdb.CommandType.TABLE
  557. Else
  558. REM Explore queries
  559. Set oQueries = Connection.getQueries
  560. sObjects = oQueries.ElementNames()
  561. For i = 0 To UBound(sObjects)
  562. If sSource = UCase(sObjects(i)) Then
  563. sCommand = sObjects(i)
  564. bFound = True
  565. Exit For
  566. End If
  567. Next i
  568. If Not bFound Then Goto Trace_NotFound
  569. lCommandType = com.sun.star.sdb.CommandType.QUERY
  570. End If
  571. End Select
  572. Set oObject = New Recordset
  573. With oObject
  574. ._CommandType = lCommandType
  575. ._Command = sCommand
  576. ._ParentName = Title
  577. ._ParentType = _Type
  578. ._ForwardOnly = ( iType = dbOpenForwardOnly )
  579. ._PassThrough = ( iOptions = dbSQLPassThrough )
  580. ._ReadOnly = ( (iLockEdit = dbReadOnly) Or _ReadOnly )
  581. Set ._This = oObject
  582. Set ._ParentDatabase = _This
  583. Call ._Initialize()
  584. RecordsetMax = RecordsetMax + 1
  585. ._Name = Format(RecordsetMax, &quot;0000000&quot;)
  586. RecordsetsColl.Add(oObject, UCase(._Name))
  587. End With
  588. If Not ( oObject._BOF And oObject._EOF ) Then oObject.MoveFirst() &apos; Do nothing if resultset empty
  589. Exit_Function:
  590. Set OpenRecordset = oObject
  591. Set oObject = Nothing
  592. Utils._ResetCalledSub(cstThisSub)
  593. Exit Function
  594. Error_Function:
  595. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  596. GoTo Exit_Function
  597. Trace_NotFound:
  598. TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel(&quot;TABLE&quot;) &amp; &quot;/&quot; &amp; _GetLabel(&quot;QUERY&quot;), pvSource))
  599. Goto Exit_Function
  600. End Function &apos; OpenRecordset V1.1.0
  601. REM -----------------------------------------------------------------------------------------------------------------------
  602. Public Function OpenSQL(Optional ByVal pvSQL As Variant _
  603. , Optional ByVal pvOption As Variant _
  604. ) As Boolean
  605. &apos; Return True if the execution of the SQL statement was successful
  606. &apos; SQL must contain a SELECT query
  607. &apos; pvOption can force pass through mode
  608. If _ErrorHandler() Then On Local Error Goto Error_Function
  609. Const cstThisSub = &quot;Database.OpenSQL&quot;
  610. Utils._SetCalledSub(cstThisSub)
  611. OpenSQL = False
  612. If IsMissing(pvSQL) Then Call _TraceArguments()
  613. If Not Utils._CheckArgument(pvSQL, 1, vbString) Then Goto Exit_Function
  614. Const cstNull = -1
  615. If IsMissing(pvOption) Then
  616. pvOption = cstNull
  617. Else
  618. If Not Utils._CheckArgument(pvOption, 2, Utils._AddNumeric(), Array(dbSQLPassThrough, cstNull)) Then Goto Exit_Function
  619. End If
  620. If _DbConnect &lt;&gt; DBCONNECTBASE And _DbConnect &lt;&gt; DBCONNECTFORM Then Goto Error_NotApplicable
  621. Dim oURL As New com.sun.star.util.URL, oDispatch As Object
  622. Dim vArgs(8) as New com.sun.star.beans.PropertyValue
  623. oURL.Complete = &quot;.component:DB/DataSourceBrowser&quot;
  624. oDispatch = StarDesktop.queryDispatch(oURL, &quot;_Blank&quot;, 8)
  625. vArgs(0).Name = &quot;ActiveConnection&quot; : vArgs(0).Value = Connection
  626. vArgs(1).Name = &quot;CommandType&quot; : vArgs(1).Value = com.sun.star.sdb.CommandType.COMMAND
  627. vArgs(2).Name = &quot;Command&quot; : vArgs(2).Value = _ReplaceSquareBrackets(pvSQL)
  628. vArgs(3).Name = &quot;ShowMenu&quot; : vArgs(3).Value = True
  629. vArgs(4).Name = &quot;ShowTreeView&quot; : vArgs(4).Value = False
  630. vArgs(5).Name = &quot;ShowTreeViewButton&quot; : vArgs(5).Value = False
  631. vArgs(6).Name = &quot;Filter&quot; : vArgs(6).Value = &quot;&quot;
  632. vArgs(7).Name = &quot;ApplyFilter&quot; : vArgs(7).Value = False
  633. vArgs(8).Name = &quot;EscapeProcessing&quot; : vArgs(8).Value = CBool(Not ( pvOption = dbSQLPassThrough ))
  634. oDispatch.dispatch(oURL, vArgs)
  635. OpenSQL = True
  636. Exit_Function:
  637. Exit Function
  638. Error_Function:
  639. TraceError(TRACEABORT, Err, &quot;OpenSQL&quot;, Erl)
  640. GoTo Exit_Function
  641. SQL_Error:
  642. TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(), 0, , pvSQL)
  643. Goto Exit_Function
  644. Error_NotApplicable:
  645. TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(), 0, 1, cstThisSub)
  646. Goto Exit_Function
  647. End Function &apos; OpenSQL V1.1.0
  648. REM -----------------------------------------------------------------------------------------------------------------------
  649. Public Function OutputTo(ByVal pvObjectType As Variant _
  650. , ByVal Optional pvObjectName As Variant _
  651. , ByVal Optional pvOutputFormat As Variant _
  652. , ByVal Optional pvOutputFile As Variant _
  653. , ByVal Optional pvAutoStart As Variant _
  654. , ByVal Optional pvTemplateFile As Variant _
  655. , ByVal Optional pvEncoding As Variant _
  656. , ByVal Optional pvQuality As Variant _
  657. , ByRef Optional pvHeaders As Variant _
  658. , ByRef Optional pvData As Variant _
  659. ) As Boolean
  660. &apos;Supported: acFormatHTML, acFormatODS, acFormatXLS, acFormatXLSX, acFormatTXT for tables and queries
  661. &apos;pvHeaders and pvData (unpublished) when pvObjectType = acOutputArray
  662. If _ErrorHandler() Then On Local Error Goto Error_Function
  663. Const cstThisSub = &quot;Database.OutputTo&quot;
  664. Utils._SetCalledSub(cstThisSub)
  665. OutputTo = False
  666. If Not Utils._CheckArgument(pvObjectType, 1, Utils._AddNumeric(), Array(acOutputTable, acOutputQuery, acOutputArray)) Then Goto Exit_Function
  667. If IsMissing(pvObjectName) Then Call _TraceArguments()
  668. If Not Utils._CheckArgument(pvObjectName, 2, vbString) Then Goto Exit_Function
  669. If IsMissing(pvOutputFormat) Then pvOutputFormat = &quot;&quot;
  670. If Not Utils._CheckArgument(pvOutputFormat, 3, vbString) Then Goto Exit_Function
  671. If pvOutputFormat &lt;&gt; &quot;&quot; Then
  672. If Not Utils._CheckArgument(UCase(pvOutputFormat), 3, vbString, Array( _
  673. UCase(acFormatHTML), &quot;HTML&quot; _
  674. , UCase(acFormatODS), &quot;ODS&quot; _
  675. , UCase(acFormatXLS), &quot;XLS&quot; _
  676. , UCase(acFormatXLSX), &quot;XLSX&quot; _
  677. , UCase(acFormatTXT), &quot;TXT&quot;, &quot;CSV&quot; _
  678. , &quot;&quot;)) _
  679. Then Goto Exit_Function &apos; A 2nd time to allow case unsensitivity
  680. End If
  681. If IsMissing(pvOutputFile) Then pvOutputFile = &quot;&quot;
  682. If Not Utils._CheckArgument(pvOutputFile, 4, vbString) Then Goto Exit_Function
  683. If IsMissing(pvAutoStart) Then pvAutoStart = False
  684. If Not Utils._CheckArgument(pvAutoStart, 5, vbBoolean) Then Goto Exit_Function
  685. If IsMissing(pvTemplateFile) Then pvTemplateFile = &quot;&quot;
  686. If Not Utils._CheckArgument(pvTemplateFile, 6, vbString) Then Goto Exit_Function
  687. If IsMissing(pvEncoding) Then pvEncoding = 0
  688. If Not Utils._CheckArgument(pvEncoding, 7, _AddNumeric()) Then Goto Exit_Function
  689. If IsMissing(pvQuality) Then pvQuality = acExportQualityPrint
  690. If Not Utils._CheckArgument(pvQuality, 7, _AddNumeric(), Array(acExportQualityPrint, acExportQualityScreen)) Then Goto Exit_Function
  691. If pvObjectType = acOutputArray Then
  692. If IsMissing(pvHeaders) Or IsMissing(pvData) Then Call _TraceArguments()
  693. pvOutputFormat = &quot;HTML&quot;
  694. End If
  695. Dim sOutputFile As String, oTable As Object
  696. Dim sOutputFormat As String, iTemplate As Integer, iOutputFile As Integer, bOutput As Boolean, sSuffix As String
  697. If pvObjectType = acOutputArray Then
  698. Set oTable = Nothing
  699. Else
  700. &apos;Find applicable table or query
  701. If pvObjectType = acOutputTable Then Set oTable = TableDefs(pvObjectName, True) Else Set oTable = Querydefs(pvObjectName, True)
  702. If IsNull(oTable) Then Goto Error_NotFound
  703. End If
  704. &apos;Determine format and parameters
  705. If pvOutputFormat = &quot;&quot; Then
  706. sOutputFormat = _PromptFormat(Array(&quot;HTML&quot;, &quot;ODS&quot;, &quot;XLS&quot;, &quot;XLSX&quot;, &quot;TXT&quot;)) &apos; Prompt user for format
  707. If sOutputFormat = &quot;&quot; Then Goto Exit_Function
  708. Else
  709. sOutputFormat = UCase(pvOutputFormat)
  710. End If
  711. &apos;Determine output file
  712. If pvOutputFile = &quot;&quot; Then &apos; Prompt file picker to user
  713. Select Case sOutputFormat
  714. Case UCase(acFormatHTML), &quot;HTML&quot; : sSuffix = &quot;html&quot;
  715. Case UCase(acFormatODS), &quot;ODS&quot; : sSuffix = &quot;ods&quot;
  716. Case UCase(acFormatXLS), &quot;XLS&quot; : sSuffix = &quot;xls&quot;
  717. Case UCase(acFormatXLSX), &quot;XLSX&quot; : sSuffix = &quot;xlsx&quot;
  718. Case UCase(acFormatTXT), &quot;TXT&quot;, &quot;CSV&quot; : sSuffix = &quot;txt&quot;
  719. End Select
  720. sOutputFile = _PromptFilePicker(sSuffix)
  721. If sOutputFile = &quot;&quot; Then Goto Exit_Function
  722. Else
  723. sOutputFile = pvOutputFile
  724. End If
  725. sOutputFile = ConvertToURL(sOutputFile)
  726. &apos;Create file
  727. Select Case sOutputFormat
  728. Case UCase(acFormatHTML), &quot;HTML&quot;
  729. If pvObjectType = acOutputArray Then
  730. bOutput = _OutputToHTML(Nothing, pvObjectName, sOutputFile, pvTemplateFile, pvHeaders, pvData)
  731. Else
  732. bOutput = _OutputToHTML(oTable, pvObjectName, sOutputFile, pvTemplateFile)
  733. End If
  734. Case UCase(acFormatODS), &quot;ODS&quot;
  735. bOutput = _OutputToCalc(oTable, sOutputFile, acFormatODS)
  736. Case UCase(acFormatXLS), &quot;XLS&quot;
  737. bOutput = _OutputToCalc(oTable, sOutputFile, acFormatXLS)
  738. Case UCase(acFormatXLS), &quot;XLSX&quot;
  739. bOutput = _OutputToCalc(oTable, sOutputFile, acFormatXLSX)
  740. Case UCase(acFormatTXT), &quot;TXT&quot;, &quot;CSV&quot;
  741. bOutput = _OutputToCalc(oTable, sOutputFile, acFormatTXT, pvEncoding)
  742. End Select
  743. &apos;Launch application, if requested
  744. If bOutput Then
  745. If pvAutoStart Then Call _ShellExecute(sOutputFile)
  746. Else
  747. GoTo Error_File
  748. End If
  749. OutputTo = True
  750. Exit_Function:
  751. If Not IsNull(oTable) Then
  752. oTable.Dispose()
  753. Set oTable = Nothing
  754. End If
  755. Utils._ResetCalledSub(cstThisSub)
  756. Exit Function
  757. Error_NotFound:
  758. TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel(&quot;OBJECT&quot;), pvObjectName))
  759. Goto Exit_Function
  760. Error_Function:
  761. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  762. GoTo Exit_Function
  763. Error_File:
  764. TraceError(TRACEFATAL, ERRFILENOTCREATED, Utils._CalledSub(), 0, , sOutputFile)
  765. GoTo Exit_Function
  766. End Function &apos; OutputTo V1.4.0
  767. REM -----------------------------------------------------------------------------------------------------------------------
  768. Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
  769. &apos; Return
  770. &apos; a Collection object if pvIndex absent
  771. &apos; a Property object otherwise
  772. Utils._SetCalledSub(&quot;Database.Properties&quot;)
  773. Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
  774. vPropertiesList = _PropertiesList()
  775. sObject = Utils._PCase(_Type)
  776. If IsMissing(pvIndex) Then
  777. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList)
  778. Else
  779. vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList, pvIndex)
  780. vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
  781. End If
  782. Set vProperty._ParentDatabase = _This
  783. Exit_Function:
  784. Set Properties = vProperty
  785. Utils._ResetCalledSub(&quot;Database.Properties&quot;)
  786. Exit Function
  787. End Function &apos; Properties
  788. REM -----------------------------------------------------------------------------------------------------------------------
  789. Public Function QueryDefs(ByVal Optional pvIndex As Variant, ByVal Optional pbCheck As Boolean) As Object
  790. &apos; Collect all Queries in the database
  791. &apos; pbCheck unpublished
  792. If _ErrorHandler() Then On Local Error Goto Error_Function
  793. Utils._SetCalledSub(&quot;Database.QueryDefs&quot;)
  794. If IsMissing(pbCheck) Then pbCheck = False
  795. Dim sObjects() As String, sObjectName As String, oObject As Object
  796. Dim i As Integer, bFound As Boolean, oQueries As Object
  797. Set oObject = Nothing
  798. If Not IsMissing(pvIndex) Then
  799. If Not Utils._CheckArgument(pvIndex, 1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
  800. End If
  801. Set oQueries = Connection.getQueries
  802. sObjects = oQueries.ElementNames()
  803. Select Case True
  804. Case IsMissing(pvIndex)
  805. Set oObject = New Collect
  806. Set oObject._This = oObject
  807. oObject._CollType = COLLQUERYDEFS
  808. Set oObject._Parent = _This
  809. oObject._Count = UBound(sObjects) + 1
  810. Goto Exit_Function
  811. Case VarType(pvIndex) = vbString
  812. bFound = False
  813. &apos; Check existence of object and find its exact (case-sensitive) name
  814. For i = 0 To UBound(sObjects)
  815. If UCase(pvIndex) = UCase(sObjects(i)) Then
  816. sObjectName = sObjects(i)
  817. bFound = True
  818. Exit For
  819. End If
  820. Next i
  821. If Not bFound Then Goto Trace_NotFound
  822. Case Else &apos; pvIndex is numeric
  823. If pvIndex &lt; 0 Or pvIndex &gt; UBound(sObjects) Then Goto Trace_IndexError
  824. sObjectName = sObjects(pvIndex)
  825. End Select
  826. Set oObject = New DataDef
  827. Set oObject._This = oObject
  828. oObject._Type = OBJQUERYDEF
  829. oObject._Name = sObjectName
  830. Set oObject._ParentDatabase = _This
  831. oObject._readOnly = _ReadOnly
  832. Set oObject.Query = oQueries.getByName(sObjectName)
  833. Exit_Function:
  834. Set QueryDefs = oObject
  835. Set oObject = Nothing
  836. Utils._ResetCalledSub(&quot;Database.QueryDefs&quot;)
  837. Exit Function
  838. Error_Function:
  839. TraceError(TRACEABORT, Err, &quot;Database.QueryDefs&quot;, Erl)
  840. GoTo Exit_Function
  841. Trace_NotFound:
  842. If Not pbCheck Then TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel(&quot;QUERY&quot;), pvIndex))
  843. Goto Exit_Function
  844. Trace_IndexError:
  845. TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(), 0)
  846. Goto Exit_Function
  847. End Function &apos; QueryDefs V1.1.0
  848. REM -----------------------------------------------------------------------------------------------------------------------
  849. Public Function Recordsets(ByVal Optional pvIndex As Variant) As Object
  850. &apos; Collect all active recordsets
  851. If _ErrorHandler() Then On Local Error Goto Error_Function
  852. Utils._SetCalledSub(&quot;Database.Recordsets&quot;)
  853. Set Recordsets = Nothing
  854. If Not IsMissing(pvIndex) Then
  855. If Not Utils._CheckArgument(pvIndex, 1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
  856. End If
  857. Dim sObjects() As String, sObjectName As String, oObject As Object
  858. Dim i As Integer, bFound As Boolean, oTables As Object
  859. Select Case True
  860. Case IsMissing(pvIndex)
  861. Set oObject = New Collect
  862. Set oObject._This = oObject
  863. oObject._CollType = COLLRECORDSETS
  864. Set oObject._Parent = _This
  865. oObject._Count = RecordsetsColl.Count
  866. Case VarType(pvIndex) = vbString
  867. bFound = _hasRecordset(pvIndex)
  868. If Not bFound Then Goto Trace_NotFound
  869. Set oObject = RecordsetsColl.Item(pvIndex)
  870. Case Else &apos; pvIndex is numeric
  871. If pvIndex &lt; 0 Or pvIndex &gt;= RecordsetsColl.Count Then Goto Trace_IndexError
  872. Set oObject = RecordsetsColl.Item(pvIndex + 1) &apos; Collection members are numbered 1 ... Count
  873. End Select
  874. Exit_Function:
  875. Set Recordsets = oObject
  876. Set oObject = Nothing
  877. Utils._ResetCalledSub(&quot;Database.Recordsets&quot;)
  878. Exit Function
  879. Error_Function:
  880. TraceError(TRACEABORT, Err, &quot;Database.Recordsets&quot;, Erl)
  881. GoTo Exit_Function
  882. Trace_NotFound:
  883. TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel(&quot;RECORDSET&quot;), pvIndex))
  884. Goto Exit_Function
  885. Trace_IndexError:
  886. TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(), 0)
  887. Goto Exit_Function
  888. End Function &apos; Recordsets V0.9.5
  889. REM -----------------------------------------------------------------------------------------------------------------------
  890. Public Function RunSQL(Optional ByVal pvSQL As Variant _
  891. , Optional ByVal pvOption As Variant _
  892. ) As Boolean
  893. &apos; Return True if the execution of the SQL statement was successful
  894. &apos; SQL must contain an ACTION query
  895. If _ErrorHandler() Then On Local Error Goto Error_Function
  896. Const cstThisSub = &quot;Database.RunSQL&quot;
  897. Utils._SetCalledSub(cstThisSub)
  898. RunSQL = False
  899. If IsMissing(pvSQL) Then Call _TraceArguments()
  900. If Not Utils._CheckArgument(pvSQL, 1, vbString) Then Goto Exit_Function
  901. Const cstNull = -1
  902. If IsMissing(pvOption) Then
  903. pvOption = cstNull
  904. Else
  905. If Not Utils._CheckArgument(pvOption, 2, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
  906. End If
  907. Dim oStatement As Object, vResult As Variant
  908. Set oStatement = Connection.createStatement()
  909. oStatement.EscapeProcessing = Not ( pvOption = dbSQLPassThrough )
  910. On Local Error Goto SQL_Error
  911. vResult = oStatement.execute(_ReplaceSquareBrackets(pvSQL))
  912. On Local Error Goto Error_Function
  913. RunSQL = True
  914. Exit_Function:
  915. Utils._ResetCalledSub(cstThisSub)
  916. Exit Function
  917. Error_Function:
  918. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  919. GoTo Exit_Function
  920. SQL_Error:
  921. TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(), 0, , pvSQL)
  922. Goto Exit_Function
  923. End Function &apos; RunSQL V1.1.0
  924. REM -----------------------------------------------------------------------------------------------------------------------
  925. Public Function TableDefs(ByVal Optional pvIndex As Variant, ByVal Optional pbCheck As Boolean) As Object
  926. &apos; Collect all tables in the database
  927. &apos; pbCheck unpublished
  928. Const cstThisSub = &quot;Database.TableDefs&quot;
  929. If _ErrorHandler() Then On Local Error Goto Error_Function
  930. Utils._SetCalledSub(cstThisSub)
  931. If IsMissing(pbCheck) Then pbCheck = False
  932. Dim sObjects() As String, sObjectName As String, oObject As Object
  933. Dim i As Integer, bFound As Boolean, oTables As Object
  934. Set oObject = Nothing
  935. If Not IsMissing(pvIndex) Then
  936. If Not Utils._CheckArgument(pvIndex, 1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
  937. End If
  938. Set oTables = Connection.getTables
  939. sObjects = oTables.ElementNames()
  940. Select Case True
  941. Case IsMissing(pvIndex)
  942. Set oObject = New Collect
  943. Set oObject._This = oObject
  944. oObject._CollType = COLLTABLEDEFS
  945. Set oObject._Parent = _This
  946. oObject._Count = UBound(sObjects) + 1
  947. Goto Exit_Function
  948. Case VarType(pvIndex) = vbString
  949. bFound = False
  950. &apos; Check existence of object and find its exact (case-sensitive) name
  951. For i = 0 To UBound(sObjects)
  952. If UCase(pvIndex) = UCase(sObjects(i)) Then
  953. sObjectName = sObjects(i)
  954. bFound = True
  955. Exit For
  956. End If
  957. Next i
  958. If Not bFound Then Goto Trace_NotFound
  959. Case Else &apos; pvIndex is numeric
  960. If pvIndex &lt; 0 Or pvIndex &gt; UBound(sObjects) Then Goto Trace_IndexError
  961. sObjectName = sObjects(pvIndex)
  962. End Select
  963. Set oObject = New DataDef
  964. With oObject
  965. ._This = oObject
  966. ._Type = OBJTABLEDEF
  967. ._Name = sObjectName
  968. Set ._ParentDatabase = _This
  969. ._ReadOnly = _ReadOnly
  970. Set .Table = oTables.getByName(sObjectName)
  971. .CatalogName = .Table.CatalogName
  972. .SchemaName = .Table.SchemaName
  973. .TableName = .Table.Name
  974. End With
  975. Exit_Function:
  976. Set TableDefs = oObject
  977. Set oObject = Nothing
  978. Utils._ResetCalledSub(cstThisSub)
  979. Exit Function
  980. Error_Function:
  981. TraceError(TRACEABORT, Err, cstThisSub, Erl)
  982. GoTo Exit_Function
  983. Trace_NotFound:
  984. If Not pbCheck Then TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(), 0, , Array(_GetLabel(&quot;TABLE&quot;), pvIndex))
  985. Goto Exit_Function
  986. Trace_IndexError:
  987. TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(), 0)
  988. Goto Exit_Function
  989. End Function &apos; TableDefs V1.1.0
  990. REM -----------------------------------------------------------------------------------------------------------------------
  991. REM --- PRIVATE FUNCTIONS ---
  992. REM -----------------------------------------------------------------------------------------------------------------------
  993. REM -----------------------------------------------------------------------------------------------------------------------
  994. Private Function _DFunction(ByVal psFunction As String _
  995. , ByVal psExpr As String _
  996. , ByVal psDomain As String _
  997. , ByVal pvCriteria As Variant _
  998. , ByVal Optional pvOrderClause As Variant _
  999. ) As Variant
  1000. &apos;Arguments: psFunction an optional aggregate function
  1001. &apos; psExpr: an SQL expression [might contain an aggregate function]
  1002. &apos; psDomain: a table- or queryname
  1003. &apos; pvCriteria: an optional WHERE clause
  1004. &apos; pcOrderClause: an optional order clause incl. &quot;DESC&quot; if relevant
  1005. If _ErrorHandler() Then On Local Error GoTo Error_Function
  1006. Dim oResult As Object &apos;To retrieve the value to find.
  1007. Dim vResult As Variant &apos;Return value for function.
  1008. Dim sSql As String &apos;SQL statement.
  1009. Dim oStatement As Object &apos;For CreateStatement method
  1010. Dim sExpr As String &apos;For inclusion of aggregate function
  1011. Dim sTempField As String &apos;Random temporary field in SQL expression
  1012. Dim sTarget as String, sWhere As String, sOrderBy As String, sLimit As String
  1013. Dim sProductName As String
  1014. vResult = Null
  1015. Randomize 2^14-1
  1016. sTempField = &quot;[TEMP&quot; &amp; Right(&quot;00000&quot; &amp; Int(100000 * Rnd), 5) &amp; &quot;]&quot;
  1017. If pvCriteria &lt;&gt; &quot;&quot; Then sWhere = &quot; WHERE &quot; &amp; pvCriteria Else sWhere = &quot;&quot;
  1018. If pvOrderClause &lt;&gt; &quot;&quot; Then sOrderBy = &quot; ORDER BY &quot; &amp; pvOrderClause Else sOrderBy = &quot;&quot;
  1019. sLimit = &quot;&quot;
  1020. sProductName = UCase(MetaData.getDatabaseProductName())
  1021. Select Case sProductName
  1022. Case &quot;MYSQL&quot;, &quot;SQLITE&quot;
  1023. If psFunction = &quot;&quot; Then
  1024. sTarget = psExpr
  1025. sLimit = &quot; LIMIT 1&quot;
  1026. Else
  1027. sTarget = UCase(psFunction) &amp; &quot;(&quot; &amp; psExpr &amp; &quot;)&quot;
  1028. End If
  1029. sSql = &quot;SELECT &quot; &amp; sTarget &amp; &quot; AS &quot; &amp; sTempField &amp; &quot; FROM &quot; &amp; psDomain &amp; sWhere &amp; sOrderBy &amp; sLimit
  1030. Case &quot;FIREBIRD (ENGINE12)&quot;
  1031. If psFunction = &quot;&quot; Then sTarget = &quot;FIRST 1 &quot; &amp; psExpr Else sTarget = UCase(psFunction) &amp; &quot;(&quot; &amp; psExpr &amp; &quot;)&quot;
  1032. sSql = &quot;SELECT &quot; &amp; sTarget &amp; &quot; AS &quot; &amp; sTempField &amp; &quot; FROM &quot; &amp; psDomain &amp; sWhere &amp; sOrderBy
  1033. Case Else &apos; Standard syntax - Includes HSQLDB
  1034. If psFunction = &quot;&quot; Then sTarget = &quot;TOP 1 &quot; &amp; psExpr Else sTarget = UCase(psFunction) &amp; &quot;(&quot; &amp; psExpr &amp; &quot;)&quot;
  1035. sSql = &quot;SELECT &quot; &amp; sTarget &amp; &quot; AS &quot; &amp; sTempField &amp; &quot; FROM &quot; &amp; psDomain &amp; sWhere &amp; sOrderBy
  1036. End Select
  1037. &apos;Lookup the value.
  1038. Set oStatement = Connection.createStatement()
  1039. With oStatement
  1040. .ResultSetType = com.sun.star.sdbc.ResultSetType.FORWARD_ONLY
  1041. .ResultSetConcurrency = com.sun.star.sdbc.ResultSetConcurrency.READ_ONLY
  1042. .EscapeProcessing = False
  1043. sSql = _ReplaceSquareBrackets(sSql) &apos;Substitute [] by quote string
  1044. Set oResult = .executeQuery(sSql)
  1045. If Not IsNull(oResult) And Not IsEmpty(oResult) Then
  1046. If Not oResult.next() Then Goto Exit_Function
  1047. vResult = Utils._getResultSetColumnValue(oResult, 1, True) &apos; Force return of binary field
  1048. End If
  1049. End With
  1050. Exit_Function:
  1051. &apos;Assign the returned value.
  1052. _DFunction = vResult
  1053. Set oResult = Nothing
  1054. Set oStatement = Nothing
  1055. Exit Function
  1056. Error_Function:
  1057. TraceError(TRACEFATAL, ERRDFUNCTION, _A2B_.CalledSub, 0, , sSQL)
  1058. Goto Exit_Function
  1059. End Function &apos; DFunction V1.5.0
  1060. REM -----------------------------------------------------------------------------------------------------------------------
  1061. Private Function _FilterOptionsDefault(ByVal plEncoding As Long) As String
  1062. &apos; Return the default FilterOptions string for table/query export to csv
  1063. Dim sFieldSeparator as string
  1064. Const cstComma = &quot;,&quot;
  1065. Const cstTextDelimitor = &quot;&quot;&quot;&quot;
  1066. If _DecimalPoint() = &quot;,&quot; Then sFieldSeparator = &quot;;&quot; Else sFieldSeparator = cstComma
  1067. _FilteroptionsDefault = Trim(Str(Asc(sFieldSeparator))) _
  1068. &amp; cstComma &amp; Trim(Str(Asc(cstTextDelimitor))) _
  1069. &amp; cstComma &amp; Trim(Str(plEncoding)) _
  1070. &amp; cstComma &amp; &quot;1&quot;
  1071. End Function &apos; _FilterOptionsDefault V1.4.0
  1072. REM -----------------------------------------------------------------------------------------------------------------------
  1073. Public Function _hasRecordset(ByVal psName As String) As Boolean
  1074. &apos; Return True if psName if in the collection of Recordsets
  1075. Dim oRecordset As Object
  1076. If _ErrorHandler() Then On Local Error Goto Error_Function
  1077. Set oRecordset = RecordsetsColl.Item(psName)
  1078. _hasRecordset = True
  1079. Exit_Function:
  1080. Exit Function
  1081. Error_Function: &apos; Item by key aborted
  1082. _hasRecordset = False
  1083. GoTo Exit_Function
  1084. End Function &apos; _hasRecordset V0.9.5
  1085. REM -----------------------------------------------------------------------------------------------------------------------
  1086. Private Sub _LoadMetadata()
  1087. &apos; Load essentially getTypeInfo() results from Metadata
  1088. Dim sProduct As String
  1089. Dim iInfo As Integer, oTypeInfo As Object, sName As String, lType As Integer
  1090. Const cstMaxInfo = 40
  1091. ReDim _ColumnTypes(0 To cstMaxInfo)
  1092. ReDim _ColumnTypeNames(0 To cstMaxInfo)
  1093. ReDim _ColumnPrecisions(0 To cstMaxInfo)
  1094. Const cstHSQLDB1 = &quot;HSQL Database Engine 1.&quot;
  1095. Const cstHSQLDB2 = &quot;HSQL Database Engine 2.&quot;
  1096. Const cstFirebird = &quot;sdbc:embedded:firebird&quot;
  1097. Const cstMSAccess2003 = &quot;MS Jet 0&quot;
  1098. Const cstMSAccess2007 = &quot;MS Jet 04.&quot;
  1099. Const cstMYSQL = &quot;MySQL&quot;
  1100. Const cstPOSTGRES = &quot;PostgreSQL&quot;
  1101. Const cstSQLITE = &quot;SQLite&quot;
  1102. With com.sun.star.sdbc.DataType
  1103. _ColumnTypesReference = Array( _
  1104. .ARRAY _
  1105. , .BIGINT _
  1106. , .BINARY _
  1107. , .BIT _
  1108. , .BLOB _
  1109. , .BOOLEAN _
  1110. , .CHAR _
  1111. , .CLOB _
  1112. , .DATE _
  1113. , .DECIMAL _
  1114. , .DISTINCT _
  1115. , .DOUBLE _
  1116. , .FLOAT _
  1117. , .INTEGER _
  1118. , .LONGVARBINARY _
  1119. , .LONGVARCHAR _
  1120. , .NUMERIC _
  1121. , .OBJECT _
  1122. , .OTHER _
  1123. , .REAL _
  1124. , .REF _
  1125. , .SMALLINT _
  1126. , .SQLNULL _
  1127. , .STRUCT _
  1128. , .TIME _
  1129. , .TIMESTAMP _
  1130. , .TINYINT _
  1131. , .VARBINARY _
  1132. , .VARCHAR _
  1133. )
  1134. End With
  1135. With Metadata
  1136. sProduct = .getDatabaseProductName() &amp; &quot; &quot; &amp; .getDatabaseProductVersion
  1137. Select Case True
  1138. Case Len(sProduct) &gt; Len(cstHSQLDB1) And Left(sProduct, Len(cstHSQLDB1)) = cstHSQLDB1
  1139. _RDBMS = DBMS_HSQLDB1
  1140. _ColumnTypesAlias = Array(0, -5, -2, 16, -4, 16, 1, -1, 91, 3, 0, 8, 6, 4, -4, -1, 2, 0, 0, 7, 0, 5, 0, 0, 92, 93, -6, -3, 12)
  1141. _BinaryStream = True
  1142. Case Len(sProduct) &gt; Len(cstHSQLDB2) And Left(sProduct, Len(cstHSQLDB2)) = cstHSQLDB2
  1143. _RDBMS = DBMS_HSQLDB2
  1144. _ColumnTypesAlias = Array(0, -5, -3, -7, 2004, 16, 1, 2005, 91, 3, 0, 8, 8, 4, -3, 12, 2, 0, 0, 8, 0, 5, 0, 0, 92, 93, -6, -3, 12)
  1145. _BinaryStream = True
  1146. Case .URL = cstFirebird &apos; Only embedded 3.0
  1147. _RDBMS = DBMS_FIREBIRD
  1148. _ColumnTypesAlias = Array(0, -5, -2, 16, 2004, 16, 1, 2005, 91, 3, 0, 8, 6, 4, -4, 2005, 2, 0, 0, 8, 0, 5, 0, 0, 92, 93, 4, 2004, 12)
  1149. _BinaryStream = True
  1150. Case Len(sProduct) &gt; Len(cstMSAccess2007) And Left(sProduct, Len(cstMSAccess2007)) = cstMSAccess2007
  1151. _RDBMS = DBMS_MSACCESS2007
  1152. _ColumnTypesAlias = Array(0, 4, -2, 16, -2, 16, 12, 12, 93, 8, 0, 8, 6, 4, -3, 12, 2, 0, 0, 8, 0, 5, 0, 0, 93, 93, -6, -2, 12)
  1153. _BinaryStream = True
  1154. Case Len(sProduct) &gt; Len(cstMSAccess2003) And Left(sProduct, Len(cstMSAccess2003)) = cstMSAccess2003
  1155. _RDBMS = DBMS_MSACCESS2003
  1156. _ColumnTypesAlias = Array(0, 4, -2, 16, -2, 16, 12, 12, 93, 8, 0, 8, 6, 4, -3, 12, 2, 0, 0, 8, 0, 5, 0, 0, 93, 93, -6, -2, 12)
  1157. _BinaryStream = True
  1158. Case Len(sProduct) &gt; Len(cstMYSQL) And Left(sProduct, Len(cstMYSQL)) = cstMYSQL
  1159. _RDBMS = DBMS_MYSQL
  1160. _ColumnTypesAlias = Array(0, -5, -2, -7, -4, -7, 1, -1, 91, 3, 0, 8, 8, 4, -4, -1, 2, 0, 0, 7, 0, 5, 0, 0, 92, 93, -6, -3, -1)
  1161. _BinaryStream = False
  1162. Case Len(sProduct) &gt; Len(cstPOSTGRES) And Left(sProduct, Len(cstPOSTGRES)) = cstPOSTGRES
  1163. _RDBMS = DBMS_POSTGRES
  1164. _ColumnTypesAlias = Array(0, -5, -3, 16, -3, 16, 1, 12, 91, 8, 0, 8, 8, 4, -3, 12, 2, 0, 0, 7, 0, 5, 0, 0, 92, 93, 4, -3, 12)
  1165. _BinaryStream = True
  1166. Case Len(sProduct) &gt; Len(cstSQLITE) And Left(sProduct, Len(cstSQLITE)) = cstSQLITE
  1167. _RDBMS = DBMS_SQLITE
  1168. _ColumnTypesAlias = Array(0, -5, -4, -7, -4, -7, 1, -1, 91, 8, 0, 8, 6, 4, -4, -1, 8, 0, 0, 8, 0, 5, 0, 0, 92, 93, -6, -4, 12)
  1169. _BinaryStream = True
  1170. Case Else
  1171. _RDBMS = DBMS_UNKNOWN
  1172. _BinaryStream = True
  1173. End Select
  1174. iInfo = -1
  1175. Set oTypeInfo = MetaData.getTypeInfo()
  1176. With oTypeInfo
  1177. .next()
  1178. Do While Not .isAfterLast() And iInfo &lt; cstMaxInfo
  1179. sName = .getString(1)
  1180. lType = .getLong(2)
  1181. If _RDBMS = DBMS_POSTGRES And (Left(sName, 1) &lt;&gt; &quot;_&quot; Or lType &lt;&gt; -1) Then &apos; Skip
  1182. Else
  1183. iInfo = iInfo + 1
  1184. _ColumnTypeNames(iInfo) = sName
  1185. _ColumnTypes(iInfo) = lType
  1186. _ColumnPrecisions(iInfo) = CLng(.getLong(3))
  1187. End If
  1188. .next()
  1189. Loop
  1190. End With
  1191. ReDim Preserve _ColumnTypes(0 To iInfo)
  1192. ReDim Preserve _ColumnTypeNames(0 To iInfo)
  1193. ReDim Preserve _ColumnPrecisions(0 To iInfo)
  1194. End With
  1195. End Sub &apos; _LoadMetadata V1.6.0
  1196. REM -----------------------------------------------------------------------------------------------------------------------
  1197. Private Function _OutputBinaryToHTML() As String
  1198. &apos; Converts Binary value to HTML compatible string
  1199. _OutputBinaryToHTML = &quot;&amp;nbsp;&quot;
  1200. End Function &apos; _OutputBinaryToHTML V1.4.0
  1201. REM -----------------------------------------------------------------------------------------------------------------------
  1202. Private Function _OutputBooleanToHTML(ByVal pbBool As Boolean) As String
  1203. &apos; Converts input boolean value to HTML compatible string
  1204. _OutputBooleanToHTML = Iif(pbBool, &quot;&amp;#x2714;&quot;, &quot;&amp;#x2716;&quot;) &apos; ✔ and ✖
  1205. End Function &apos; _OutputBooleanToHTML V1.4.0
  1206. REM -----------------------------------------------------------------------------------------------------------------------
  1207. Private Function _OutputClassToHTML(ByVal pvArray As Variant) As String
  1208. &apos; Formats classes attribute of &lt;tr&gt; and &lt;td&gt; tags
  1209. If Not IsArray(pvArray) Then
  1210. _OutputClassToHTML = &quot;&quot;
  1211. ElseIf UBound(pvArray) &lt; LBound(pvArray) Then
  1212. _OutputClassToHTML = &quot;&quot;
  1213. Else
  1214. _OutputClassToHTML = &quot; class=&quot;&quot;&quot; &amp; Join(pvArray, &quot; &quot;) &amp; &quot;&quot;&quot;&quot;
  1215. End If
  1216. End Function &apos; _OutputClassToHTML V1.4.0
  1217. REM -----------------------------------------------------------------------------------------------------------------------
  1218. Private Function _OutputDataToHTML(ByRef pvTable As Variant, ByVal pvName As String, ByVal piFile As Integer _
  1219. , ByRef Optional pvHeaders As Variant _
  1220. , ByRef Optional pvData As Variant _
  1221. ) As Boolean
  1222. &apos; Write html tags around data found in pvTable
  1223. &apos; Exit when error without execution stop (to avoid file remaining open ...)
  1224. Dim oTableRS As Object, vData() As Variant, i As Integer, j As Integer
  1225. Dim vFieldsBin() As Variant, iDataType As Integer, iNumRows As Integer, iNumFields As Integer, vDataCell As Variant
  1226. Dim bDataArray As Boolean, sHeader As String
  1227. Dim vTrClass() As Variant, vTdClass As Variant, iCountRows As Integer, iLastRow As Integer
  1228. Const cstMaxRows = 200
  1229. On Local Error GoTo Error_Function
  1230. bDataArray = IsNull(pvTable)
  1231. Print #piFile, &quot; &lt;table class=&quot;&quot;dbdatatable&quot;&quot;&gt;&quot;
  1232. Print #piFile, &quot; &lt;caption&gt;&quot; &amp; pvName &amp; &quot;&lt;/caption&gt;&quot;
  1233. vFieldsBin() = Array()
  1234. If bDataArray Then
  1235. Set oTableRS = Nothing
  1236. iNumFields = UBound(pvHeaders) + 1
  1237. ReDim vFieldsBin(0 To iNumFields - 1)
  1238. For i = 0 To iNumFields - 1
  1239. vFieldsBin(i) = False
  1240. Next i
  1241. Else
  1242. Set oTableRS = pvTable.OpenRecordset( , , dbReadOnly)
  1243. iNumFields = oTableRS.Fields.Count
  1244. ReDim vFieldsBin(0 To iNumFields - 1)
  1245. With com.sun.star.sdbc.DataType
  1246. For i = 0 To iNumFields - 1
  1247. iDataType = oTableRS.Fields(i).DataType
  1248. vFieldsBin(i) = Utils._IsBinaryType(iDataType)
  1249. Next i
  1250. End With
  1251. End If
  1252. With oTableRS
  1253. Print #piFile, &quot; &lt;thead&gt;&quot;
  1254. Print #piFile, &quot; &lt;tr&gt;&quot;
  1255. For i = 0 To iNumFields - 1
  1256. If bDataArray Then sHeader = pvHeaders(i) Else sHeader = .Fields(i)._Name
  1257. Print #piFile, &quot; &lt;th scope=&quot;&quot;col&quot;&quot;&gt;&quot; &amp; sHeader &amp; &quot;&lt;/th&gt;&quot;
  1258. Next i
  1259. Print #piFile, &quot; &lt;/tr&gt;&quot;
  1260. Print #piFile, &quot; &lt;/thead&gt;&quot;
  1261. Print #piFile, &quot; &lt;tfoot&gt;&quot;
  1262. Print #piFile, &quot; &lt;/tfoot&gt;&quot;
  1263. Print #piFile, &quot; &lt;tbody&gt;&quot;
  1264. If bDataArray Then
  1265. iLastRow = UBound(pvData, 2) + 1
  1266. Else
  1267. .MoveLast
  1268. iLastRow = .RecordCount
  1269. .MoveFirst
  1270. End If
  1271. iCountRows = 0
  1272. Do While iCountRows &lt; iLastRow
  1273. If bDataArray Then
  1274. iNumRows = iLastRow
  1275. Else
  1276. vData() = .GetRows(cstMaxRows)
  1277. iNumRows = UBound(vData, 2) + 1
  1278. End If
  1279. For j = 0 To iNumRows - 1
  1280. iCountRows = iCountRows + 1
  1281. vTrClass() = Array()
  1282. If iCountRows = 1 Then vTrClass() = _AddArray(vTrClass, &quot;firstrow&quot;)
  1283. If iCountRows = iLastRow Then vTrClass() = _AddArray(vTrClass, &quot;lastrow&quot;)
  1284. If (iCountRows Mod 2) = 0 Then vTrClass() = _AddArray(vTrClass, &quot;even&quot;) Else vTrClass() = _AddArray(vTrClass, &quot;odd&quot;)
  1285. Print #piFile, &quot; &lt;tr&quot; &amp; _OutputClassToHTML(vTrClass) &amp; &quot;&gt;&quot;
  1286. For i = 0 To iNumFields - 1
  1287. vTdClass() = Array()
  1288. If i = 0 Then vTdClass() = _AddArray(vTdClass, &quot;firstcol&quot;)
  1289. If i = iNumFields - 1 Then vTdClass() = _AddArray(vTdClass, &quot;lastcol&quot;)
  1290. If Not vFieldsBin(i) Then
  1291. If bDataArray Then vDataCell = pvData(i, j) Else vDataCell = vData(i, j)
  1292. If vDataCell Is Nothing Then vDataCell = Null &apos; Necessary because Null object has not a VarType = vbNull
  1293. If VarType(vDataCell) = vbString Then &apos; Null string gives IsDate = True !
  1294. If Len(vDataCell) &gt; 0 And IsDate(vDataCell) Then vDataCell = CDate(vDataCell)
  1295. End If
  1296. Select Case VarType(vDataCell)
  1297. Case vbEmpty, vbNull
  1298. vTdClass() = _AddArray(vTdClass, &quot;null&quot;)
  1299. Print #piFile, &quot; &lt;td&quot; &amp; _OutputClassToHTML(vTdClass) &amp; &quot;&gt;&quot; &amp; _OutputNullToHTML() &amp; &quot;&lt;/td&gt;&quot;
  1300. Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDecimal, vbUShort, vbULong, vbBigInt
  1301. vTdClass() = _AddArray(vTdClass, &quot;numeric&quot;)
  1302. If vDataCell &lt; 0 Then vTdClass() = _AddArray(vTdClass, &quot;negative&quot;)
  1303. Print #piFile, &quot; &lt;td&quot; &amp; _OutputClassToHTML(vTdClass) &amp; &quot;&gt;&quot; &amp; _OutputNumberToHTML(vDataCell) &amp; &quot;&lt;/td&gt;&quot;
  1304. Case vbBoolean
  1305. vTdClass() = _AddArray(vTdClass, &quot;bool&quot;)
  1306. If vDataCell = False Then vTdClass() = _AddArray(vTdClass, &quot;false&quot;)
  1307. Print #piFile, &quot; &lt;td&quot; &amp; _OutputClassToHTML(vTdClass) &amp; &quot;&gt;&quot; &amp; _OutputBooleanToHTML(vDataCell) &amp; &quot;&lt;/td&gt;&quot;
  1308. Case vbDate
  1309. vTdClass() = _AddArray(vTdClass, &quot;date&quot;)
  1310. Print #piFile, &quot; &lt;td&quot; &amp; _OutputClassToHTML(vTdClass) &amp; &quot;&gt;&quot; &amp; _OutputDateToHTML(vDataCell) &amp; &quot;&lt;/td&gt;&quot;
  1311. Case vbString
  1312. vTdClass() = _AddArray(vTdClass, &quot;char&quot;)
  1313. Print #piFile, &quot; &lt;td&quot; &amp; _OutputClassToHTML(vTdClass) &amp; &quot;&gt;&quot; &amp; _OutputStringToHTML(vDataCell) &amp; &quot;&lt;/td&gt;&quot;
  1314. Case Else
  1315. Print #piFile, &quot; &lt;td&quot; &amp; _OutputClassToHTML(vTdClass) &amp; &quot;&gt;&quot; &amp; _CStr(vDataCell) &amp; &quot;&lt;/td&gt;&quot;
  1316. End Select
  1317. Else &apos; Binary fields
  1318. Print #piFile, &quot; &lt;td&quot; &amp; _OutputClassToHTML(vTdClass) &amp; &quot;&gt;&quot; &amp; _OutputBinaryToHTML() &amp; &quot;&lt;/td&gt;&quot;
  1319. End If
  1320. Next i
  1321. Print #piFile, &quot; &lt;/tr&gt;&quot;
  1322. Next j
  1323. Loop
  1324. If Not bDataArray Then .mClose()
  1325. End With
  1326. Set oTableRS = Nothing
  1327. Print #piFile, &quot; &lt;/tbody&gt;&quot;
  1328. Print #piFile, &quot; &lt;/table&gt;&quot;
  1329. _OutputDataToHTML = True
  1330. Exit_Function:
  1331. Exit Function
  1332. Error_Function:
  1333. TraceError(TRACEWARNING, Err, &quot;_OutputDataToHTML&quot;, Erl)
  1334. _OutputDataToHTML = False
  1335. Resume Exit_Function
  1336. End Function &apos; _OutputDataToHTML V1.4.0
  1337. REM -----------------------------------------------------------------------------------------------------------------------
  1338. Private Function _OutputDateToHTML(ByVal psDate As Date) As String
  1339. &apos; Converts input date to HTML compatible string
  1340. _OutputDateToHTML = Format(psDate) &apos; With regional settings - Ignores time if = to 0
  1341. End Function &apos; _OutputDateToHTML V1.4.0
  1342. REM -----------------------------------------------------------------------------------------------------------------------
  1343. Private Function _OutputNullToHTML() As String
  1344. &apos; Converts Null value to HTML compatible string
  1345. _OutputNullToHTML = &quot;&amp;nbsp;&quot;
  1346. End Function &apos; _OutputNullToHTML V1.4.0
  1347. REM -----------------------------------------------------------------------------------------------------------------------
  1348. Private Function _OutputNumberToHTML(ByVal pvNumber As Variant, ByVal Optional piPrecision As Integer) As String
  1349. &apos; Converts input number to HTML compatible string
  1350. Dim vNumber As Variant
  1351. If IsMissing(piPrecision) Then piPrecision = -1
  1352. If pvNumber = Int(pvNumber) Then
  1353. vNumber = Int(pvNumber)
  1354. Else
  1355. If piPrecision &gt;= 0 Then vNumber = (Int(pvNumber * 10 ^ piPrecision + 0.5)) / 10 ^ piPrecision Else vNumber = pvNumber
  1356. End If
  1357. _OutputNumberToHTML = Format(vNumber)
  1358. End Function &apos; _OutputNumberToHTML V1.4.0
  1359. REM -----------------------------------------------------------------------------------------------------------------------
  1360. Private Function _OutputStringToHTML(ByVal psString As String) As String
  1361. &apos; Converts input string to HTML compatible string
  1362. &apos; - UTF-8 encoding
  1363. &apos; - recognition of next patterns
  1364. &apos; - &amp;quot; - &amp;amp; - &amp;apos; - &amp;lt; - &amp;gt;
  1365. &apos; - &lt;pre&gt;
  1366. &apos; - &lt;a href=&quot;...
  1367. &apos; - &lt;br&gt;
  1368. &apos; - &lt;img src=&quot;...
  1369. &apos; - &lt;b&gt;, &lt;u&gt;, &lt;i&gt;
  1370. Dim vPatterns As Variant
  1371. Dim lCurrentChar as Long, lPattern As Long, lNextPattern As Long, sPattern As String
  1372. Dim sOutput As String, sChar As String
  1373. Dim sUrl As String, lNextQuote As Long, lUrl As Long, bQuote As Boolean, bTagEnd As Boolean
  1374. Dim i As Integer, l As Long
  1375. vPatterns = Array( _
  1376. &quot;&amp;quot;&quot;, &quot;&amp;amp;&quot;, &quot;&amp;apos;&quot;, &quot;&amp;lt;&quot;, &quot;&amp;gt;&quot;, &quot;&amp;nbsp;&quot; _
  1377. , &quot;&lt;pre&gt;&quot;, &quot;&lt;/pre&gt;&quot;, &quot;&lt;br&gt;&quot; _
  1378. , &quot;&lt;a href=&quot;&quot;&quot;, &quot;&lt;a id=&quot;&quot;&quot;, &quot;&lt;/a&gt;&quot;, &quot;&lt;img src=&quot;&quot;&quot; _
  1379. , &quot;&lt;span class=&quot;&quot;&quot;, &quot;&lt;/span&gt;&quot; _
  1380. , &quot;&lt;b&gt;&quot;, &quot;&lt;/b&gt;&quot;, &quot;&lt;u&gt;&quot;, &quot;&lt;/u&gt;&quot;, &quot;&lt;i&gt;&quot;, &quot;&lt;/i&gt;&quot; _
  1381. )
  1382. lCurrentChar = 1
  1383. sOutput = &quot;&quot;
  1384. Do While lCurrentChar &lt;= Len(psString)
  1385. &apos; Where is next closest pattern ?
  1386. lPattern = Len(psString) + 1
  1387. sPattern = &quot;&quot;
  1388. For i = 0 To UBound(vPatterns)
  1389. lNextPattern = InStr(lCurrentChar, psString, vPatterns(i), 1) &apos; Text (not case-sensitive) string comparison
  1390. If lNextPattern &gt; 0 And lNextPattern &lt; lPattern Then
  1391. lPattern = lNextPattern
  1392. sPattern = Mid(psString, lPattern, Len(vPatterns(i)))
  1393. End If
  1394. Next i
  1395. &apos; Up to the next pattern or to the end of the string, UTF8-encode each character
  1396. For l = lCurrentChar To lPattern - 1
  1397. sChar = Mid(psString, l, 1)
  1398. sOutput = sOutput &amp; Utils._UTF8Encode(sChar)
  1399. Next l
  1400. &apos; Process hyperlink patterns and keep others
  1401. If Len(sPattern) &gt; 0 Then
  1402. Select Case LCase(sPattern)
  1403. Case &quot;&lt;a href=&quot;&quot;&quot;, &quot;&lt;a id=&quot;&quot;&quot;, &quot;&lt;img src=&quot;&quot;&quot;, &quot;&lt;span class=&quot;&quot;&quot;
  1404. &apos; Up to next quote, url-encode
  1405. lNextQuote = 0
  1406. lUrl = lPattern + Len(sPattern)
  1407. lNextQuote = InStr(lUrl, psString, &quot;&quot;&quot;&quot;, 1)
  1408. If lNextQuote = 0 Then lNextQuote = Len(psString) &apos; Should not happen but, if quoted string not closed ...
  1409. sUrl = Mid(psString, lUrl, lNextQuote - lUrl)
  1410. sOutput = sOutput &amp; sPattern &amp; sUrl &amp; &quot;&quot;&quot;&quot;
  1411. lCurrentChar = lNextQuote + 1
  1412. bQuote = False
  1413. bTagEnd = False
  1414. Do
  1415. sChar = Mid(psString, lCurrentChar, 1)
  1416. Select Case sChar
  1417. Case &quot;&quot;&quot;&quot;
  1418. bQuote = Not bQuote
  1419. sOutput = sOutput &amp; sChar
  1420. Case &quot;&gt;&quot; &apos; Tag end if not somewhere between quotes
  1421. If Not bQuote Then
  1422. bTagEnd = True
  1423. sOutput = sOutput &amp; sChar
  1424. Else
  1425. sOutput = sOutput &amp; _UTF8Encode(sChar)
  1426. End If
  1427. Case Else
  1428. sOutput = sOutput &amp; _UTF8Encode(sChar)
  1429. End Select
  1430. lCurrentChar = lCurrentChar + 1
  1431. If lCurrentChar &gt; Len(psString) Then bTagEnd = True &apos; Should not happen but, if tag not closed ...
  1432. Loop Until bTagEnd
  1433. Case Else
  1434. sOutput = sOutput &amp; sPattern
  1435. lCurrentChar = lPattern + Len(sPattern)
  1436. End Select
  1437. Else
  1438. lCurrentChar = Len(psString) + 1
  1439. End If
  1440. Loop
  1441. _OutputStringToHTML = sOutput
  1442. End Function &apos; _OutputStringToHTML V1.4.0
  1443. REM -----------------------------------------------------------------------------------------------------------------------
  1444. Private Function _OutputToCalc(poData As Object _
  1445. , ByVal psOutputFile As String _
  1446. , ByVal psFilter As String _
  1447. , Optional ByVal plEncoding As Long _
  1448. ) As Boolean
  1449. &apos; https://wiki.documentfoundation.org/Documentation/DevGuide/Spreadsheet_Documents#Database_Import
  1450. &apos; https://wiki.documentfoundation.org/Documentation/DevGuide/Spreadsheet_Documents#Filter_Options
  1451. Dim oCalcDoc As Object, oSheet As Object, vWin As Variant
  1452. Dim vImportDesc() As Variant, iSource As Integer
  1453. Dim oRange As Object, i As Integer, iCol As Integer, oColumns As Object
  1454. If _ErrorHandler() Then On Local Error Goto Error_Function
  1455. _OutputToCalc = False
  1456. If IsMissing(plEncoding) Then plEncoding = acUTF8Encoding
  1457. &apos; Create a new OO-Calc-Document
  1458. Set oCalcDoc = StarDesktop.LoadComponentFromURL( _
  1459. &quot;private:factory/scalc&quot; _
  1460. , &quot;_default&quot; ,0, Array() _
  1461. )
  1462. &apos; Get the unique spreadsheet
  1463. Set oSheet = oCalcDoc.Sheets(0)
  1464. &apos; Describe import
  1465. With poData
  1466. If ._Type = &quot;TABLEDEF&quot; Then
  1467. iSource = com.sun.star.sheet.DataImportMode.TABLE
  1468. Else
  1469. iSource = com.sun.star.sheet.DataImportMode.QUERY
  1470. End If
  1471. vImportDesc = Array( _
  1472. _MakePropertyValue(&quot;DatabaseName&quot;, URL) _
  1473. , _MakePropertyValue(&quot;SourceType&quot;, iSource) _
  1474. , _MakePropertyValue(&quot;SourceObject&quot;, ._Name) _
  1475. )
  1476. oSheet.Name = ._Name
  1477. End With
  1478. &apos; Import
  1479. oSheet.getCellByPosition(0, 0).doImport(vImportDesc())
  1480. Select Case psFilter
  1481. Case acFormatODS, acFormatXLS, acFormatXLSX &apos; Formatting
  1482. iCol = poData.Fields().Count
  1483. Set oRange = oSheet.getCellRangeByPosition(0, 0, iCol - 1, 0)
  1484. oRange.CharWeight = com.sun.star.awt.FontWeight.BOLD
  1485. oRange.CellBackColor = RGB(200, 200, 200)
  1486. oRange.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
  1487. Set oColumns = oRange.getColumns()
  1488. For i = 0 To iCol - 1
  1489. oColumns.getByIndex(i).OptimalWidth = True
  1490. Next i
  1491. oCalcDoc.storeAsUrl(psOutputFile, Array( _
  1492. _MakePropertyValue(&quot;FilterName&quot;, psFilter) _
  1493. , _MakePropertyValue(&quot;Overwrite&quot;, True) _
  1494. ))
  1495. Case Else
  1496. oCalcDoc.storeAsUrl(psOutputFile, Array( _
  1497. _MakePropertyValue(&quot;FilterName&quot;, psFilter) _
  1498. , _MakePropertyValue(&quot;FilterOptions&quot;, _FilterOptionsDefault(plEncoding)) _
  1499. , _MakePropertyValue(&quot;Overwrite&quot;, True) _
  1500. ))
  1501. End Select
  1502. oCalcDoc.close(False)
  1503. _OutputToCalc = True
  1504. Exit_Function:
  1505. Set oColumns = Nothing
  1506. Set oRange = Nothing
  1507. Set oSheet = Nothing
  1508. Set oCalcDoc = Nothing
  1509. Exit Function
  1510. Error_Function:
  1511. TraceError(TRACEABORT, ERRDFUNCTION, _A2B_.CalledSub, 0, , sSQL)
  1512. Goto Exit_Function
  1513. End Function &apos; OutputToCalc V1.4.0
  1514. REM -----------------------------------------------------------------------------------------------------------------------
  1515. Public Function _OutputToHTML(ByRef pvTable As Variant, ByVal pvName As String, ByVal psOutputFile As String, ByVal psTemplateFile As String _
  1516. , ByRef Optional pvHeaders As Variant _
  1517. , ByRef Optional pvData As Variant _
  1518. ) As Boolean
  1519. &apos; http://www.ehow.com/how_5652706_create-html-template-ms-access.html
  1520. Dim bDataArray As Boolean
  1521. Dim vMinimalTemplate As Variant, vTemplate As Variant
  1522. Dim iFile As Integer, i As Integer, sLine As String, lBody As Long
  1523. Const cstTitle = &quot;&lt;!--Template_Title--&gt;&quot;, cstBody = &quot;&lt;!--Template_Body--&gt;&quot;
  1524. Const cstTitleAlt = &quot;&lt;!--AccessTemplate_Title--&gt;&quot;, cstBodyAlt = &quot;&lt;!--AccessTemplate_Body--&gt;&quot;
  1525. On Local Error GoTo Error_Function
  1526. vMinimalTemplate = Array( _
  1527. &quot;&lt;!DOCTYPE html&gt;&quot; _
  1528. , &quot;&lt;html&gt;&quot; _
  1529. , &quot; &lt;head&gt;&quot; _
  1530. , &quot; &lt;title&gt;&quot; &amp; cstTitle &amp; &quot;&lt;/title&gt;&quot; _
  1531. , &quot; &lt;/head&gt;&quot; _
  1532. , &quot; &lt;body&gt;&quot; _
  1533. , &quot; &quot; &amp; cstBody _
  1534. , &quot; &lt;/body&gt;&quot; _
  1535. , &quot;&lt;/html&gt;&quot; _
  1536. )
  1537. vTemplate = _ReadFileIntoArray(psTemplateFile)
  1538. If LBound(vTemplate) &gt; UBound(vTemplate) Then vTemplate() = vMinimalTemplate()
  1539. bDataArray = IsNull(pvTable)
  1540. &apos; Write output file
  1541. iFile = FreeFile()
  1542. Open psOutputFile For Output Access Write Lock Read Write As #iFile
  1543. For i = 0 To UBound(vTemplate)
  1544. sLine = vTemplate(i)
  1545. sLine = Join(Split(sLine, cstTitleAlt), cstTitle)
  1546. sLine = Join(Split(sLine, cstBodyAlt), cstBody)
  1547. Select Case True
  1548. Case InStr(sLine, cstTitle) &gt; 0
  1549. sLine = Join(Split(sLine, cstTitle), pvName)
  1550. Print #iFile, sLine
  1551. Case InStr(sLine, cstBody) &gt; 0
  1552. lBody = InStr(sLine, cstBody)
  1553. If lBody &gt; 1 Then Print #iFile, Left(sLine, lBody - 1)
  1554. If bDataArray Then
  1555. _OutputDataToHTML(pvTable, pvName, iFile, pvHeaders, pvData)
  1556. Else
  1557. _OutputDataToHTML(pvTable, pvName, iFile)
  1558. End If
  1559. If Len(sLine) &gt; lBody + Len(cstBody) - 1 Then Print #iFile, Right(sLine, Len(sLine) - lBody + Len(cstBody) + 1)
  1560. Case Else
  1561. Print #iFile, sLine
  1562. End Select
  1563. Next i
  1564. Close #iFile
  1565. _OutputToHTML = True
  1566. Exit_Function:
  1567. Exit Function
  1568. Error_Function:
  1569. _OutputToHTML = False
  1570. GoTo Exit_Function
  1571. End Function &apos; _OutputToHTML V1.4.0
  1572. REM -----------------------------------------------------------------------------------------------------------------------
  1573. Private Function _PropertiesList() As Variant
  1574. _PropertiesList = Array(&quot;Connect&quot;, &quot;Name&quot;, &quot;ObjectType&quot; _
  1575. , &quot;OnCreate&quot;, &quot;OnFocus&quot;, &quot;OnLoad&quot;, &quot;OnLoadFinished&quot;, &quot;OnModifyChanged&quot; _
  1576. , &quot;OnNew&quot;, &quot;OnPrepareUnload&quot;, &quot;OnPrepareViewClosing&quot;, &quot;OnSave&quot;, &quot;OnSaveAs&quot; _
  1577. , &quot;OnSaveAsDone&quot;, &quot;OnSaveAsFailed&quot;, &quot;OnSaveDone&quot;, &quot;OnSaveFailed&quot;, &quot;OnSaveTo&quot; _
  1578. , &quot;OnSaveToDone&quot;, &quot;OnSaveToFailed&quot;, &quot;OnSubComponentClosed&quot;, &quot;OnSubComponentOpened&quot; _
  1579. , &quot;OnTitleChanged&quot;, &quot;OnUnfocus&quot;, &quot;OnUnload&quot;, &quot;OnViewClosed&quot;, &quot;OnViewCreated&quot; _
  1580. , &quot;Version&quot; _
  1581. )
  1582. End Function &apos; _PropertiesList
  1583. REM -----------------------------------------------------------------------------------------------------------------------
  1584. Private Function _PropertyGet(ByVal psProperty As String) As Variant
  1585. &apos; Return property value of the psProperty property name
  1586. Dim i As Integer, vEvents As Variant, sEvent As String, vEvent As Variant
  1587. If _ErrorHandler() Then On Local Error Goto Error_Function
  1588. Utils._SetCalledSub(&quot;Database.get&quot; &amp; psProperty)
  1589. _PropertyGet = EMPTY
  1590. Select Case UCase(psProperty)
  1591. Case UCase(&quot;Connect&quot;)
  1592. If IsNull(Document) Then _PropertyGet = &quot;&quot; Else _PropertyGet = Document.Datasource.URL
  1593. &apos; Location = ConvertFromUrl(URL)
  1594. Case UCase(&quot;Name&quot;)
  1595. _PropertyGet = Title
  1596. Case UCase(&quot;ObjectType&quot;)
  1597. _PropertyGet = _Type
  1598. Case UCase(&quot;OnCreate&quot;), UCase(&quot;OnFocus&quot;), UCase(&quot;OnLoad&quot;), UCase(&quot;OnLoadFinished&quot;), UCase(&quot;OnModifyChanged&quot;) _
  1599. , UCase(&quot;OnNew&quot;), UCase(&quot;OnPrepareUnload&quot;), UCase(&quot;OnPrepareViewClosing&quot;), UCase(&quot;OnSave&quot;), UCase(&quot;OnSaveAs&quot;) _
  1600. , UCase(&quot;OnSaveAsDone&quot;), UCase(&quot;OnSaveAsFailed&quot;), UCase(&quot;OnSaveDone&quot;), UCase(&quot;OnSaveFailed&quot;), UCase(&quot;OnSaveTo&quot;) _
  1601. , UCase(&quot;OnSaveToDone&quot;), UCase(&quot;OnSaveToFailed&quot;), UCase(&quot;OnSubComponentClosed&quot;), UCase(&quot;OnSubComponentOpened&quot;) _
  1602. , UCase(&quot;OnTitleChanged&quot;), UCase(&quot;OnUnfocus&quot;), UCase(&quot;OnUnload&quot;), UCase(&quot;OnViewClosed&quot;), UCase(&quot;OnViewCreated&quot;)
  1603. &apos; Find script event
  1604. sEvent = &quot;&quot;
  1605. If IsNull(Document) Then vEvents = Array() Else vEvents = Document.getEvents().ElementNames &apos; Returns an array
  1606. For i = 0 To UBound(vEvents)
  1607. If UCase(vEvents(i)) = UCase(psProperty) Then
  1608. sEvent = vEvents(i)
  1609. Exit For
  1610. End If
  1611. Next i
  1612. If sEvent = &quot;&quot; Then
  1613. _PropertyGet = &quot;&quot;
  1614. Else
  1615. vEvent = Document.getEvents().getByName(sEvent)
  1616. If IsEmpty(vEvent) Then
  1617. _PropertyGet = &quot;&quot;
  1618. ElseIf vEvent(0).Value &lt;&gt; &quot;Script&quot; Then
  1619. _PropertyGet = &quot;&quot;
  1620. Else
  1621. _PropertyGet = vEvent(1).Value
  1622. End If
  1623. End If
  1624. Case UCase(&quot;Version&quot;)
  1625. _PropertyGet = MetaData.getDatabaseProductName() &amp; &quot; &quot; &amp; MetaData.getDatabaseProductVersion
  1626. Case Else
  1627. Goto Trace_Error
  1628. End Select
  1629. Exit_Function:
  1630. Utils._ResetCalledSub(&quot;Database.get&quot; &amp; psProperty)
  1631. Exit Function
  1632. Trace_Error:
  1633. TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(), 0, , psProperty)
  1634. _PropertyGet = EMPTY
  1635. Goto Exit_Function
  1636. Error_Function:
  1637. TraceError(TRACEABORT, Err, &quot;Database._PropertyGet&quot;, Erl)
  1638. _PropertyGet = EMPTY
  1639. GoTo Exit_Function
  1640. End Function &apos; _PropertyGet
  1641. REM -----------------------------------------------------------------------------------------------------------------------
  1642. Public Function _ReplaceSquareBrackets(ByVal psSql As String) As String
  1643. &apos; Returns psSql after substitution of [] by quote character
  1644. &apos; [] square brackets in (single) quoted strings not affected
  1645. Dim sQuote As String &apos;RDBMS specific quote character
  1646. Dim vSubStrings() As Variant, i As Integer
  1647. Const cstSingleQuote = &quot;&apos;&quot;
  1648. sQuote = MetaData.IdentifierQuoteString
  1649. If sQuote = &quot; &quot; Then &apos; IdentifierQuoteString returns a space &quot; &quot; if identifier quoting is not supported.
  1650. _ReplaceSquareBrackets = Trim(psSql)
  1651. Exit Function
  1652. End If
  1653. vSubStrings() = Split(psSql, cstSingleQuote)
  1654. For i = 0 To UBound(vSubStrings)
  1655. If (i Mod 2) = 0 Or (i = UBound(vSubStrings)) Then &apos; Only even substrings are parsed for square brackets. Last substring is parsed anyway
  1656. vSubStrings(i) = Join(Split(vSubStrings(i), &quot;[&quot;), sQuote)
  1657. vSubStrings(i) = Join(Split(vSubStrings(i), &quot;]&quot;), sQuote)
  1658. End If
  1659. Next i
  1660. _ReplaceSquareBrackets = Trim(Join(vSubStrings, cstSingleQuote))
  1661. End Function &apos; ReplaceSquareBrackets V1.1.0
  1662. </script:module>