CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2001
    Posts
    44

    storing names to a .txt file

    i created a form called "EnterName.frm", inside this form there is a textbox called "txtName" and a command button called "cmdOK".

    well my problem is how can i store the name input by the user in the textbox to a text file called "namelist.txt" upon the cmdOk is clicked?

    the file "namelist.txt" will always store names whenever different users input their names in the textbox "txtName".




  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: storing names to a .txt file

    to add string to a file:
    dim freenum as integer
    freenum = freefile
    open "path/filename.extension" for append as #freenum
    print #freenum, text1.text
    close #freenum
    Now, before adding, you should check if string already exist, so open the file for input (open "path/filename.extension" for input as #freenum), use the lineinput statement to achieve one line at a time (inside of a loop till you find EOF = true to read the whole file) and if you find what you read is equal to what you have in text1.text then close the file and exit the sub. Otherwise, if no match is found, just use the code above.
    Hope this help.


    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Mar 2001
    Posts
    44

    Re: storing names to a .txt file

    ok, i understand that u wanted to check if the input name in textbox is already exist in the text file before being added into the text file.
    can show me the codes pls? thank you.


  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: storing names to a .txt file

    Instead of text file you can create an .INI file and keep your names there

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: storing names to a .txt file

    Although Iouri is right (ini is better solution), if you still want to go on with text file, then:
    private sub command1_click
    Dim freenum As Integer
    Dim myvar As String
    screen.mousepointer =vbhourglass
    freenum = FreeFile
    Open "path\filename.extension" For Input As #freenum
    Do While EOF(freenum) = False
    Line Input #freenum, myvar
    If Trim(myvar) = Trim(text1.Text) Then
    Close #freenum
    screen.mousepointer =vbdefault
    Exit Sub
    End If
    Loop
    Close #freenum
    freenum = FreeFile
    Open "path\filename.extension" For Append As #freenum
    Print #freenum, Trim(text1.Text)
    Close #freenum
    screen.mousepointer =vbdefault
    end sub

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Mar 2001
    Posts
    44

    Re: storing names to a .txt file

    thanks. your method works


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

Featured