Tác giả: truongphu
Cấp độ bài viết: Căn bản
Tóm tắt: Hướng dẫn ADODB đọc file text
1- Mặc định đọc text file với Delimited là dấu ,
2- ADODB.Connect khai đến folder; (không khai đến file)
Using vb Syntax Highlighting
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\" & _
";Extended Properties=""text;HDR=YES;FMT=Delimited"""
"Data Source=" & App.Path & "\" & _
";Extended Properties=""text;HDR=YES;FMT=Delimited"""
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
3- ADODB.Recordset khai tên file thay cho table:
Using vb Syntax Highlighting
Rs.Open "SELECT * FROM Danhsách1.txt", _
Cn, adOpenStatic, adLockOptimistic, adCmdText
Cn, adOpenStatic, adLockOptimistic, adCmdText
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
4- Ta có thể dùng các Format Delimited khác mặc định, vd: dấu Tab, dấu ; dấu | dấu \
dấu * dấu/ ... hay Fixed-Length = độ dài record giống nhau trên một cột
5- Khai Format delimited với Registry:
5a- Khai dấu Tab, code như sau;
Using vb Syntax Highlighting
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & _
"\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Jet\4.0\Engines\Text"
strValueName = "Format"
strValue = "TabDelimited"
objReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & _
"\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Jet\4.0\Engines\Text"
strValueName = "Format"
strValue = "TabDelimited"
objReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
Parsed in 0.006 seconds, using GeSHi 1.0.8.4
5b- Khai dấu ,
Using vb Syntax Highlighting
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & _
"\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Jet\4.0\Engines\Text"
strValueName = "Format"
strValue = "CSVDelimited"
objReg.SetStringValue _
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & _
"\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Jet\4.0\Engines\Text"
strValueName = "Format"
strValue = "CSVDelimited"
objReg.SetStringValue _
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
5c- Khai dấu tùy ý; code như trên, strValue = "CSVDelimited" sửa lại là:
strValue = "Delimited (x)"
mà x là dấu đang dùng; vd dấu |
- Mã: Chọn tất cả
strValue = "Delimited (|)"
5d- Khai Fixed-Length
- Mã: Chọn tất cả
strValue = "Fixed-Length"
6- Khai Format delimited với file Schema.ini:
6a- Khai một file với dấu:
Đây là dạng khai Format delimited đơn giản nhất: Tại thư mục của Project (EXE), ta tạo một file có tên là Schema.ini, nội dung:
dòng 1: tên file txt trong []; vd: [MyLog.txt]
dòng 2: Format=TabDelimited
Nội dung file toàn bộ như sau, dùng cho Format=Tab
- Mã: Chọn tất cả
[MyLog.txt]
Format=TabDelimited
6b- Khai nhiều file với dấu khác nhau:
- Mã: Chọn tất cả
[File_1.txt]
Format=CSVDelimited
[File_2.txt]
Format=TabDelimited
[File_3.txt]
Format=Delimited(|)
trong đó, File_1.txt là dấu , File_2.txt là dấu Tab File_3.txt là dấu |
6c- Khai một file với fixed-length
- Mã: Chọn tất cả
[Test.txt]
Format=FixedLength
Col1=FirstName Text Width 7
Col2=LastName Text Width 10
Col3=ID Text Integer 3
trong đó cột 1 là text dài 7 bytes, cột 2 text 10 bytes và cột 3 Integer với 3 bytes text

