I have a csv file I am reading in and checking the field length - I am getting errors that say my line is too short based on the error logic I coded. But when I look at the csv file, it is fine.
Attribute VB_Name = "Module2"
Sub ImportCSVFileARDet(filenameCSV, numFieldsINT)
On Error GoTo ImportCSVFile_Err
Const ForReading = 1
Dim fso, f As Object
Dim FileLineString As String
Dim LineOpenQuotePosition As Integer
Dim LineCloseQuotePosition As Integer
Dim tmpQryString As String
Dim i As Integer
Dim cnt As Long, totalCnt As Long
ctr = 0
Dim tmpSQL
ErrMsg = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filenameCSV)
GlobalMessage = GlobalMessage & Now() & ": Started Processing file " & filenameCSV & " (" & f.Size & " bytes)." & vbNewLine
OpenConnection ' open connection
OBJdbConnection.BeginTrans
msg = "INFO: Started importing file " & filenameCSV
tmpSQL = "INSERT INTO UpdateLog ( UpdateLogDate, UpdateLogEntry, UpdateComplete )"
tmpSQL = tmpSQL & " VALUES ( CONVERT(datetime, GETDATE(), 101), '" & Left(msg, 255) & "', 'I' )"
OBJdbConnection.Execute tmpSQL
OBJdbConnection.CommitTrans
'CloseConnection ' close connection
'Debug.Print Right(filenameCSV, Len(filenameCSV) - InStr(1, filenameCSV, "\stg")) & " - start: " & Now()
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fs
penTextFile(filenameCSV, ForReading)
If (f.AtEndOfStream) Then
ErrMsg = "Error: Data File (" & Right(filenameCSV, Len(filenameCSV) - InStr(1, filenameCSV, "\stg")) & ") is empty."
GoTo ImportCSVFile_Err
End If
'OpenConnection ' open connection
OBJdbConnection.BeginTrans
' delete import table contents
OBJdbConnection.Execute "DELETE FROM CSVFileImport"
' delete data table contents
OBJdbConnection.Execute "truncate table ARDtlData"
OBJdbConnection.CommitTrans
CloseConnection ' close connection
cnt = 0
totalCnt = 0
ImportCSVLoop_Start:
On Error GoTo ImportCSVFile_Err2
While (Not f.AtEndOfStream)
If cnt = 0 Then
OpenConnection ' open connection
OBJdbConnection.BeginTrans
End If
cnt = cnt + 1
totalCnt = totalCnt + 1
'Debug.Print Right(filenameCSV, Len(filenameCSV) - InStr(1, filenameCSV, "\stg")) & " - start: " & Now()
tmpQryString = "INSERT INTO CSVFileImport ("
For i = 1 To numFieldsINT
tmpQryString = tmpQryString & "Field" & CStr(i) & ", "
Next
tmpQryString = Left(tmpQryString, Len(tmpQryString) - 2) & ") SELECT "
FileLineString = f.ReadLine
NormalLen = 126
If Len(Trim(FileLineString)) < NormalLen Then
WriteLog True, "Line " & totalCnt & " in " & filenameCSV & "is too short. Will be ignored."
WriteLog True, "Line length is " & Len(Trim(FileLineString)) & " characters but it should be " & NormalLen & " characters."
WriteLog True, "Line is -->" & FileLineString & "<--"
GoTo IgnoreLine
End If
Example of csv file information:
" 0002018"," 03975166"," 002"," 001","-0000406.17","P"," ","PHX","42122 ","41072 ","2007/09/24","2007/09/24"," 140350"
" 0002018"," 03976357"," 001"," 001","-0000228.90","P"," ","TIR","42122 ","41072 ","2007/09/24","2007/09/24"," 140350"
" 0002018"," 03976498"," 001"," 001","-0000845.65","P"," ","MES","42122 ","41072 ","2007/09/24","2007/09/24"," 140350"
" 0002018"," 03978182"," 001"," 001","-0000863.09","P"," ","PEO","42519 ","41716 ","2007/10/26","2007/10/26"," 090607"
" 0002018"," 03979089"," 001"," 001","-0000837.69","P"," ","PHX","42519 ","41716 ","2007/10/26","2007/10/26"," 090607"
It happens randomly.
Attribute VB_Name = "Module2"
Sub ImportCSVFileARDet(filenameCSV, numFieldsINT)
On Error GoTo ImportCSVFile_Err
Const ForReading = 1
Dim fso, f As Object
Dim FileLineString As String
Dim LineOpenQuotePosition As Integer
Dim LineCloseQuotePosition As Integer
Dim tmpQryString As String
Dim i As Integer
Dim cnt As Long, totalCnt As Long
ctr = 0
Dim tmpSQL
ErrMsg = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filenameCSV)
GlobalMessage = GlobalMessage & Now() & ": Started Processing file " & filenameCSV & " (" & f.Size & " bytes)." & vbNewLine
OpenConnection ' open connection
OBJdbConnection.BeginTrans
msg = "INFO: Started importing file " & filenameCSV
tmpSQL = "INSERT INTO UpdateLog ( UpdateLogDate, UpdateLogEntry, UpdateComplete )"
tmpSQL = tmpSQL & " VALUES ( CONVERT(datetime, GETDATE(), 101), '" & Left(msg, 255) & "', 'I' )"
OBJdbConnection.Execute tmpSQL
OBJdbConnection.CommitTrans
'CloseConnection ' close connection
'Debug.Print Right(filenameCSV, Len(filenameCSV) - InStr(1, filenameCSV, "\stg")) & " - start: " & Now()
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fs
If (f.AtEndOfStream) Then
ErrMsg = "Error: Data File (" & Right(filenameCSV, Len(filenameCSV) - InStr(1, filenameCSV, "\stg")) & ") is empty."
GoTo ImportCSVFile_Err
End If
'OpenConnection ' open connection
OBJdbConnection.BeginTrans
' delete import table contents
OBJdbConnection.Execute "DELETE FROM CSVFileImport"
' delete data table contents
OBJdbConnection.Execute "truncate table ARDtlData"
OBJdbConnection.CommitTrans
CloseConnection ' close connection
cnt = 0
totalCnt = 0
ImportCSVLoop_Start:
On Error GoTo ImportCSVFile_Err2
While (Not f.AtEndOfStream)
If cnt = 0 Then
OpenConnection ' open connection
OBJdbConnection.BeginTrans
End If
cnt = cnt + 1
totalCnt = totalCnt + 1
'Debug.Print Right(filenameCSV, Len(filenameCSV) - InStr(1, filenameCSV, "\stg")) & " - start: " & Now()
tmpQryString = "INSERT INTO CSVFileImport ("
For i = 1 To numFieldsINT
tmpQryString = tmpQryString & "Field" & CStr(i) & ", "
Next
tmpQryString = Left(tmpQryString, Len(tmpQryString) - 2) & ") SELECT "
FileLineString = f.ReadLine
NormalLen = 126
If Len(Trim(FileLineString)) < NormalLen Then
WriteLog True, "Line " & totalCnt & " in " & filenameCSV & "is too short. Will be ignored."
WriteLog True, "Line length is " & Len(Trim(FileLineString)) & " characters but it should be " & NormalLen & " characters."
WriteLog True, "Line is -->" & FileLineString & "<--"
GoTo IgnoreLine
End If
Example of csv file information:
" 0002018"," 03975166"," 002"," 001","-0000406.17","P"," ","PHX","42122 ","41072 ","2007/09/24","2007/09/24"," 140350"
" 0002018"," 03976357"," 001"," 001","-0000228.90","P"," ","TIR","42122 ","41072 ","2007/09/24","2007/09/24"," 140350"
" 0002018"," 03976498"," 001"," 001","-0000845.65","P"," ","MES","42122 ","41072 ","2007/09/24","2007/09/24"," 140350"
" 0002018"," 03978182"," 001"," 001","-0000863.09","P"," ","PEO","42519 ","41716 ","2007/10/26","2007/10/26"," 090607"
" 0002018"," 03979089"," 001"," 001","-0000837.69","P"," ","PHX","42519 ","41716 ","2007/10/26","2007/10/26"," 090607"
It happens randomly.