Export và Import ra tập tin text từ Access (VB)
Hiện nay các bạn yêu thích lập trình sử dụng Access là nguồn chứa dữ liệu khá phổ biến vì đơn giản,
dễ quản trị và đáp ứng được yêu cầu công việc. Hôm nay chúng tôi xin giới thiệu một đoạn code để
export và import ra tập tin text từ Access (VB)
Export Text (Flat file) từ Access Ms-Access
Option Explicit
Public Sub Export_Table_2_TextFile()
On Error GoTo LocalErrorHandler
Dim dbCompany As Database
Dim rsGeneral As Recordset
Dim ExpGeneral As PubExpGeneral
Dim blnTab_Text As Boolean
Dim FullName As String
Dim FileHandle As Byte
Dim strFileToExport As String
Dim chkFileExist As String
'Give Path with File name
FullName = E:\General ' Thu muc chua du lieu, ban co the thay doi theo nhu cau của minh
blnTab_Text = False
Set dbCompany = OpenDatabase(FullName)
'Ví dụ tên bang la Company
Set rsGeneral = dbCompany.OpenRecordset(Company, dbOpenTable)
With ExpGeneral
.EmpNumber = No.
.EmpName = Name
.EmpAddress = Address
.EmpCity = City
Sử dụng TAB hoăc dấu phẩy
If blnTab_Text Then
.Delimiter1 = Chr(9)
.Delimiter2 = Chr(9)
.Delimiter3 = Chr(9)
Else
.Delimiter1 = Chr(44)
.Delimiter2 = Chr(44)
.Delimiter3 = Chr(44)
End If
.CRLF = vbCrLf
End With
FileHandle = FreeFile
'Tên tập tin
strFileToExport = C:\Exported.txt
chkFileExist = Dir(strFileToExport)
If chkFileExist <> Then
Kill strFileToExport
End If
Open strFileToExport For Random As FileHandle Len = Len(ExpGeneral)
Put FileHandle, , ExpGeneral
Do Until rsGeneral.EOF
With ExpGeneral
.EmpNumber = rsGeneral(EmpNo)
.EmpName = rsGeneral(EmpName)
.EmpAddress = rsGeneral(EmpAddress)
.EmpCity = rsGeneral(EmpCity)
End With
Put FileHandle, , ExpGeneral
rsGeneral.MoveNext
Loop
rsGeneral.Close
Set rsGeneral = Nothing
Close FileHandle
Exit Sub
LocalErrorHandler:
MsgBox Error Occured : & Err.Description, , Error
End Sub
'Import Text vào Ms-Access
Public Sub Import_TextFile_2_Table()
On Error GoTo LocalErrorHandler
Dim dbCompany As Database
Dim rsGeneral As Recordset
Dim FullName As String
Dim FileHandle As Byte
Dim ImportRecord As String
Dim flnName As String
Dim RowPosition As Double
Dim EmpNumber As String
Dim EmpName As String
Dim EmpAddress As String
Dim EmpCity As String
Dim Delimiter As String
flnName = C:\Exported.txt
Delimiter = ,
FileHandle = FreeFile
Open flnName For Input As FileHandle
Line Input #FileHandle, ImportRecord
FullName = C:\General
Set dbCompany = OpenDatabase(FullName)
Set rsGeneral = dbCompany.OpenRecordset(Company, dbOpenDynaset)
Do Until EOF(FileHandle)
Line Input #FileHandle, ImportRecord
RowPosition = RowPosition + 1
EmpNumber = Trim(Mid(ImportRecord, 1, InStr(1, ImportRecord, Delimiter, 1) - 1))
EmpName = Trim(Mid(ImportRecord, 7, 10))
EmpAddress = Trim(Mid(ImportRecord, 18, 30))
EmpCity = Trim(Mid(ImportRecord, 49))
rsGeneral.AddNew
rsGeneral(EmpNo) = EmpNumber
rsGeneral(EmpName) = EmpName
rsGeneral(EmpAddress) = EmpAddress
rsGeneral(EmpCity) = EmpCity
rsGeneral.Update
Loop
Close FileHandle
rsGeneral.Close
Set rsGeneral = Nothing
dbCompany.Close
Set dbCompany = Nothing
Exit Sub
LocalErrorHandler:
MsgBox Error Occured : & Err.Description, , Error
End Sub