Link to home
Start Free TrialLog in
Avatar of CFMI
CFMIFlag for United States of America

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(tcFileOut)
    Set objStreamRead = objFSO.OpenTextFile(tcFileIn)
   
    Do Until objStreamRead.AtEndOfStream
        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
Avatar of omgang
omgang
Flag of United States of America image

First, you need to instantiate the FileSystemObject objFSO

Set objFSO = New FileSystemObject

With that addition the function runs but the tab delimited file that gets created is empty.
OM Gang
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
You have j to increment here but should be i
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(tcFileOut)
    Set objStreamRead = objFSO.OpenTextFile(tcFileIn)
   
    Do Until objStreamRead.AtEndOfStream
        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
Avatar of CFMI

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?
ASKER CERTIFIED SOLUTION
Avatar of omgang
omgang
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFMI

ASKER

Excellent, It work well - Thank you!!!