OA_Utils.bas 5.9 KB

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