Class dùng cho Fixed Length Text String
Có l lp trình vi Text String là công vic bn thường làm nht. Ðó là lý do ti sao
hơn mười năm trước Microsoft Basic ca Bill Gates rt thnh hành vì nó có nhng
String Functions rt tin dng như Left, Right và Mid.
Có nhiu Database programs export data records dưới dng Fixed Length (có chiu
dài c định) Text String. Khi mt Text String là gm rt nhiu Data fields concatenated
li vi nhau như thế vic ghi nh mi Data field bt đầu t character th my là c mt
vn đề.
Dưới đây là mt VB6 Class dùng cho Fixed Length Text String để ta extract nhng
SubStrings t nó mt cách d dàng và không b ln ln.
Gi s ta có nhng Text Strings như sau:
123456789012345678901234567890123456789012345678901234567890123456789012123
4567890121234567890123456789012345
Truong Tam Phong 25 Greenfield Rd Cabramatta, NSW 21669724 3645
9724 1324 ptruong@ozemail.com.au
Le Thanh Long 24/426 King St Fairfield, NSW 2165 9425 2534
9425 7755 llong@bigpond.com.au
Hàng th nht bên trên là đ dùng làm thước mu cho ta thy mi hàng Text gm có
nhiu Data fields nm kế tiếp nhau (concatenated). Ta có th đặt tên các Data fields
và xác định chiu dài (size) ca mi Data field như dưới đây:
Name,20
Address1,20
Address2,20
Phone,12
Fax,12
Email,25
Nếu viết program theo li thông thường ta s nói đến Data field Phone bng cách
dùng Function Mid, chng hn như Mid(TextString,61,12) , 61 là Offset ca Data
field Phone. Tương t như vy, nói đến Data field Email ta phi viết
Mid(TextString,85,25), mà 85 là Offset ca Data field Email.
Viết Code kiu ny rt d lm ln nếu ta dùng các Data fields ny thường. Do đó ta có
Class clsTextRecord. Sau khi instantiate mt Object ca clsTextRecord, ta ch cn
cho nó mt File cha cu trúc ca các Data fields như nhng hàng nói trên,
clsTextRecord s t động tính ra Offsets ca tt c các Data fields.
Program ny dùng Delimited String Class clsString để Tokenise nhng hàng text trong
File nói trên.
Dưới đây là Listing ca Property Let StructureFileName ca clsTextRecord :
Public Property Let StructureFileName(ByVal vData As String)
' Read the info about Datafields and their sizes to create a collection
of Datafields.
Dim FileNum, TextLine, FName
mvarStructureFileName = vData
myLength = 0
' Obtain an available File handle
FileNum = FreeFile
' Open the Structure file as Input
Open mvarStructureFileName For Input As FileNum
Offset = 1 ' Initialise the Offset
' Create a collection of Datafields
Set DataFields = New Collection
Do While Not EOF(FileNum)
' Read a line from the Structure file
Line Input #FileNum, TextLine
' Declare and instantiate an object of Delimited String class type
clsString
Dim aLine As New clsString
' Declare and instantiate an object of Class ClsSubString which has
' FieldName, Offset and Size
Dim SubString As New ClsSubString
' Tokenise the string into Fieldname and the size of the data field
aLine.Text = TextLine
aLine.Delimiter = ","
FName = aLine.TokenAt(1)
SubString.FieldName = FName ' assign FieldName
SubString.Size = Val(aLine.TokenAt(2)) ' assign field Size in
character
SubString.Offset = Offset ' assign offset of SubString in String
' Update Offset value for the next data field
Offset = Offset + SubString.Size
myLength = myLength + SubString.Size
' Add an SubString Object to the DataFields collection
DataFields.Add Item:=SubString, Key:=FName
Set SubString = Nothing
Loop
' Fill the internal String with Blank spaces
mvarTextStringData = Space(myLength)
Close FileNum ' Close input file
End Property
Khi bn chy program mu kèm theo, trước hết nó instantiate mt Object loi
clsTextRecord, load các Test Text Strings t file Persons.txt vào mt Listbox tên
lstInputString và đọc content ca File cha cu trúc ca các Data Fields tên
PersonRecordStructure.txt.
Listing ca Sub Form_load như sau:
Private Sub Form_Load()
' Instantiate an Object of ClsTextRecord
Set ALongString = New ClsTextRecord
' Build the Collection of Datafields
ALongString.StructureFileName = App.Path & "\PersonRecordStructure.txt"
' Load the test Strings into the ListBox
PopulateListBoxFromFile lstInputString, "Persons.txt", False
End Sub
Chn mt item trong lstInputString ri click nút CmdGetSubStrings. Program s
ngc khúc selected Text String và display các Data fields trong nhng TextBoxes bên
dưới. Nếu bn chn hàng đầu program s display cho thy mi Data field cha bao
nhiêu characters.
Ngoài ra, ta cũng có th dùng clsTextRecord để viết data trc tiếp vào mi Data field
trong Fixed Length Text String.
Lưu ý cách dùng tên các Data Fields trong listing ca Sub CmdGetSubStrings_Click()
như sau:
Private Sub CmdGetSubStrings_Click()
' assign the selected string in the listbox to the ALongString object
ALongString.TextStringData = lstInputString.Text
' Assign appropriate datafield data to the TextBoxes
txtName.Text = ALongString.FieldData("Name")
txtAddress1.Text = ALongString.FieldData("Address1")
txtAddress2.Text = ALongString.FieldData("Address2")
txtPhone.Text = ALongString.FieldData("Phone")
txtFax.Text = ALongString.FieldData("Fax")
txtEmail.Text = ALongString.FieldData("Email")
End Sub
Bn có th download source code ca program mu ny k c class clsTextRecord.