Results 1 to 11 of 11

Thread: [RESOLVED] How To Get Data From A TXT File?

  1. #1

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Resolved [RESOLVED] How To Get Data From A TXT File?

    I have made a simple text file user-defined at run-time. I have two Text Boxes called "Text1" and "Text2". I then need the filename, without the file extension to be placed in Text1. And in Text2, what is inside the simple text file.

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: How To Get Data From A TXT File?

    Code:
    Dim sFileName As String
    Dim s As String
    
    sFileName = "C:\test.txt"
    
    Text1.Text = Left$(sFileName, Len(sFileName) - 4) ' -4 for .txt
    
    'OPen file and grab all contents
    Open sFileName For Input As #1
    
    s = Input(LOF(1), 1)
    
    Close #1
    
    Text2.Text = s

  3. #3
    Junior Member
    Join Date
    Apr 2008
    Posts
    30

    Re: How To Get Data From A TXT File?

    Ok. If it's the same as what i am thinking then you would have to do "FSO" as that is the one way i can say will be easy for you

    add this code to a new module
    Code:
    'Declare FileSystemObject Script Variables (FSOSV)
    Public FSO As New FileSystemObject
    Public F As File
    Public fol As Folder
    Then this code for opening
    Code:
    Text1.Text = FSO.OpenTextFile(App.Path & "\database\catalogue\pages.txt", ForReading).ReadAll
    Edit the code, and add the refrences of microsoft scriptlet libery =]
    Is your name George?

  4. #4
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How To Get Data From A TXT File?

    Quote Originally Posted by ThEiMp
    I have made a simple text file user-defined at run-time. I have two Text Boxes called "Text1" and "Text2". I then need the filename, without the file extension to be placed in Text1. And in Text2, what is inside the simple text file.
    Well i think lintz solved your problem except one little thing.

    Your file name you are opening is sFileName = Text1.Text & ".txt" because you said you want to enter file path without extension. And if you enter C:\Test in your textbox you must later add ext. to open the file

  5. #5

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: How To Get Data From A TXT File?

    I am getting an error called, in the following lines of code...
    Error: Text1.Text = Left$(sFileName, Len(sFileName) - 4)
    The error called is error code '5'.

    And yes, I have been able to fix up the file extension that was inside the code, I got from the last post I got. And that I don't think is the problem with this code. Because that code doesn't get executed until this line of code with the error in it, gets executed.

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: How To Get Data From A TXT File?

    What is value is 'sFileName'?

    My example worked fine for me

  7. #7
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: How To Get Data From A TXT File?

    Quote Originally Posted by Dungeon Keeper
    Your file name you are opening is sFileName = Text1.Text & ".txt" because you said you want to enter file path without extension.
    I interpreted the initial question as he already knew the file name and wanted to display it in Text1 but without the extension...

  8. #8

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: How To Get Data From A TXT File?

    Bye the way... The value of sFileName is found inside "Form4.Text1.Text".

  9. #9

    Thread Starter
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,948

    Re: How To Get Data From A TXT File?

    Yes. I want to input the data into Text1. But get the program to add the extension of *.TXT onto it after you click OK, which is found in the Form.

  10. #10
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: How To Get Data From A TXT File?

    Oh OK. Try this.

    Code:
    Sub Command1_Click()
    Dim sFileName As String
    Dim s As String
    
    sFileName = Text1.Text & ".txt"
    
    'OPen file and grab all contents
    Open sFileName For Input As #1
    
    s = Input(LOF(1), 1)
    
    Close #1
    
    Text2.Text = s 
    
    End Sub

  11. #11
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    Re: How To Get Data From A TXT File?

    Hi, i have tried lintz' code and it works. But when i try to edit it and apply it in my program, i get an error :Run time error '62' Input past end of line. May i know what is the problem?

    Code:
    Private Sub Command1_Click()
    Dim sFileName As String
    Dim s As String
    
    sFileName = "C:\Documents and Settings\All Users\Application Data\BVRP Software\mobile PhoneTools\document\recu0006.SMS"
    
    Text1.Text = Left$(sFileName, Len(sFileName) - 4) ' -4 for .txt
    
    'OPen file and grab all contents
    Open sFileName For Input As #1
    
    s = Input(LOF(1), 1)
    
    Close #1
    
    Text2.Text = s
    End Sub
    Basically, i just change the path. My file is .SMS extension but it can be opened by using notepad. Is this cause the problem?
    I'm still on the path of learning....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width