Consulting

Results 1 to 7 of 7

Thread: read textfile

  1. #1

    read textfile

    I've added a textbox (FName) on Userform1 and Userform2 for the user's name. When the user initializes the program via Userform1, Data1; vbTab; Data2; vbTab; Data3; vbTab; FName are entered into the textfile. When Userform2 is displayed, the code populates the labels and textbox from the information found in the textfile. I'm able to populate FName with my name found in the textfile, but names varies in lenth. How can I adjust this code to account for varying lengths in names?

    flName = Split(f, vbTab)(3)
    FName = Left$(flName, 15)
    Last edited by Aussiebear; 04-07-2023 at 01:52 PM. Reason: Added code tags

  2. #2
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    Try

    FName = Left$(flName, Len(flName))

    Rob
    Last edited by Aussiebear; 04-07-2023 at 01:52 PM. Reason: Added code tags

  3. #3
    I tried the code and it inputs a "Enter" symbol behind my name. The symbol looks like a shaded backwards P.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    If you wrote

    Data1; vbTab; Data2; vbTab; Data3; vbTab; FName 

    to the textfile, then flName should return okay on its own.
    Last edited by Aussiebear; 04-07-2023 at 01:53 PM. Reason: Added code tags

  5. #5
    GTO, you are right. I discovered that when writing the info to the text file, the code creates a enter input in the text file (The cursor was not at the end of the line of text; like hitting the enter button to move to the next line). So my question now, when writing the info to the textfile, how do I prevent the code from creating an Enter input?

  6. #6
    I put a semi-colon at the end of the code. I'm not sure that the correct way of doing it, but it seems to work

    Data1; vbTab; Data2; vbTab; Data3; vbTab; FName;
    Last edited by Aussiebear; 04-07-2023 at 01:54 PM. Reason: Adjusted the code tags

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by av8tordude
    GTO, you are right. I discovered that when writing the info to the text file, the code creates a enter input in the text file (The cursor was not at the end of the line of text; like hitting the enter button to move to the next line). So my question now, when writing the info to the textfile, how do I prevent the code from creating an Enter input?
    I think you would need to show an example file so that we could see how it is actually constructing/readin the textfile. .xls Format if possible, as I do not have access to .xlsm until Monday.

    Here was my admittedly (and early-bound) simplistic testing:

    Userform1:

    Option Explicit
        
    Private Sub CommandButton1_Click()
    Dim sData1 As Variant, sData2 As Variant, sData3 As Variant, sFName As String
    Dim FSO As FileSystemObject
    Dim fsoTStream As TextStream
    Dim strLine As String
    sData1 = txtData_1.Value
        sData2 = txtData_2.Value
        sData3 = txtData_3.Value
        sFName = txtFName.Value
    Set FSO = New FileSystemObject
    Set fsoTStream = FSO.CreateTextFile(ThisWorkbook.Path & "\Test.txt", True)
    fsoTStream.WriteLine sData1 & vbTab & sData2 & vbTab & sData3 & vbTab & sFName
    fsoTStream.Close
    Unload Me
    UserForm2.Show
    End Sub
    UserForm2:

    Private Sub UserForm_Initialize()
    Dim FSO As FileSystemObject
    Dim fsoTStream As TextStream
    Set FSO = New FileSystemObject
        Set fsoTStream = FSO.OpenTextFile(ThisWorkbook.Path & "\Test.txt", ForReading, False, TristateUseDefault)
        TextBox1.Value = Split(fsoTStream.ReadLine, vbTab)(3)
    End Sub
    Last edited by Aussiebear; 04-07-2023 at 01:55 PM. Reason: Adjusted the code tags

Posting Permissions

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