1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| Sub ExportTables()
Dim strPath As String
Dim db As DAO.Database, rTbls As DAO.Recordset, r As DAO.Recordset
Dim strDateFmt As String, strQuote As String, strDelimiter As String
Dim i As Integer, strField As String, valField As Variant
Dim strLine As String
strPath = "E:\Temp\" ' Chemin sortie
strQuote = """" ' caractère pour encadrer les données Texte
strDateFmt = "YYYYMMDD" ' Format Date
strDelimiter = ";" ' Délimiteur
Set db = CurrentDb
Set rTbls = db.OpenRecordset("SELECT * FROM MsysObjects WHERE Type=1 AND NOT (Name Like 'MSys*')")
Do While Not rTbls.EOF
Set r = db.OpenRecordset("SELECT * FROM [" & rTbls!Name & "]")
Open strPath & rTbls!Name & ".txt" For Output As #1
' Colonnes
strLine = ""
For i = 0 To r.Fields.Count - 1
strField = r.Fields(i).Name
Select Case strField
' Champs exclus
Case "s_GUID", "s_Generation", "s_Lineage"
Case Else
If strLine <> "" Then strLine = strLine & strDelimiter
strLine = strLine & strQuote & strField & strQuote
End Select
Next
Print #1, strLine
' données
Do While Not r.EOF
strLine = ""
For i = 0 To r.Fields.Count - 1
strField = r.Fields(i).Name
Select Case strField
' Champs exclus
Case "s_GUID", "s_Generation", "s_Lineage"
Case Else
valField = r.Fields(i)
If strLine <> "" Then strLine = strLine & strDelimiter
Select Case VarType(valField)
Case vbNull, vbEmpty
Case vbInteger, vbLong, vbSingle, vbDouble, _
vbCurrency, vbDecimal, vbByte
strLine = strLine & Str(valField)
Case vbDate
strLine = strLine & Format(valField, strDateFmt)
Case vbString
strLine = strLine & strQuote & valField & strQuote
Case vbBoolean
strLine = strLine & Str(CLng(valField))
End Select
End Select
Next
Print #1, strLine
' enrgistrement suivant
r.MoveNext
Loop
Close #1
' Table suivante
rTbls.MoveNext
Loop
End Sub |
Partager