CFMI
asked on
MS Access VBA to convert File types
Hello Experts,
I wrote some VBA code to switch from pipe delimited to a Tab delimited file but my VAB doesn't debug and I hope it works; can you please help?
Thanks!
Public Function ConvertFile(tcFileIn As String, tcFileOut As String)
'Declare variables.
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim objFSO As Scripting.FileSystemObject
Dim objStreamRead As Scripting.TextStream
Dim objStreamWrite As Scripting.TextStream
Dim lcLine As String
Dim laLine() As String
Dim lcField As String
Dim i As Integer
Set objStreamWrite = objFSO.CreateTextFile(tcFi leOut)
Set objStreamRead = objFSO.OpenTextFile(tcFile In)
Do Until objStreamRead.AtEndOfStrea m
lcLine = objStreamRead.ReadLine
laLine = Split(lcLine, "/")
lcLine = ""
For i = 1 To UBound(laLine)
lcField = Trim(laLine(j))
If Right(lcField, 1) = Chr(10) Then
lcField = Trim(Left(lcField, Len(lcField) - 1))
End If
lcLine = lcLine & lcField & Chr(9)
i = i + 1
Next i
objStreamWrite.Write (lcLine) & Chr$(10)
Loop
End Function
I wrote some VBA code to switch from pipe delimited to a Tab delimited file but my VAB doesn't debug and I hope it works; can you please help?
Thanks!
Public Function ConvertFile(tcFileIn As String, tcFileOut As String)
'Declare variables.
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim objFSO As Scripting.FileSystemObject
Dim objStreamRead As Scripting.TextStream
Dim objStreamWrite As Scripting.TextStream
Dim lcLine As String
Dim laLine() As String
Dim lcField As String
Dim i As Integer
Set objStreamWrite = objFSO.CreateTextFile(tcFi
Set objStreamRead = objFSO.OpenTextFile(tcFile
Do Until objStreamRead.AtEndOfStrea
lcLine = objStreamRead.ReadLine
laLine = Split(lcLine, "/")
lcLine = ""
For i = 1 To UBound(laLine)
lcField = Trim(laLine(j))
If Right(lcField, 1) = Chr(10) Then
lcField = Trim(Left(lcField, Len(lcField) - 1))
End If
lcLine = lcLine & lcField & Chr(9)
i = i + 1
Next i
objStreamWrite.Write (lcLine) & Chr$(10)
Loop
End Function
By changing this line
laLine = Split(lcLine, "/")
to
laLine = Split(lcLine, "|")
It does split on the pipe symbol but I'm seeing this resulg
Original = This|Is|My|Pipe|Delimited| File
Output = This This This
OM Gang
laLine = Split(lcLine, "/")
to
laLine = Split(lcLine, "|")
It does split on the pipe symbol but I'm seeing this resulg
Original = This|Is|My|Pipe|Delimited|
Output = This This This
OM Gang
You have j to increment here but should be i
lcField = Trim(laLine(j))
getting closer
OM Gang
lcField = Trim(laLine(j))
getting closer
OM Gang
Two more things:
1) you're doulbe incrementing here
i = i + 1
Next i
2) arrays index at 0 not 1 so you're actually starting at the second array value
For i = 1 To UBound(laLine)
Try this
Public Function ConvertFile(tcFileIn As String, tcFileOut As String)
On Error GoTo Err_ConvertFile
'Declare variables.
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim objFSO As Scripting.FileSystemObject
Dim objStreamRead As Scripting.TextStream
Dim objStreamWrite As Scripting.TextStream
Dim lcLine As String
Dim laLine() As String
Dim lcField As String
Dim i As Integer
Set objFSO = New FileSystemObject
Set objStreamWrite = objFSO.CreateTextFile(tcFi leOut)
Set objStreamRead = objFSO.OpenTextFile(tcFile In)
Do Until objStreamRead.AtEndOfStrea m
lcLine = objStreamRead.ReadLine
laLine = Split(lcLine, "|")
lcLine = ""
For i = 0 To UBound(laLine)
lcField = Trim(laLine(i))
If Right(lcField, 1) = Chr(10) Then
lcField = Trim(Left(lcField, Len(lcField) - 1))
End If
lcLine = lcLine & lcField & Chr(9)
'i = i + 1
Next i
objStreamWrite.Write (lcLine) & Chr$(10)
Loop
objStreamRead.Close
objStreamWrite.Close
Exit_ConvertFile:
Set objStreamWrite = Nothing
Set objStreamRead = Nothing
Set objFSO = Nothing
Exit Function
Err_ConvertFile:
MsgBox Err.Number & " (" & Err.Description & ") in procedure ConvertFile of Module Module9"
Resume Exit_ConvertFile
End Function
OM Gang
1) you're doulbe incrementing here
i = i + 1
Next i
2) arrays index at 0 not 1 so you're actually starting at the second array value
For i = 1 To UBound(laLine)
Try this
Public Function ConvertFile(tcFileIn As String, tcFileOut As String)
On Error GoTo Err_ConvertFile
'Declare variables.
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim objFSO As Scripting.FileSystemObject
Dim objStreamRead As Scripting.TextStream
Dim objStreamWrite As Scripting.TextStream
Dim lcLine As String
Dim laLine() As String
Dim lcField As String
Dim i As Integer
Set objFSO = New FileSystemObject
Set objStreamWrite = objFSO.CreateTextFile(tcFi
Set objStreamRead = objFSO.OpenTextFile(tcFile
Do Until objStreamRead.AtEndOfStrea
lcLine = objStreamRead.ReadLine
laLine = Split(lcLine, "|")
lcLine = ""
For i = 0 To UBound(laLine)
lcField = Trim(laLine(i))
If Right(lcField, 1) = Chr(10) Then
lcField = Trim(Left(lcField, Len(lcField) - 1))
End If
lcLine = lcLine & lcField & Chr(9)
'i = i + 1
Next i
objStreamWrite.Write (lcLine) & Chr$(10)
Loop
objStreamRead.Close
objStreamWrite.Close
Exit_ConvertFile:
Set objStreamWrite = Nothing
Set objStreamRead = Nothing
Set objFSO = Nothing
Exit Function
Err_ConvertFile:
MsgBox Err.Number & " (" & Err.Description & ") in procedure ConvertFile of Module Module9"
Resume Exit_ConvertFile
End Function
OM Gang
ASKER
Unfortunately, we receive, a Compile error: ‘User-defined type not defined” on this syntax –
Dim fso As New FileSystemObject
Is it possible, I need to select another Reference?
Dim fso As New FileSystemObject
Is it possible, I need to select another Reference?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, It work well - Thank you!!!
Set objFSO = New FileSystemObject
With that addition the function runs but the tab delimited file that gets created is empty.
OM Gang