Results 1 to 10 of 10

Thread: [RESOLVED] Write / Read to a specific line in text

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    32

    Resolved [RESOLVED] Write / Read to a specific line in text

    Hello Friends,

    I have a list with 40 lines which each line contains 3 data.
    I want to read a specific line and get said 3 data and assign them to 3 text boxes on the form.

    (LINE1)Data1,Data2,Data3
    (LINE2)Data1,Data2,Data3
    (LINE3)Data1,Data2,Data3
    (LINE4)Data1,Data2,Data3
    (LINE5)Data1,Data2,Data3
    .
    .
    .
    etc.

    Case 1
    Let's say. I wan to get Line4 's Data and assign them to text boxes as
    Text1 = Data1, Text2 = Data2, Text3=Data3

    Case2
    Let's say I want to change LINE2's data in text file with text boxes value as
    Data1 = Text1, Data2 = Text2, Data3 = Text3 and write it to the file.

    Can someone help me ?

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Write / Read to a specific line in text

    This may not really be an area where text files are of great use.

    Here's a crude way: Create an array of strings and read up the text file and store the lines in the array. Now manipulate the contents of the array as you want. Once you are done, write the array back to the text file, overwriting any existing contents.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    32

    Re: Write / Read to a specific line in text

    Dear honeybee, thanks for your suggestion. To be honest I am not so good at VB and I don't know how to do your suggestion. Before I write this message to you I searched this forum for "Data from File to Array" and I found some codes but I couldn't adopt them to my file. So, it would be really great, if you could write sample codes for me. Many thanks in advance.

  4. #4
    gibra
    Guest

    Re: Write / Read to a specific line in text


  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Write / Read to a specific line in text

    Here's a sample that you can play with. Hopefully the comments will help you understand the code and logic involved.
    Code:
    Option Explicit
    '
    ' Assumes the following are drawn on the Form:
    '       Three TextBoxes, named: Text1, Text2, Text3
    '
    '       Four Command Buttons, named:
    '         cmdSelectData - when clicked will prompt user for
    '                         a Line Number of the file to be changed
    '                         The data from that line will be displayed
    '                         int the TextBoxes
    '         cmdChange -     when clicked will save the data in the TextBoxes
    '                         back to the array of lines
    '         cmdSaveData -   when clicked will save all the data back to
    '                         the original File
    '         cmdRefresh -    when clicked will re-read the file from disk
    '
    Private strFile As String
    Private strLines() As String
    Private strElements() As String
    Private intLine As Integer
    
    Private Sub GetData()
    Dim intFile As Integer
    '
    ' Obtain a free File Number
    ' and open the input file
    '
    intFile = FreeFile
    Open strFile For Input As intFile
    '
    ' Read the entire contents of the file
    ' close it and split into records
    '
    strData = Input(LOF(intFile), intFile)
    Close intFile
    strLines = Split(strData, vbNewLine)
    '
    ' strLines(0) contains Line 1 of the file
    ' strLines(1) contains Line 2 of the file
    ' etc
    ' strLines(UBound(strLines)) contains last line of the file
    '
    End Sub
    
    Private Sub cmdRefresh_Click()
    Call GetData
    End Sub
    
    Private Sub cmdSaveData_Click()
    '
    ' Save the data back to the original file
    '
    Dim strData As String
    Dim intFile As Integer
    intFile = FreeFile
    Open strFile For Output As intFile
    '
    ' Create the output from the strLines array
    ' and write it to the file
    '
    strData = Join(strLines, vbNewLine)
    Print #intFile, strData;
    Close intFile
    End Sub
    
    Private Sub cmdSelectData_Click()
    intLine = CInt(InputBox("Which Line do you want to change?"))
    strElements = Split(strLines(intLine - 1), ",")
    '
    ' strElements(0) = data1 from the selected line
    ' strelements(1) = data2 from the selected line
    ' strElements(2) = data3 from the selected line
    ' Display the data in the TextBoxes
    '
    Text1.Text = strElements(0)
    Text2.Text = strElements(1)
    Text3.Text = strElements(2)
    End Sub
    
    Private Sub cmdChange_Click()
    '
    ' Put the new data back into the strLines array
    '
    strLines(intLine) = Text1.Text & "," & Text2.Text & "," & Text3.Text
    End Sub
    
    
    Private Sub Form_Load()
    '
    ' Get the data from the file
    '
    strFile = "C:\MyDir\MyFile.dat"     ' The full path to, and name of your file
    Call GetData
    End Sub
    Run the code and click on cmdSelectData; enter the line number (first line is 1) you want to change. The data from that line will appear in the TextBoxes; make any changes you want and click on cmdChange; you can then click on cmdSelectData and change another record etc.

    When you've finished making all the changes, click on cmdSaveData to write the data back to the file.

    If you want to see the changed data after saving it, click on cmdRefresh and go through the process again. If you click on cmdRefresh before clicking on cmdSaveData you'll loose any changes you've made.

    (If you don't understand the Input, Split and Join functions then look in Help.)

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    32

    Re: Write / Read to a specific line in text

    Dear Doogle, thanks for the code. As you requested, I created 3 textboxes and 4 command buttons and renamed them as defined.
    Then I changed the file path in code as
    Code:
    strFile = "C:\test.txt"
    but when I run program it gives "Compile Error : Variable not Defined" under GetData() and showing below line.
    Code:
    strData = Input(LOF(intFile), intFile)

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    32

    Re: Write / Read to a specific line in text

    Ok, I found the problem.

    there was miss coding and when I changed them as herebelow, program window appeared.

    Code:
    Open strFile For Input As intFile
    
    strFile = Input(LOF(intFile), intFile)
    Select button works. I clicked Change button then Save button but now it gives another error as "Run-time error "52": Bad file name or number" in herebelow line
    Code:
    Open strFile For Output As intFile

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    32

    Smile Re: Write / Read to a specific line in text

    Annddd, I fixed said problem as well Just I replaced file path with StrFile.

    Code:
    strFile = "C:\test.txt"
    but then I notized that program is saving changed data in text boxes to wrong line. I mean if you change Line3, it saves data to line4. And I fixed this problem with putting -1 as herebelow.

    Code:
    strLines((intLine - 1)) = Text1.Text & "," & Text2.Text & "," & Text3.Text
    Now program is working as I wish. Many many thanks Doogle.

    I am writing whole code here again, maybe someone else may need this codes in the future.

    Code:
    Option Explicit
    '
    ' Assumes the following are drawn on the Form:
    '       Three TextBoxes, named: Text1, Text2, Text3
    '
    '       Four Command Buttons, named:
    '         cmdSelectData - when clicked will prompt user for
    '                         a Line Number of the file to be changed
    '                         The data from that line will be displayed
    '                         int the TextBoxes
    '         cmdChange -     when clicked will save the data in the TextBoxes
    '                         back to the array of lines
    '         cmdSaveData -   when clicked will save all the data back to
    '                         the original File
    '         cmdRefresh -    when clicked will re-read the file from disk
    '
    Private strFile As String
    Private strLines() As String
    Private strElements() As String
    Private intLine As Integer
    
    Private Sub GetData()
    Dim intFile As Integer
    '
    ' Obtain a free File Number
    ' and open the input file
    '
    
    intFile = FreeFile
    Open strFile For Input As intFile
    '
    ' Read the entire contents of the file
    ' close it and split into records
    '
        strFile = Input(LOF(intFile), intFile)
    
        
    Close intFile
    strLines = Split(strFile, vbNewLine)
    '
    ' strLines(0) contains Line 1 of the file
    ' strLines(1) contains Line 2 of the file
    ' etc
    ' strLines(UBound(strLines)) contains last line of the file
    '
    End Sub
    
    Private Sub cmdRefresh_Click()
    Call GetData
    End Sub
    
    Private Sub cmdSaveData_Click()
    '
    ' Save the data back to the original file
    '
    Dim strData As String
    Dim intFile As Integer
    intFile = FreeFile
    Open "c:\test.txt" For Output As intFile
    '
    ' Create the output from the strLines array
    ' and write it to the file
    '
    strData = Join(strLines, vbNewLine)
    Print #intFile, strData;
    Close intFile
    End Sub
    
    Private Sub cmdSelectData_Click()
    intLine = CInt(InputBox("Which Line do you want to change?"))
    strElements = Split(strLines(intLine - 1), ",")
    '
    ' strElements(0) = data1 from the selected line
    ' strelements(1) = data2 from the selected line
    ' strElements(2) = data3 from the selected line
    ' Display the data in the TextBoxes
    '
    Text1.Text = strElements(0)
    Text2.Text = strElements(1)
    Text3.Text = strElements(2)
    End Sub
    
    Private Sub cmdChange_Click()
    '
    ' Put the new data back into the strLines array
    '
    strLines((intLine - 1)) = Text1.Text & "," & Text2.Text & "," & Text3.Text
    End Sub
    
    
    Private Sub Form_Load()
    '
    ' Get the data from the file
    '
    strFile = "C:\test.txt"     ' The full path to, and name of your file
    Call GetData
    End Sub

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Write / Read to a specific line in text

    I guess I should have said that it wasn't tested - sorry

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    32

    Re: Write / Read to a specific line in text

    Don't be sorry my friend. I already adopted your code to my program and it works perfectly. You also helped me to improve my VB6. Many thanks again.

Tags for this Thread

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