Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Ok, the effort to save processing time backfired
somewhat. You DO need to kill the schema.ini at the end, which I would've realized if I had been thinking straight. -----Original Message----- A co-worker of mine discovered that the F, K, and S cause the column to be read as currency, but that a schema.ini solves the problem. So I added a section into the function that counts the fields in the string and creates a schema.ini making each one a text field. It all seems a bit long to me, but it also seems to work. And since there are no Excel workbooks involved, I'm hoping I can also port it over to Access without too much trouble, although I haven't actually tried that yet. I'm a great believer in sharing, so for anyone else who might be interested, here's the code: Public Function SplitCSVString(strInput As String) As Variant 'CSV String Parser 'It's sort of like Split(), but the delimiter is always a comma, 'and it can handle quotation mark text qualifiers. 'Always returns an array of at least one element. 'If the input string is empty (""), the element will also be an empty string. 'If the input string contains a LineFeed or CarriageReturn character, 'the line break and anything following it will be dropped. Dim strPathName As String Dim strFileName As String Dim intFileNum As Integer Dim strSplitString() As String Dim cntFolder As New ADODB.Connection Dim rstFile As New ADODB.Recordset Dim intFields As Integer strPathName = "C:\" ' strFileName = "SplitCSVString" & Format (Date, "yyyymmdd") & ".csv" strFileName = "SplitCSVString.csv" intFileNum = FreeFile() ReDim strSplitString(1 To 1) 'No need to go to a lot of trouble if there's nothing to split. If Not InStr(strInput, ",") 0 Then strSplitString(1) = strInput SplitCSVString = strSplitString Exit Function End If 'Sorry folks, one line only If InStr(strInput, vbLf) 0 Then strInput = Left(strInput, InStr(strInput, vbLf) - 1) End If If InStr(strInput, vbCr) 0 Then strInput = Left(strInput, InStr(strInput, vbLf) - 1) End If 'No need to go to a lot of trouble if there's nothing to split. If Not InStr(strInput, ",") 0 Then strSplitString(1) = strInput SplitCSVString = strSplitString Exit Function End If 'Write the string to a file Open strPathName & strFileName For Output As #intFileNum Print #intFileNum, strInput Close #intFileNum 'Read the file with the parser to get column count cntFolder.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strPathName & ";" & "Extended Properties=""text;HDR=No;FMT=Delimited""" rstFile.Open "SELECT * FROM " & strFileName, cntFolder, adOpenStatic, adLockReadOnly 'Write schema.ini Open strPathName & "schema.ini" For Output As #intFileNum Print #intFileNum, "[" & strFileName & "]" Print #intFileNum, "ColNameHeader=False" Print #intFileNum, "Format=CSVDelimited" For intFields = 1 To rstFile.Fields.Count Print #intFileNum, "Col" & intFields & "=C" & intFields & " Char" Next intFields Close #intFileNum rstFile.Close 'Read the file with the parser rstFile.Open "SELECT * FROM " & strFileName, cntFolder, adOpenStatic, adLockReadOnly rstFile.MoveFirst For intFields = 1 To rstFile.Fields.Count ReDim Preserve strSplitString(1 To intFields) If IsNull(rstFile.Fields(intFields - 1).Value) Then strSplitString(intFields) = "" Else strSplitString(intFields) = rstFile.Fields (intFields - 1).Value End If Next intFields rstFile.Close cntFolder.Close 'Kill the file - skipping these 2 lines in an effort to save processing time ' Kill strPathName & strFileName ' Kill strPathName & "schema.ini" 'Return the array SplitCSVString = strSplitString End Function -----Original Message----- Ok, this is getting very discouraging. I thought I had everything solved. I really did. The function has worked GREAT...until today. I think I've run across some sort of bug. If the input string is this: "ENTRY,327,327f,f,F,""327F"",327F,327K,327S" we discover that the uppercase F, K, and S are dropped when they're combined with a number and not enclosed in quotes. I've tried interrupting the function before it kills the csv file and it with Excel, and it works fine. But not with the ADODB connection. I am absolutely at my wit's end. SURELY there has got to be SOME WAY to accurately parse a simple csv string, but for the life of me, I can't seem to find it! HELP!!!!!!!! . |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
parse text string | Excel Worksheet Functions | |||
Parse this string | Excel Discussion (Misc queries) | |||
Q: parse string | Excel Discussion (Misc queries) | |||
String Parse | Excel Programming | |||
Wish to parse through a text string to find data | Excel Programming |