Option Compare Database

Option Private Module
Option Explicit


Public Sub ExportRelation(ByVal rel As DAO.Relation, ByVal filepath As String)
    Dim FSO As Object
    Dim OutFile As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Set OutFile = FSO.CreateTextFile(filepath, overwrite:=True, unicode:=False)
    
    OutFile.WriteLine rel.Attributes 'RelationAttributeEnum
    OutFile.WriteLine rel.name
    OutFile.WriteLine rel.table
    OutFile.WriteLine rel.foreignTable
    
    Dim f As DAO.Field
    For Each f In rel.Fields
        OutFile.WriteLine "Field = Begin"
        OutFile.WriteLine f.name
        OutFile.WriteLine f.ForeignName
        OutFile.WriteLine "End"
    Next
    
    OutFile.Close

    logger "ExportRelation", "DEBUG", "Relation " & rel.name & " exported to " & filepath

End Sub

Public Sub ImportRelation(ByVal filepath As String)
    Dim FSO As Object
    Dim InFile As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set InFile = FSO.OpenTextFile(filepath, iomode:=ForReading, create:=False, Format:=TristateFalse)
    Dim rel As DAO.Relation
    Set rel = New DAO.Relation
    
    rel.Attributes = InFile.readline
    rel.name = InFile.readline
    rel.table = InFile.readline
    rel.foreignTable = InFile.readline
    
    Dim f As DAO.Field
    Do Until InFile.AtEndOfStream
        If "Field = Begin" = InFile.readline Then
            Set f = New DAO.Field
            f.name = InFile.readline
            f.ForeignName = InFile.readline
            If "End" <> InFile.readline Then
                Set f = Nothing
                logger "ImportRelation", "ERROR", "Missing 'End' for a 'Begin' in " & filepath
                GoTo next_rel
            End If
            rel.Fields.Append f
        End If
next_rel:
    Loop
    
    InFile.Close
    
    CurrentDb.Relations.Append rel

    logger "ImportRelation", "DEBUG", "Relation " & rel.name & " imported from " & filepath

End Sub