VCS_Relation.bas 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. Public Sub ExportRelation(ByVal rel As DAO.Relation, ByVal filepath As String)
  5. Dim FSO As Object
  6. Dim OutFile As Object
  7. Dim relname As String
  8. relname = rel.name
  9. Set FSO = CreateObject("Scripting.FileSystemObject")
  10. Set OutFile = FSO.CreateTextFile(filepath, overwrite:=True, unicode:=False)
  11. OutFile.WriteLine rel.Attributes 'RelationAttributeEnum
  12. OutFile.WriteLine rel.name
  13. OutFile.WriteLine rel.table
  14. OutFile.WriteLine rel.foreignTable
  15. Dim f As DAO.Field
  16. For Each f In rel.Fields
  17. OutFile.WriteLine "Field = Begin"
  18. OutFile.WriteLine f.name
  19. OutFile.WriteLine f.ForeignName
  20. OutFile.WriteLine "End"
  21. Next
  22. OutFile.Close
  23. logger "ExportRelation", "DEBUG", "Relation " & relname & " exported to " & filepath
  24. End Sub
  25. Public Sub ImportRelation(ByVal filepath As String)
  26. Dim FSO As Object
  27. Dim InFile As Object
  28. Set FSO = CreateObject("Scripting.FileSystemObject")
  29. Set InFile = FSO.OpenTextFile(filepath, iomode:=ForReading, Create:=False, Format:=TristateFalse)
  30. Dim rel As DAO.Relation
  31. Set rel = New DAO.Relation
  32. Dim relname As String
  33. relname = rel.name
  34. rel.Attributes = InFile.readline
  35. rel.name = InFile.readline
  36. rel.table = InFile.readline
  37. rel.foreignTable = InFile.readline
  38. Dim f As DAO.Field
  39. Do Until InFile.AtEndOfStream
  40. If "Field = Begin" = InFile.readline Then
  41. Set f = New DAO.Field
  42. f.name = InFile.readline
  43. f.ForeignName = InFile.readline
  44. If "End" <> InFile.readline Then
  45. Set f = Nothing
  46. logger "ImportRelation", "ERROR", "Missing 'End' for a 'Begin' in " & filepath
  47. GoTo next_rel
  48. End If
  49. rel.Fields.Append f
  50. End If
  51. next_rel:
  52. Loop
  53. InFile.Close
  54. CurrentDb.Relations.Append rel
  55. logger "ImportRelation", "DEBUG", "Relation " & relname & " imported from " & filepath
  56. End Sub