[RESOLVED] how to stop it from writing the same details
The most basic registration form :), if for example I run this and enter
username1 and password1
then do that again it will display in txt file as
"username1","password1"
"username1","password1"
Is there an If statement I could use?, so if the username exists then exit sub :?
Code:
Dim savefile As Long
savefile = FreeFile()
Open "D:\a.txt" For Append As #savefile
Write #savefile, (Text1.Text); (Text2.Text)
Close #savefile
Re: how to stop it from writing the same details
It gets written twice because you are Appending, use Output instead.
Open "D:\a.txt" For Output As #savefile
Re: how to stop it from writing the same details
Quote:
Originally Posted by Edgemeal
It gets written twice because you are Appending, use Output instead.
Open "D:\a.txt" For Output As #savefile
when i use output it only writes 1 line, so cant have multiple logins
Re: how to stop it from writing the same details
Then you really have no choice but to search the file to see if it already exists or not.
Re: how to stop it from writing the same details
And you would probably want to check to see if the username and password you are about to write already exists in the file, or it would get duplicated.
Re: how to stop it from writing the same details
Quote:
Originally Posted by Hack
And you would probably want to check to see if the username and password you are about to write already exists in the file, or it would get duplicated.
manualy or using vbcode?
Re: how to stop it from writing the same details
You are writing a program. There is no point in doing anything manually.
Re: how to stop it from writing the same details
Re: how to stop it from writing the same details
Re: how to stop it from writing the same details
I have a login for my application, and they way I do it is I store all the usernames and passwords in a txt file, and at the start of my program, I load all the usernames into a string array, and all the passwords in another string array. That way anytime I need to do something with the usernames and passwords they're all right there to search through.
Code:
strBats = App.Path & "\bats\"
intFileNum = FreeFile
intIndex = 0
Open strBats & "users.sun" For Input As intFileNum
Do While EOF(intFileNum) = False
ReDim Preserve strUser(intIndex), strPassword(intIndex)
Input #intFileNum, strUser(intIndex), strPassword(intIndex)
intIndex = intIndex + 1
Loop
Close #intFileNum
Then when making changes (ie adding) to the users, you need only to make the change to the string arrays, then open the file for OUTPUT and write the arrays back in:
Code:
Open strBats & "users.sun" For Output As intFileNum
For intIndex2 = LBound(strUser()) To UBound(strUser())
Write #intFileNum, strUser(intIndex2), strPassword(intIndex2)
Next intIndex2
Close #intFileNum
The only issue with doing it this way is to work with the username and passwords together, the indexes of the two arrays must stay the same. The username stored in the 4th index corresponds to the password stored in the 4th index. Gotta be careful not to change them in your code.
Hope I could help!
Re: how to stop it from writing the same details
I wonder why this does not work?, it was code that I used for login form,
Code:
Dim uname As String
Dim is_matched As Boolean
If (Trim(uname) = (Text1.Text)) Then
MsgBox "username exists"
Else
MsgBox "username does not exist"
is_matched = False
Open "G:\College Work ------- Updated 2008 March\new-work\savedfile.txt" For Input As #1
Do While Not EOF(1)
Input #1, uname
If Trim(uname) = Trim(Text1.Text) Then
is_matched = True
Close #1
Exit Do
End If
Loop
Close #1
Ofcourse i modified it, setup a new form for tests, not working how I wanted,
When I type username in text1 (lets say one that exists in txt file), it still says username does not exist :S, hmm, I still remain testing this, until someone can put me right :)
Re: [RESOLVED] how to stop it from writing the same details
I would just load the usernames into an array, then when the user types their username do something like:
Code:
'Load users
Dim intFileNum as Integer, intIndex as Integer
Dim strUsers() as string
intfileNum = FreeFile
open [file] for input as intfilenum
do while eof(intfilenum) = false
redim preserve strUsers(intIndex)
Input #intfilenum, strUsers(intindex)
intIndex = intIndex + 1
Loop
'Check Username
for intIndex = lbound(strUsers()) to ubound(strUsers())
if strUsers(intIndex) = txtUser.text then
msgbox "User found!"
'Your Code
Else
msgbox "User Not Found!"
end if
Next intIndex