Import and export of files
Excel
' Excel Import
Private Sub Btninexcel_click ()
Dim Strselectfile as String
With Application.filedialog (3)
. AllowMultiSelect = False
. InitialFileName = ""
. Filters.clear
. Filters.add "Excel File", "*.xls"
If. Show =-1 Then
Strselectfile =. Selecteditems.item (1)
Else
Exit Sub
End If
End with
Docmd.transferspreadsheet 0, 8, "Test table", Strselectfile, True
MsgBox "Import succeeded! "
Docmd.opentable "Test Sheet"
End Sub
' Excel Export
Private Sub Btnoutexcel_click ()
Dim Strselectfile as String
With Application.filedialog (2)
. AllowMultiSelect = False
. InitialFileName = "Test.xls"
If. Show =-1 Then
Strselectfile =. Selecteditems.item (1)
Else
Exit Sub
End If
End with
Docmd.transferspreadsheet 1, 8, "Test table", Strselectfile, True
MsgBox "Export success! "
ShellEx Strselectfile
End Sub
Csv
' Import CSV
Private Sub Btnindocmd_click ()
Dim Strselectfile as String
With Application.filedialog (3)
. AllowMultiSelect = False
. InitialFileName = ""
. Filters.clear
. Filters.add "CSV file", "*. CSV "
If. Show =-1 Then
Strselectfile =. Selecteditems.item (1)
Else
Exit Sub
End If
End with
Docmd.transfertext Acimportdelim,, "Test sheet", Strselectfile, True
MsgBox "Import succeeded! "
Docmd.opentable "Test Sheet"
End Sub
' Export CSV
Private Sub Btnoutdocmd_click ()
Dim Strselectfile as String
With Application.filedialog (2)
. AllowMultiSelect = False
. InitialFileName = "Test.csv"
If. Show =-1 Then
Strselectfile =. Selecteditems.item (1)
Else
Exit Sub
End If
End with
Docmd.transfertext Acexportdelim,, "Test sheet", Strselectfile, True
MsgBox "Export success! "
ShellEx Strselectfile
End Sub
Txt
' Export txt
Private Sub Btnouttxt_click ()
Dim Strselectfile as String
With Application.filedialog (2)
. AllowMultiSelect = False
. InitialFileName = "Test.txt"
If. Show =-1 Then
Strselectfile =. Selecteditems.item (1)
Else
Exit Sub
End If
End with
Docmd.transfertext Acexportdelim,, "Test sheet", Strselectfile, True
MsgBox "Export success! "
ShellEx Strselectfile
End Sub
Create txt and write content
Need to refer to Microsoft Script Runtime
Dim FSO as New FileSystemObject
Fso. CreateTextFile (Currentproject.path & "\test.txt")
Fso. OpenTextFile (Currentproject.path & "\test.txt", ForWriting). WriteLine "test Data"
Append new data to old data
Fso. OpenTextFile (Currentproject.path & "\test.txt", ForAppending). WriteLine "test Data"
Knowledge learned from Access-VBA development (II.)