VCS_Relation.bas 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. Attribute VB_Name = "VCS_Relation"
  2. Option Compare Database
  3. Option Private Module
  4. Option Explicit
  5. Public Sub ExportRelation(ByVal rel As DAO.Relation, ByVal filePath As String)
  6. Dim FSO As Object
  7. Dim OutFile As Object
  8. Set FSO = CreateObject("Scripting.FileSystemObject")
  9. Set OutFile = FSO.CreateTextFile(filePath, overwrite:=True, Unicode:=False)
  10. OutFile.WriteLine rel.Attributes 'RelationAttributeEnum
  11. OutFile.WriteLine rel.name
  12. OutFile.WriteLine rel.table
  13. OutFile.WriteLine rel.foreignTable
  14. Dim f As DAO.Field
  15. For Each f In rel.Fields
  16. OutFile.WriteLine "Field = Begin"
  17. OutFile.WriteLine f.name
  18. OutFile.WriteLine f.ForeignName
  19. OutFile.WriteLine "End"
  20. Next
  21. OutFile.Close
  22. End Sub
  23. Public Sub ImportRelation(ByVal filePath As String)
  24. Dim FSO As Object
  25. Dim InFile As Object
  26. Set FSO = CreateObject("Scripting.FileSystemObject")
  27. Set InFile = FSO.OpenTextFile(filePath, iomode:=ForReading, create:=False, Format:=TristateFalse)
  28. Dim rel As DAO.Relation
  29. Set rel = New DAO.Relation
  30. rel.Attributes = InFile.ReadLine
  31. rel.name = InFile.ReadLine
  32. rel.table = InFile.ReadLine
  33. rel.foreignTable = InFile.ReadLine
  34. Dim f As DAO.Field
  35. Do Until InFile.AtEndOfStream
  36. If "Field = Begin" = InFile.ReadLine Then
  37. Set f = New DAO.Field
  38. f.name = InFile.ReadLine
  39. f.ForeignName = InFile.ReadLine
  40. If "End" <> InFile.ReadLine Then
  41. Set f = Nothing
  42. Err.Raise 40000, "ImportRelation", "Missing 'End' for a 'Begin' in " & filePath
  43. End If
  44. rel.Fields.Append f
  45. End If
  46. Loop
  47. InFile.Close
  48. CurrentDb.Relations.Append rel
  49. End Sub