VCS_Table.bas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. Option Compare Database
  2. Option Private Module
  3. Option Explicit
  4. Public Sub ExportLinkedTable(ByVal tbl_name As String, ByVal dirpath As String)
  5. On Error GoTo Err_LinkedTable
  6. Dim tempFilePath As String
  7. tempFilePath = VCS_File.TempFile()
  8. Dim FSO As Object
  9. Dim OutFile As Object
  10. Set FSO = CreateObject("Scripting.FileSystemObject")
  11. ' open file for writing with Create=True, Unicode=True (USC-2 Little Endian format)
  12. Set OutFile = FSO.CreateTextFile(tempFilePath, overwrite:=True, unicode:=True)
  13. OutFile.Write CurrentDb.TableDefs(tbl_name).name
  14. OutFile.Write vbCrLf
  15. If InStr(1, CurrentDb.TableDefs(tbl_name).connect, "DATABASE=" & CurrentProject.path) Then
  16. 'change to relatave path
  17. Dim connect() As String
  18. connect = Split(CurrentDb.TableDefs(tbl_name).connect, CurrentProject.path)
  19. OutFile.Write connect(0) & "." & connect(1)
  20. Else
  21. OutFile.Write CurrentDb.TableDefs(tbl_name).connect
  22. End If
  23. OutFile.Write vbCrLf
  24. OutFile.Write CurrentDb.TableDefs(tbl_name).SourceTableName
  25. OutFile.Write vbCrLf
  26. Dim db As DAO.Database
  27. Set db = CurrentDb
  28. Dim td As DAO.TableDef
  29. Set td = db.TableDefs(tbl_name)
  30. Dim idx As DAO.Index
  31. For Each idx In td.Indexes
  32. If idx.Primary Then
  33. OutFile.Write Right$(idx.Fields, Len(idx.Fields) - 1)
  34. OutFile.Write vbCrLf
  35. End If
  36. Next
  37. Err_LinkedTable_Fin:
  38. On Error Resume Next
  39. OutFile.Close
  40. 'save files as .odbc
  41. Dim path As String
  42. path = dirpath & to_filename(tbl_name) & ".LNKD"
  43. VCS_File.ConvertUcs2Utf8 tempFilePath, path
  44. Kill tempFilePath
  45. logger "ExportLinkedTable", "DEBUG", "LinkedTable " & tbl_name & " exported to " & path
  46. Exit Sub
  47. Err_LinkedTable:
  48. OutFile.Close
  49. logger "ExportLinkedTable", "ERROR", "Unable to export " & tbl_name & ": " & err.Description
  50. Resume Err_LinkedTable_Fin
  51. End Sub
  52. Public Sub ImportLinkedTable(ByVal tblName As String, ByVal obj_path As String)
  53. Dim db As DAO.Database
  54. Dim FSO As Object
  55. Dim InFile As Object
  56. Set db = CurrentDb
  57. Set FSO = CreateObject("Scripting.FileSystemObject")
  58. Dim tempFilePath As String
  59. tempFilePath = VCS_File.TempFile()
  60. ConvertUtf8Ucs2 obj_path & tblName & ".LNKD", tempFilePath
  61. ' open file for reading with Create=False, Unicode=True (USC-2 Little Endian format)
  62. Set InFile = FSO.OpenTextFile(tempFilePath, iomode:=ForReading, Create:=False, Format:=TristateTrue)
  63. On Error GoTo err_notable:
  64. DoCmd.DeleteObject acTable, tblName
  65. GoTo err_notable_fin
  66. err_notable:
  67. err.Clear
  68. Resume err_notable_fin
  69. err_notable_fin:
  70. On Error GoTo Err_CreateLinkedTable:
  71. Dim td As DAO.TableDef
  72. Set td = db.CreateTableDef(InFile.readline())
  73. Dim connect As String
  74. connect = InFile.readline()
  75. If InStr(1, connect, "DATABASE=.\") Then 'replace relative path with literal path
  76. connect = Replace(connect, "DATABASE=.\", "DATABASE=" & CurrentProject.path & "\")
  77. End If
  78. td.connect = connect
  79. td.SourceTableName = InFile.readline()
  80. db.TableDefs.Append td
  81. GoTo Err_CreateLinkedTable_Fin
  82. Err_CreateLinkedTable:
  83. logger "ImportLinkedTable", "ERROR", "Unable to import " & tblName & ": " & err.Description
  84. Resume Err_CreateLinkedTable_Fin
  85. Err_CreateLinkedTable_Fin:
  86. 'this will throw errors if a primary key already exists or the table is linked to an access database table
  87. 'will also error out if no pk is present
  88. On Error GoTo Err_LinkPK_Fin:
  89. Dim Fields As String
  90. Fields = InFile.readline()
  91. Dim field As Variant
  92. Dim sql As String
  93. sql = "CREATE INDEX __uniqueindex ON " & td.name & " ("
  94. For Each field In Split(Fields, ";+")
  95. sql = sql & "[" & field & "]" & ","
  96. Next
  97. 'remove extraneous comma
  98. sql = Left$(sql, Len(sql) - 1)
  99. sql = sql & ") WITH PRIMARY"
  100. 'CurrentDb.Execute sql
  101. run_sql sql
  102. logger "ImportLinkedTable", "DEBUG", "LinkedTable " & tblName & " improted from " & obj_path & tblName & ".LNKD"
  103. Err_LinkPK_Fin:
  104. On Error Resume Next
  105. InFile.Close
  106. Kill tempFilePath
  107. End Sub