OA_Utils.bas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. Public Function oa_tbl_exists() As Boolean
  5. ' return True if the 'USysOpenAccess' table exists
  6. On Error GoTo err
  7. oa_tbl_exists = (CurrentDb.TableDefs("USysOpenAccess").name = "USysOpenAccess")
  8. Exit Function
  9. err:
  10. If err.Number = 3265 Then
  11. oa_tbl_exists = False
  12. Else
  13. OA_MsgBox "Error: " & err.Description, vbCritical
  14. End If
  15. End Function
  16. Public Function update_oa_param(ByVal key As String, ByVal val As String)
  17. ' create or update the parameter in USysOpenAccess
  18. If Not oa_tbl_exists() Then
  19. Call create_oa_tbl
  20. End If
  21. If DCount("key", "USysOpenAccess", "[key]='" & key & "'") = 1 Then
  22. ' CurrentDb.Execute "UPDATE USysOpenAccess SET USysOpenAccess.val = '" & val & "' " & _
  23. ' "WHERE (((USysOpenAccess.key)='" & key & "'));"
  24. run_sql "UPDATE USysOpenAccess SET USysOpenAccess.val = '" & val & "' " & _
  25. "WHERE (((USysOpenAccess.key)='" & key & "'));"
  26. Else
  27. ' CurrentDb.Execute "INSERT INTO USysOpenAccess ( val, [key] ) " & _
  28. ' "SELECT '" & val & "' AS Expr1, '" & key & "' AS Expr2;"
  29. run_sql "INSERT INTO USysOpenAccess ( val, [key] ) " & _
  30. "SELECT '" & val & "' AS Expr1, '" & key & "' AS Expr2;"
  31. End If
  32. End Function
  33. Public Function create_oa_tbl()
  34. 'creates the 'USysOpenAccess' table and hide it
  35. 'CurrentDb.Execute "SELECT 'include_tables' as key, 'USysOpenAccess' as val INTO USysOpenAccess;"
  36. run_sql "SELECT 'include_tables' as key, 'USysOpenAccess' as val INTO USysOpenAccess;"
  37. Application.SetHiddenAttribute acTable, "USysOpenAccess", True
  38. End Function
  39. Public Function get_include_tables()
  40. get_include_tables = oa_param("include_tables")
  41. End Function
  42. Public Function oa_param(ByVal key As String, Optional ByVal default_value As String = "") As String
  43. oa_param = default_value
  44. On Error GoTo err_oa_table
  45. oa_param = DFirst("val", "USysOpenAccess", "[key]='" & key & "'")
  46. err_oa_table:
  47. End Function
  48. Public Function IsInArray(ByVal stringToBeFound As String, ByRef arr As Variant) As Boolean
  49. ' returns True if the string is in the array
  50. IsInArray = (UBound(filter(arr, stringToBeFound)) > -1)
  51. End Function
  52. Public Function msys_type_filter(acType) As String
  53. 'returns a sql filter string for the object type
  54. 'NB: do not return system tables
  55. 'NB2: here are the types in msysobjects table:
  56. '-32768 = Form
  57. '-32766 = Macro
  58. '-32764 = Report
  59. '32761 = Module
  60. '-32758 Users
  61. '-32757 Database Document
  62. '-32756 Data Access Pages
  63. '1 Table - Local Access Tables
  64. '2 Access Object - Database
  65. '3 Access Object - Containers
  66. '4 Table - Linked ODBC Tables
  67. '5 Queries
  68. '6 Table - Linked Access Tables
  69. '8 SubDataSheets
  70. Select Case acType
  71. Case acTable
  72. msys_type_filter = "(([Type]=1 or [Type]=4 or [Type]=6) AND ([name] Not Like 'MSys*' AND [name] Not Like 'f_*_Data'))"
  73. Case acQuery
  74. msys_type_filter = "[Type]=5"
  75. Case acForm
  76. msys_type_filter = "[Type]=-32768"
  77. Case acReport
  78. msys_type_filter = "[Type]=-32764"
  79. Case acModule
  80. msys_type_filter = "[Type]=-32761"
  81. Case acMacro
  82. msys_type_filter = "[Type]=-32766"
  83. Case Else
  84. GoTo typerror
  85. End Select
  86. Exit Function
  87. typerror:
  88. OA_MsgBox "typerror:" & acType & " is not a valid object type"
  89. msys_type_filter = ""
  90. End Function
  91. Public Function remove_ext(ByVal filename As String) As String
  92. ' removes the extension of a file name
  93. If Not InStr(filename, ".") > 0 Then
  94. remove_ext = filename
  95. Exit Function
  96. End If
  97. Dim splitted_name As Variant
  98. splitted_name = Split(filename, ".")
  99. Dim i As Integer
  100. remove_ext = ""
  101. For i = 0 To (UBound(Split(filename, ".")) - 1)
  102. If Len(remove_ext) > 0 Then remove_ext = remove_ext & "."
  103. remove_ext = remove_ext & Split(filename, ".")(i)
  104. Next i
  105. End Function
  106. Public Function complete_gitignore()
  107. ' creates or complete the .gitignore file of the repo
  108. Dim gitignore_path, str_existing_keys, str As String
  109. Dim key As Variant
  110. Dim keys() As String
  111. keys = Split("*.accdb;*.laccdb;*.mdb;*.ldb;*.accde;*.mde;*.accda", ";")
  112. gitignore_path = CurrentProject.path & "\.gitignore"
  113. Dim fso As Object
  114. Set fso = CreateObject("Scripting.FileSystemObject")
  115. Dim oFile As Object
  116. If Not fso.FileExists(gitignore_path) Then
  117. Set oFile = fso.CreateTextFile(gitignore_path)
  118. Else
  119. Set oFile = fso.OpenTextFile(gitignore_path, ForReading)
  120. str_existing_keys = ""
  121. While Not oFile.AtEndOfStream
  122. str = oFile.readline
  123. If Len(str_existing_keys) = 0 Then
  124. str_existing_keys = str
  125. Else
  126. str_existing_keys = str_existing_keys & ";" & str
  127. End If
  128. Wend
  129. oFile.Close
  130. Dim existing_keys() As String
  131. existing_keys = Split(str_existing_keys, ";")
  132. Set oFile = fso.OpenTextFile(gitignore_path, ForAppending)
  133. End If
  134. oFile.WriteBlankLines (2)
  135. oFile.WriteLine ("#[ automatically added by OpenAccess")
  136. For Each key In keys
  137. If Not IsInArray(key, existing_keys) Then
  138. oFile.WriteLine key
  139. End If
  140. Next key
  141. oFile.WriteLine "#]"
  142. oFile.WriteBlankLines (2)
  143. oFile.Close
  144. Set fso = Nothing
  145. Set oFile = Nothing
  146. End Function
  147. Public Sub SaveProject()
  148. On Error Resume Next
  149. CurrentProject.Application.RunCommand acCmdSave
  150. Exit Sub
  151. End Sub
  152. Public Sub run_sql(ByVal sql As String)
  153. On Error GoTo next_try
  154. CurrentDb.Execute sql
  155. Exit Sub
  156. next_try:
  157. err.Clear
  158. On Error GoTo err
  159. Dim cnn As New ADODB.Connection
  160. Set cnn = CurrentProject.Connection
  161. cnn.Execute (sql)
  162. Set cnn = Nothing
  163. Exit Sub
  164. err:
  165. logger "run_sql", "CRITICAL", "Error while running the SQL statement: " & sql
  166. End Sub