OA_Properties.bas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. '****
  5. '*
  6. '* Import/Export the properties of DAO Objects
  7. '*
  8. '****
  9. Public Sub ExportProperties(daoObject As Object, file_path As String)
  10. 'exports the properties of an Access DAO object (Database, Cintainer, TableDef, Querydef, Field)
  11. ' to a file, with a TAB separator
  12. '
  13. On Error GoTo err
  14. VCS_Dir.MkDirIfNotExist Left$(file_path, InStrRev(file_path, "\"))
  15. Dim prp As Object
  16. Dim prp_name, prp_value, Line As String
  17. Dim prp_type As Integer
  18. Dim count As Integer
  19. count = 0
  20. Call logger("ExportProperties", "INFO", "Try to export the properties of '" & daoObject.name & "' to '" & file_path & "'")
  21. Dim objStream As ADODB.Stream
  22. Set objStream = CreateObject("ADODB.Stream")
  23. objStream.Open
  24. objStream.Type = 2 'Text
  25. objStream.Charset = "x-ansi"
  26. For Each prp In daoObject.Properties
  27. If Not isReadOnly(prp) Then
  28. prp_name = prp.name
  29. prp_value = CStr(prp.value)
  30. Line = prp_name & vbTab & prp_value & vbTab & CStr(prp.Type)
  31. objStream.WriteText Line, adWriteLine
  32. count = count + 1
  33. End If
  34. Next prp
  35. Call logger("ExportProperties", "INFO", "> " & count & " properties exported")
  36. exitsub:
  37. objStream.SaveToFile file_path, adSaveCreateOverWrite
  38. objStream.Close
  39. Exit Sub
  40. err:
  41. Call logger("ExportProperties", "ERROR", "Error during database properties export: " & err.Description)
  42. GoTo exitsub
  43. 'err_prop:
  44. ' Call logger("ExportProperties", "ERROR", "Error while exporting property " & prp_name & "")
  45. ' GoTo next_prop
  46. End Sub
  47. Public Sub ImportProperties(daoObject As Object, file_path As String)
  48. 'Import the properties of an EXISTING Access DAO object (Database, Cintainer, TableDef, Querydef, Field)
  49. '! warning, it is not really stable
  50. On Error GoTo err
  51. Dim splitted_line As Variant
  52. Dim Line, prp_name, prp_value As String
  53. Dim prp_type As Integer
  54. Dim buffer As String
  55. Dim count As Integer
  56. Call logger("ImportProperties", "INFO", "Try to import the properties of '" & daoObject.name & "' from '" & file_path & "'")
  57. buffer = ReadFile(file_path, "x-ansi")
  58. count = 0
  59. For Each Line In Split(buffer, vbNewLine)
  60. If Len(Line) = 0 Then Exit For
  61. count = count + 1
  62. splitted_line = Split(Line, vbTab)
  63. prp_name = splitted_line(0)
  64. prp_value = splitted_line(1)
  65. prp_type = CInt(splitted_line(2))
  66. If prp_value <> vbNullString Then
  67. Call SetOrCreateProperty(daoObject, prp_name, prp_value, prp_type, True)
  68. End If
  69. Next
  70. Call logger("ImportProperties", "INFO", "> Done")
  71. Exit Sub
  72. err:
  73. Call logger("ImportProperties", "ERROR", "Error during database properties import (line " & count & "): " & err.Description)
  74. End Sub
  75. Sub SetOrCreateProperty(ByRef daoObject As Object, ByVal prp_name As String, ByVal prp_value As String, ByVal prp_type As Integer, Optional ByVal IgnoreReadOnly As Boolean = False)
  76. Dim prp As Object
  77. On Error GoTo err
  78. prp = daoObject.Properties(prp_name)
  79. If prp.Inherited = True Then Exit Sub
  80. prp.value = convert(prp_value, prp_type)
  81. Exit Sub
  82. err:
  83. Select Case err.Number
  84. Case 3270
  85. ' property not found
  86. Call CreateProperty(daoObject, prp_name, prp_value, prp_type)
  87. Case 3421
  88. Call logger("SetOrCreateProperty", "ERROR", prp_name & " : Type is not compatible with property (" & prp_value & ", " & prp_type & ")")
  89. Case Else
  90. If Not IgnoreReadOnly Then
  91. Call logger("SetOrCreateProperty", "ERROR", prp_name & " : Property is read only")
  92. End If
  93. End Select
  94. End Sub
  95. Public Sub CreateProperty(ByRef daoObject As Object, ByVal prp_name As String, ByVal prp_value As String, ByVal prp_type As Integer)
  96. On Error GoTo err
  97. Dim prp As Object
  98. Set prp = daoObject.CreateProperty(prp_name, prp_type, prp_value)
  99. daoObject.Properties.Append prp
  100. Exit Sub
  101. err:
  102. Call logger("CreateProperty", "ERROR", prp_name & "> Unable to create the property (" & prp_value & ", " & prp_type & "): " & err.Description)
  103. End Sub
  104. Function convert(value As String, dbType As Integer)
  105. Select Case dbType
  106. Case dbBoolean
  107. convert = CBool(convert)
  108. Case dbByte
  109. convert = CByte(value)
  110. Case dbInteger
  111. convert = CInt(value)
  112. Case dbLong
  113. convert = CLng(value)
  114. Case dbCurrency
  115. convert = CCur(value)
  116. Case dbSingle
  117. convert = CSng(value)
  118. Case dbDouble
  119. convert = CDbl(value)
  120. Case dbDate
  121. convert = CDate(value)
  122. Case dbText
  123. convert = CStr(value)
  124. Case dbLongBinary
  125. convert = CLng(value)
  126. Case dbMemo
  127. convert = CStr(value)
  128. Case dbGUID
  129. convert = CStr(value)
  130. Case Else
  131. convert = CVar(value)
  132. End Select
  133. End Function
  134. Public Function isReadOnly(prp As DAO.Property) As Boolean
  135. On Error GoTo err
  136. If prp.Inherited = True Then GoTo err
  137. DAO.BeginTrans
  138. prp.value = prp.value
  139. isReadOnly = False
  140. DAO.Rollback
  141. Exit Function
  142. err:
  143. isReadOnly = True
  144. DAO.Rollback
  145. End Function