Knowledge learned from Access-VBA development (II.)

Source: Internet
Author: User

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.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.