Re: reading a file that has null or blank field



thanks for your reply.

I understand why it is happening, but I don't know how to handle such an
issue. Isn't there a function that would allow me to handle error?

thanks again.

"ekkehard.horner" wrote:

RdS wrote:

hi,

when reading through a CSV file and a field is missing I receive a run time
error. I have the on error resume next commented out for now during
debugging. But, how do I handle if a field is missing when trying to assign
the array? In this case would missing mean "NULL"?

No, there aren't NULL values in plain text files processed with
the FileSystemObject. The error results from trying to access
elements from arySourceFields which aren't there because Split()
couldn't create them due to lack of ","s.

The code is as follows:

set objFSO = CreateObject("Scripting.FileSystemObject")
set objSourceFile = objFSO.OpenTextFile(strSourceFilename, ForReading)
if err <> 0 then ErrHandler

Const UBOUND_EXPECTED = 4 ' 5 items indexed 0 ... 4

do until objSourceFile.AtEndOfStream
arySourceFields = split(objSourceFile.ReadLine,",",-1,1)
If UBOUND_EXPECTED <> UBound( arySourceFields ) Then
skip or raise hell
Else
work with all fields
End If
if err <> 0 then ErrHandler
wscript.stdout.write arySourceFields(0) '''remove.
loop
.
.
.


Example of csv file contents:
500,world,1,1,5
501,now,2,6,0
.
.
.
600,,,
601

the problem occurs for records like 601 above. notice there are no commas
seperating other fields. the csv just has first field and nothing in
remaining fields like rest of records. Code works fine for all other
instances including 600,,,

Assigning arySourceFields(x) to any variable or simply doing the
stdout.write above causes runtime of:
createfiles.vbs(102,1) Microsoft VBScript runtime error: Subscript out of
range: 'wscript.stdout'

How do I prevent the error from occuring in my code without the on error
resume next? Isn't there a function that would check to see if the subject
is valid?

Thank you.

.



Relevant Pages

  • Re: reading a file that has null or blank field
    ... sorry...but I see you answered inline. ... how do I handle if a field is missing when trying to assign ... Example of csv file contents: ...
    (microsoft.public.scripting.vbscript)
  • Re: reading a file that has null or blank field
    ... when reading through a CSV file and a field is missing I receive a run time ... how do I handle if a field is missing when trying to assign ... Example of csv file contents: ...
    (microsoft.public.scripting.vbscript)
  • Re: reading a file that has null or blank field
    ... when reading through a CSV file and a field is missing I receive a run time error. ... how do I handle if a field is missing when trying to assign the array? ... createfiles.vbsMicrosoft VBScript runtime error: Subscript out of range: 'wscript.stdout' ...
    (microsoft.public.scripting.vbscript)
  • Re: CSV dropping last comma for all lines after line 15
    ... Column Delimiters Missing in Spreadsheet Saved as Text ... Then try saving the file as a CSV file. ... noticed that Excel is dropping the last comma on the lines beyond data line ...
    (microsoft.public.excel.setup)

Loading