Click to See Complete Forum and Search --> : storing names to a .txt file
ariel_au
April 13th, 2001, 05:03 AM
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".
Cimperiali
April 13th, 2001, 05:59 AM
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.
ariel_au
April 13th, 2001, 06:31 AM
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.
Iouri
April 13th, 2001, 06:59 AM
Instead of text file you can create an .INI file and keep your names there
Iouri Boutchkine
iouri@hotsheet.com
Cimperiali
April 13th, 2001, 07:14 AM
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.
ariel_au
April 15th, 2001, 05:47 AM
thanks. your method works
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.