Parsing delimited text files
When reading data from a text file, you may find that it is in a format that needs
seperating which could be Comma Seperated file which could use ',' as it's delimiter. To
be able to seperate the different records you can create a function to parse the files
that you read into your program. I have released a OCX Control on my web site which does
this for you without any code. Here Goes 1. Start a new
Standard-EXE project, form1 is created by default 2. Add a module to your
project. Project, Add Module. The code window is displayed by default. Add the following
code : Public Function ParseString(ByVal vsString As _
String, ByVal vsDelimiter As String, ByVal _
viNumber As Integer)
'Author: Steve Anderson
'Created : 13/08/98
'Purpose : Parses out a section
'from a delimited string
Dim iFoundat As Integer
Dim iFoundatold As Integer
Dim iCurrentSection As Integer
Dim sText As String
If Len(vsString) > 0 And InStr(vsString, _
vsDelimiter) > 0 And viNumber > 0 Then
iFoundat = 1
iFoundatold = 1
Do While InStr(iFoundatold + 1, vsString, _
vsDelimiter) > 0
iFoundatold = iFoundat
iFoundat = InStr(iFoundat + 1, vsString, _
vsDelimiter)
iCurrentSection = iCurrentSection + 1
Loop
If viNumber > iCurrentSection Then
Exit Function
End If
iFoundat = 1
iCurrentSection = 0
Do
iFoundatold = iFoundat
iFoundat = InStr(iFoundat + 1, vsString, _
vsDelimiter)
If Trim(sText) = "" Then
sText = Mid(vsString, 1, iFoundat - 1)
iCurrentSection = iCurrentSection + 1
Else
If iFoundat > 0 Then
sText = Mid(vsString, iFoundatold +
1, _
(iFoundat - 1) - iFoundatold)
Else
sText = Mid(vsString, iFoundatold +
1)
End If
iCurrentSection = iCurrentSection + 1
End If
If iCurrentSection = viNumber Then
ParseString = sText
Exit Do
End If
Loop
End If
ParseString = sText
End Function
3. Open up
form1's and add a command button. Open up the click even for
the command button and type the following:
Msgbox ParseString("Visit - allapi.mentalis.org -
for everything on Visual Basic", "-", 2)
4. Run the
project (F5) and click on the command button and
'www.microweird.demon.co.uk' will be displayed in a message
box.
|