Solved

Reading Delimited Text Files

Posted on 1998-08-12
11
176 Views
Last Modified: 2010-04-30
Possible a difficult question so depending on depth of explanation, more point will be awarded.  Now the question.  I have some text delimited(comma's and double quotes) files so how do I read them without using the Input# function or how does Input# work?  Essentially, I have a situation where I cannot use Input# but because of differing record/line lengths but the variables are indentical up to the point where the shorter record ends.  So as I may have to use Line Input#, I was wonder how to parse a line that is comma delimited when a text variable/field may contain a comma.  If any of this explanation of the problem is unclear, let me know.

Thanks in advance, hozempa
0
Comment
Question by:hozempa
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1468660
One easy way to do, is reading the file in a string file, ad after process this string as you want.

I will add some sample code tomorrow
0
 
LVL 8

Expert Comment

by:vettranger
ID: 1468661
First, define the varabile "aa$" as a global array of any dimension :

Then, use the following function. Pass it your source string (in your case read via the Line Input# statement), and a string containing your delimiter (in your case ",".

The function will fill dynamically redimension aa$ as it finds more elements in your source string, and will return the number of array elements it placed in aa$.

CODE FOLLOWS :

Public Function ParseString(strSource As String, strDelim As String)
Dim intElementCnt As Integer
Dim intCurPos As Integer
Dim intStrLen As Integer

intElementCnt = 1
intCurPos = 1
intStrLen = Len(strSource)
Do
    ReDim Preserve aa$(1 To intElementCnt)
    intStrLen = (InStr(intCurPos, strSource, strDelim) - intCurPos)
    If intStrLen < 0 Then
        aa$(intElementCnt) = Right$(strSource, (Len(strSource) - (intCurPos - 1)))
    Else
        aa$(intElementCnt) = Mid$(strSource, intCurPos, intStrLen)
        intCurPos = intCurPos + (Len(aa$(intElementCnt)) + Len(strDelim))
        intElementCnt = intElementCnt + 1
    End If
Loop Until intStrLen < 0
ParseString = UBound(aa$)

End Function

0
 
LVL 1

Author Comment

by:hozempa
ID: 1468662
vettranger,
Looking at the code, it appears that it doesn't handles a text variable/field that contains a comma.  If you can provide a sample that does account for this senario then I will accept this answer, otherwise I will have to reject and open it to others.

Regards, hozempa
0
 
LVL 2

Expert Comment

by:cantrell
ID: 1468663
You want to parse the line or simply read it in as is?
0
 
LVL 8

Expert Comment

by:vettranger
ID: 1468664
I use this for parsing comma delimited strings ....

It handles ANY character that you specify as a delimeter, ditzo. If you can't learn to code, and you can't even use or READ code that is put in your lap, then I think you need another line of business!! ROFL
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:hozempa
ID: 1468665
You're correct about the code vettrange. However, I accidentally stated comma in my comment when I meant double quotation marks.  And obviously, your code falls short of this doesn't it.  Considering that the original question stated the problem of text strings containing commas, I believe that you are the one who has the problem with writing code.  Your simple little comma parsing routine is inadequate for the task at hand so, in the future, please do not post an answer if you do not understand the question.  By the way, your code has errors also.

hozempa
0
 
LVL 1

Author Comment

by:hozempa
ID: 1468666
Vettrange,
Acutally, there was nothing wrong with my rejection comment.  If you R-E-A-D it carefully, you may notice that it states your code does not handle a situation where a text field within the comma delimited string contains comma's.  Since this text field should necessary remain entact your code with separate it into more that one variable because it doesn't respect ,"a text field that should be counted as one variable, despite the number of commas it may contain".  By the way, you code is still buggy.
0
 
LVL 2

Accepted Solution

by:
richtsteig earned 100 total points
ID: 1468667
hozempa,

if you don't mind that this code is a little bit buggy too, here is a possible answer
for you:

The Function StrGetParaX is given as string "s" that contains a number of parameters delimeted by "sepChar" (Comma in your case). xChar (double quote) overwrites the sepChar. The Function returns parameter n (or the number of values as string when n is 0)

Function StrGetParaX(s As String, n As Long, sepChar As String, xChr As String) As String

Dim strings() As String

Dim pCount As Long
Dim tmpStr As String
Dim sLine As Long
Dim Item As String

StrGetParaX = vbNullString
If n < 0 Then _
   Exit Function
   
tmpStr = s
pCount = StrCountStr(s, sepChar) + 1 'counting the number of separators
ReDim strings(1 To pCount) 'max number of strings
sLine = 0

Do While Len(tmpStr) > 0
   sLine = sLine + 1
   Item = StrFromTo(tmpStr, Left(tmpStr, 1), True, sepChar, False, False)
   If Left(Item, 1) = xChr And Right(Item, 1) = xChr Then 'complete argument
      strings(sLine) = Mid(Item, 2, Len(Item) - 2)
      tmpStr = Mid(tmpStr, Len(Item) + 2)
   ElseIf Left(Item, 1) = xChr Then
      strings(sLine) = StrFromTo(tmpStr, xChr, False, xChr, False, False)
      tmpStr = Mid(tmpStr, Len(strings(sLine)) + 4)
   Else 'normal
      strings(sLine) = Item
      tmpStr = Mid(tmpStr, Len(Item) + 2)
   End If
Loop

If n = 0 Then
   StrGetParaX = CStr(sLine)
   Exit Function
End If

If n <= UBound(strings) Then _
   StrGetParaX = strings(n)
   
End Function

StrFromTo returns a specified part of the string s.
FromStr is the starting point. IncludeFrom is to decide whether FromStr is part of the result or not. The returnstring will be upto ToStr (included or not). If ToAsCharList is set to True, ToStr will be interpreted as a list of possible delimeters.

Function StrFromTo(s As String, FromStr As String, IncludeFrom As Boolean, ToStr As String, IncludeTo As Boolean, ToAsCharList As Boolean) As String

Dim p As Long
Dim i As Long
Dim tmpStr As String
Dim thischar As String

p = InStr(s, FromStr)
If p = 0 Then
   StrFromTo = vbNullString
   Exit Function
End If

tmpStr = Mid(s, p + Len(FromStr))

If ToAsCharList Then
   StrFromTo = IIf(IncludeFrom, FromStr, vbNullString)
   For i = 1 To Len(tmpStr)
      thischar = Mid(tmpStr, i, 1)
      If InStr(ToStr, thischar) Then
         If IncludeTo Then _
            StrFromTo = StrFromTo + thischar
         Exit Function
      Else
         StrFromTo = StrFromTo + thischar
      End If
   Next i
Else
   p = InStr(tmpStr, ToStr)
   If p = 0 Or ToStr = vbNullString Then
      StrFromTo = tmpStr
   Else
      StrFromTo = Left(tmpStr, p - 1)
      If IncludeTo Then _
         StrFromTo = StrFromTo + ToStr
   End If
   If IncludeFrom Then _
      StrFromTo = FromStr + StrFromTo
End If

End Function

0
 

Expert Comment

by:jicpal
ID: 1468668
Have a look at www.mygale.org/~jumbo/Exchange/ParseList.Txt

Regards,
Pascal
0
 
LVL 2

Expert Comment

by:richtsteig
ID: 1468669
Sorry, I forgot StrCountStr which simply is:

Function StrCountStr(s As String, searchStr As String) As Long

Dim i As Long
Dim pos As Long

StrCountStr = 0
If searchStr = vbNullString Then
   Exit Function
End If

pos = InStr(s, searchStr)
Do While pos > 0
   StrCountStr = StrCountStr + 1
   pos = InStr(pos + Len(searchStr), s, searchStr)
Loop
   
End Function

0
 
LVL 1

Author Comment

by:hozempa
ID: 1468670
richsteig,
Although I have already solved the problem and requested that this question be deleted, I will award the points to you even though I haven't had the chance to look it over.  Thanks for responding to the question.

regards, hozempa
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now