Line Input and Parsing.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone provide sample code of finding 14 commas (a sample of 3 commas
would do)...so I can Parse data which has a string WITHOUT double-quotes
around it?

TIA - Bob
 
Bob Barnes said:
Can someone provide sample code of finding 14 commas (a sample of 3
commas would do)...so I can Parse data which has a string WITHOUT
double-quotes around it?

Please post a sample of the text in the input file.
 
Dirk - Here it is...Notice how the first string of 050234 is WITHOUT double
quotes (that's how the software writes it).

ALL commas currently in text will be replace w/ ...

Thank you - Bob

050234,"050234",2006-08-24 00:00:00,2006-08-24 00:00:00,"154","Is the center
maintaining copies of all hotwork permits issued for the site for a period of
one year?","If a hotwork permit is issued for a facility, that facility is
required to maintain both the pink and yellow copies of the permit in their
Compliance Manual for a period of one (1) year.","CTOA","Other",2006-08-29
00:00:00,2006-08-30 00:00:00,"hot work permit found",""
 
Bob Barnes said:
Dirk - Here it is...Notice how the first string of 050234 is WITHOUT
double quotes (that's how the software writes it).

ALL commas currently in text will be replace w/ ...

Thank you - Bob

050234,"050234",2006-08-24 00:00:00,2006-08-24 00:00:00,"154","Is the
center maintaining copies of all hotwork permits issued for the site
for a period of one year?","If a hotwork permit is issued for a
facility, that facility is required to maintain both the pink and
yellow copies of the permit in their Compliance Manual for a period
of one (1) year.","CTOA","Other",2006-08-29 00:00:00,2006-08-30
00:00:00,"hot work permit found",""

Here's a simple example of parsing into fields using the comma as a
delimiter. It reads and parses only a single line from the file --
there's no loop logic to read multiple lines.

'----- start of code -----
Dim sPath As String
Dim iFile As Integer
Dim sLine As String
Dim aFields() As String
Dim iX As Integer

sPath = "C:\Temp\BarnesTest.txt" ' substitute your path

iFile = FreeFile
Open sPath For Input As #iFile

' Read a line into a string variable
Line Input #iFile, sLine

' Split the line based on the comma delimiter
aFields = Split(sLine, ",")

' Report the results.
Debug.Print "The line has "; UBound(aFields) + 1; " fields."
For iX = 0 To UBound(aFields)
Debug.Print "Field " & iX & ":", aFields(iX)
Next iX

Close iFile
'----- end of code -----

Now, I see a problem using this simple approach with the sample line you
posted. One of the text strings that make up the line includes a comma.
This simplistic parsing won't understand that a comma that is within
quotes must be ignored, not used as a delimiter. I'm not sure whether
your statement that
ALL commas currently in text will be replace w/ ...

means that this won't be a problem in practical application.
 
Dirk - Thank you. You've helped me several times before.

I did the following in test mode...
Line Input #iNumOne, vStore
MsgBox vStore
Y = Split(vStore, ",")(0)
MsgBox Y
Y = Split(vStore, ",")(1)
MsgBox Y
Y = Split(vStore, ",")(2)
MsgBox Y
Y = Split(vStore, ",")(3)
MsgBox Y
Y = Split(vStore, ",")(4)
MsgBox Y
Y = Split(vStore, ",")(5)
MsgBox Y
Y = Split(vStore, ",")(6)
MsgBox Y
Y = Split(vStore, ",")(7)
MsgBox Y
Y = Split(vStore, ",")(8)
MsgBox Y
Y = Split(vStore, ",")(9)
MsgBox Y
Y = Split(vStore, ",")(10)
MsgBox Y
Y = Split(vStore, ",")(11)
MsgBox Y
Y = Split(vStore, ",")(12)
MsgBox Y
Y = Split(vStore, ",")(13)
MsgBox Y

It works. I then need to strip some extra double quotes, but that can be
handled. I'll then load that data into a reciving table and test for
duplicates of a 3-key Primary Key...all doable.

Thank you again - Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top