Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4619678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:27:25+00:00 2026-05-22T02:27:25+00:00

So i’m new to working with vba in access and i’m having trouble getting

  • 0

So i’m new to working with vba in access and i’m having trouble getting this code to work. What it is suppose to do is take a selected text file and read the original file into a list box. Then there is a second button that when pressed will convert the text file from a pipe delimited file into a tab delimited file and then show the changed file into a new listbox.

Option Compare Database
Option Explicit


Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'

    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function

Private Sub Command0_Click()
 Dim fdlg As Office.FileDialog

    Dim pipe_file As Variant
    Dim FileName As String
    Dim fn As Integer
    Dim varFile As Variant
    Dim FilePath As String

    Me.OrigFile.RowSource = ""
    Me.ConvertFile.RowSource = ""
    Me.FileName = ""
    Me.FilePath = ""
    FileName = ""



    Set fdlg = Application.FileDialog(msoFileDialogFilePicker)
    With fdlg
        .AllowMultiSelect = False
        .Title = "Select pipe delimited file"
        .Filters.Clear
        .Filters.Add "Text Files", "*.txt"

        If .Show = True Then
            For Each varFile In .SelectedItems
                FileName = GetFilenameFromPath(varFile)
                FilePath = varFile
            Next varFile
            Me.FileName = FileName
            Me.FilePath = FilePath

            fn = FreeFile

            Open FileName For Input As #fn
            Do While Not EOF(fn)
                Line Input #fn, pipe_file
                Me.OrigFile.AddItem pipe_file
            Loop

            Close #fn
        Else
            MsgBox "You clicked Cancel in the file dialog box."
        End If
    End With
End Sub

Private Sub Convert_File_Click()
'ByVal OutputFile As String)'
On Error GoTo error1
Dim pipe_file As Variant
Dim ThisString As String
Dim NewString As String
Dim A As Integer
Dim InputFile As String
InputFile = Me.FilePath
Open InputFile For Input As #1

Const FileName = "c:\outputfile.txt"
Dim my_filenumber As Integer
my_filenumber = FreeFile
Open FileName For Output As #2
'Open OutputFile For Output As #2'

While Not EOF(1)
NewString = ""
Line Input #1, ThisString
For A = 1 To Len(ThisString)
If Mid(ThisString, A, 1) = "|" Then
NewString = NewString & Chr$(9)
Else
NewString = NewString & Mid(ThisString, A, 1)
End If
Next

Print #2, ThisString
Wend
Do While Not EOF(2)
Line Input #2, pipe_file
Me.ConvertFile.AddItem pipe_file
Loop
Close #2
Close #1
Exit Sub
error1:
Close #1
Close #2
End Sub

This is what i have so far now my issue is pertaining to the second button or Convert_File_Click() convertfile is the listbox i’m trying to update and filepath is a textbox that hold the filepath of the textfile that is selected.
Any help is appreciated, Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:27:25+00:00Added an answer on May 22, 2026 at 2:27 am

    Okay so after spending some time researching it and a lot of time debugging i finally figured it out so i figured i’d post my results in case somebody else ever needs help with this

        Function PipeToTab(ByVal OriginalText As String) As String
    'Runs though current line of text stored in original text'
     On Error GoTo error1
     Dim ThisString As String, NewString As String, a As Integer
     NewString = ""
    
    For a = 1 To Len(OriginalText)
    'checks to see if current char is white space and if it is removes it
        If Mid(OriginalText, a, 1) = " " Then
        'checks to see if current char is | and if it is changes it to char$(9) (tab)
        ElseIf Mid(OriginalText, a, 1) = "|" Then
            NewString = NewString & Chr$(9)
        Else
            NewString = NewString & Mid(OriginalText, a, 1)
        End If
    Next
        PipeToTab = NewString
     Exit Function
    error1:
    MsgBox (Err.Description)
    
     End Function`
    

    This is the function i came up with to convert a line of text from the text file from “|” to tabs as well as removing any additional white space.

        `Private Sub Convert_File_Click()
        On Error GoTo error1
        Dim pipe_file As Variant
        Dim ThisString As String
        Dim a As Integer
        Dim rfs, rts, InputFile, wfs, wts, OutputFile As Object
        Dim InputFileName, OutputFileName, OriginalText, updatedText As String
    
        ' File initialization
        'open the original source file and create the output file with the name desired from textbox.
        InputFileName = Me.FilePath 'filepath is a textbox that holds the location 
        'and name of where you want the textfile to go 
            Set rfs = CreateObject("Scripting.FileSystemObject")
            Set InputFile = rfs.GetFile(InputFileName)
    
    
        'open the text streams
            Set rts = InputFile.OpenAsTextStream(1, -2) 'Read
            Set wts = OutputFile.OpenAsTextStream(8, -2) 'Append
    
        'then put line into conversion function and get the updated text
        'move onto the next line until EOF
    
            While rts.AtEndofStream = False
                 OriginalText = rts.ReadLine 'read current line of file
                 If OriginalText <> Empty Then
                   updatedText = PipeToTab(OriginalText)
                   wts.WriteLine updatedText 'put updated text into newly created file(output file)
                Else
                End If
            Wend`
    'Output file clean up
        wts.Close
    'Input File clean up
        rts.Close
    
    
    End If
    'clear out filestreams
        Set OutputFile = Nothing
        Set wfs = Nothing
        Set wts = Nothing
        Set InputFile = Nothing
        Set rfs = Nothing
        Set rts = Nothing
    
    Exit Sub
    error1:
    ' File Clean up
    rts.Close
    Set InputFile = Nothing
    Set rfs = Nothing
    Set rts = Nothing
    
    'Output
    wts.Close
    Set OutputFile = Nothing
    Set wfs = Nothing
    Set wts = Nothing
    MsgBox (Err.Description)
    End Sub
    

    This here is the button used to convert the text file. I used text streams and a line reader in order to send each line of the text file into the pipe to tab function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.