OA_Properties.bas 5.2 KB

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