VCS_Relation.bas 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. On Error GoTo err
  6. Dim fso As Object
  7. Dim OutFile As Object
  8. Dim relname As String
  9. relname = rel.name
  10. Set fso = CreateObject("Scripting.FileSystemObject")
  11. Set OutFile = fso.CreateTextFile(filePath, overwrite:=True, unicode:=False)
  12. OutFile.WriteLine rel.Attributes 'RelationAttributeEnum
  13. OutFile.WriteLine rel.name
  14. OutFile.WriteLine rel.table
  15. OutFile.WriteLine rel.foreignTable
  16. Dim f As DAO.field
  17. For Each f In rel.Fields
  18. OutFile.WriteLine "Field = Begin"
  19. OutFile.WriteLine f.name
  20. OutFile.WriteLine f.ForeignName
  21. OutFile.WriteLine "End"
  22. Next
  23. OutFile.Close
  24. logger "ExportRelation", "DEBUG", "Relation " & relname & " exported to " & filePath
  25. Exit Sub
  26. err:
  27. logger "ImportRelation", "ERROR", "Unable to import relation " & relname & " [" & err.Description & "]"
  28. End Sub
  29. Public Sub ImportRelation(ByVal filePath As String)
  30. On Error GoTo err
  31. Dim fso As Object
  32. Dim InFile As Object
  33. Set fso = CreateObject("Scripting.FileSystemObject")
  34. Set InFile = fso.OpenTextFile(filePath, iomode:=ForReading, Create:=False, Format:=TristateFalse)
  35. Dim rel As DAO.Relation
  36. Set rel = New DAO.Relation
  37. Dim relname As String
  38. relname = rel.name
  39. rel.Attributes = InFile.readline
  40. rel.name = InFile.readline
  41. rel.table = InFile.readline
  42. rel.foreignTable = InFile.readline
  43. Dim f As DAO.field
  44. Do Until InFile.AtEndOfStream
  45. If "Field = Begin" = InFile.readline Then
  46. Set f = New DAO.field
  47. f.name = InFile.readline
  48. f.ForeignName = InFile.readline
  49. If "End" <> InFile.readline Then
  50. Set f = Nothing
  51. logger "ImportRelation", "ERROR", "Missing 'End' for a 'Begin' in " & filePath
  52. GoTo next_rel
  53. End If
  54. rel.Fields.Append f
  55. End If
  56. next_rel:
  57. Loop
  58. InFile.Close
  59. CurrentDb.Relations.Append rel
  60. logger "ImportRelation", "DEBUG", "Relation " & relname & " imported from " & filePath
  61. Exit Sub
  62. err:
  63. logger "ImportRelation", "ERROR", "Unable to import relation " & relname & " [" & err.Description & "]"
  64. End Sub