Results 1 to 4 of 4

Thread: Changing the saved info in a Text File

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    39

    Exclamation Changing the saved info in a Text File

    Let's say for example, some info's been saved using this code:

    Open "C:\Textfile.txt" For Append As #3
    Write #3, Text1, Text2, Combo1.text, List1.text
    Close #3

    and the text file is something like:
    Code:
    "James", "16", "Green", "1990"
    "Bob", "20", "Red", "1980"
    How can change an specific info in the text file? (For example, the user enters "Bob" as his name in my program and changes "Red" to something else in a combobox. So i need the computer to find "Bob" in the text file and change "Red" to something the user enters in my program.)

    If any more info is required, tell me and i will add.

    Asap & thnk you
    Last edited by incognito_301; Apr 17th, 2008 at 07:20 PM.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    265

    Re: Changing the saved info in a Text File

    Please attach the file

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

    Re: Changing the saved info in a Text File

    With files that are sequentially written and with variable length records, you'll have to read all the file, search for "Bob", make the necessary change and then re-write the complete file. If you have many records it's easier to use a simple Database.

  4. #4
    Addicted Member ThorSubak's Avatar
    Join Date
    Apr 2008
    Location
    Cebu
    Posts
    153

    Re: Changing the saved info in a Text File

    here's the way...

    vb Code:
    1. Dim toAddLine As String 'the string to be replace
    2.     Dim dNewStr(0 To 3) As String 'can be adjusted : for the as if field
    3.     Dim dLineSplit(0 To 1) As String 'as a storage of the text file data per row
    4.     Dim nF As Integer 'for the filenumber
    5.     Dim arrey() As String 'for split
    6.     nF = FreeFile
    7.     Open "D:\samp.txt" For Input As #nF
    8.     w = 0
    9.         Do Until EOF(nF)
    10.             Line Input #nF, dLineSplit(w)
    11.             w = w + 1
    12.         Loop
    13.     Close #nF
    14.     nF = FreeFile
    15.     Open "D:\samp.txt" For Output As #nF
    16.         For y = 0 To UBound(dLineSplit)
    17.             arrey = Split(Trim(dLineSplit(y)), ",")
    18.             For x = 0 To UBound(arrey)
    19.                     If Trim(arrey(x)) = Me.Text_param.Text Then
    20.                         toAddLine = Me.Text1.Text 'the new value
    21.                     Else: toAddLine = arrey(x)
    22.                     End If
    23.                     dNewStr(x) = Replace(toAddLine, Chr(34), "")
    24.             Next
    25.            Write #nF, dNewStr(0), dNewStr(1), dNewStr(2), dNewStr(3)
    26.             dNewStr(0) = "" 'clear all arrays
    27.             dNewStr(1) = ""
    28.             dNewStr(2) = ""
    29.             dNewStr(3) = ""
    30.         Next
    31.     Close #nF
    32.  
    33.     'must have:
    34.       'Text_param - the parameter of value to be update
    35.       'Text1 - the new value

    __________________________
    if do you think the problem is solved, please mark your post as RESOLVED and please do rate.
    Last edited by ThorSubak; Apr 18th, 2008 at 05:01 AM.

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