Solved

Reading Delimited Text Files

Posted on 1998-08-12
11
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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