OA_Utils.bas 5.1 KB

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