For a school project i am attempting to create a register and login section of a program. i have managed to create the register by writing user names and passwords to a text document, as we cannot use access databases. however in order to do the actual login i have no idea on how to search the text file for a specific password. All my inputs are in text boxes, and i was wondering if anyone could be any help.
Searching for a specific word in a text file using VB6
Collapse
X
-
Put the text from the file with the name and passwords in an array (total text).
Split each array input (total text) into an second array.
Find in this second array the name and password.
See attachment -
this is my code
Option Explicit
Dim ArrayPassword() As String
Private Sub Form_Load()
Dim FileIn As Integer
Dim inputdata As String
ReDim ArrayPassword(0 )
FileIn = FreeFile
On Error GoTo Error_Laden
Open "\\MAM-INET\aimee.burn s$\SDD\Year12\p roject\password s.txt" For Input As #FileIn
Do While Not EOF(FileIn)
ReDim Preserve ArrayPassword(U Bound(ArrayPass word) + 1)
Input #FileIn, inputdata
ArrayPassword(U Bound(ArrayPass word)) = inputdata
Loop
Close #FileIn
Exit Sub
Error_Laden:
Close #FileIn
MsgBox ("Error loading !")
End Sub
Private Sub Command1_Click( )
Dim ArrPasswordIdx As Integer
Dim ArrayPWLine() As String
Dim Found As Boolean
Found = False
For ArrPasswordIdx = LBound(ArrayPas sword) + 1 To UBound(ArrayPas sword)
ReDim ArrPwline(0)
ArrayPWLine = Split(ArrayPass word(ArrPasswor dIdx), " ")
If ArrayPWLine(0) = txtUsername.Tex t Then
txtpassword.Tex t = ArrayPWLine(1)
Found = True
End If
Next
If Found = False Then
txtpassword.Tex t = ""
MsgBox "Not a valid name"
End If
End Sub
however i keep getting "subscript out of range" errors
the code ive used for writing the username and password to a file is
Private Sub cmdRegister_Cli ck()
Dim EmployeeName As String
EmployeeName = txtUsername
Open "\\MAM-INET\aimee.burn s$\SDD\Year12\p roject\Password s.txt" For Append As #1
Dim Password As Integer
Password = Int(Rnd * 1000) + 1 'Generate number
MsgBox "Your pass number is" & Password
Print #1, EmployeeName, Password
Close #1
frmmanagelogin. Show
Unload Me
End Sub
Private Sub Form_Load()
End Sub
im using random no. generator cause its part of our program specifications. any idea on how to fix this?Comment
-
Your code=
sets a TAB between name and password while the textline is split with a SPACE=Code:Print #1, EmployeeName, Password
The TAB as separator gives "subscript out of range" by splitting the line.Code:ArrayPWLine = Split(ArrayPassword(ArrPasswordIdx), " ")
It must be:
Code:Print #1, EmployeeName & " " & Password
If You use RND, You must also initialize the generator with: Randomize (see attachment) (see also help of VB6)
If it still not work on Your PC, please attach Your VB6 project (Zipped) in BYTES.
PS !! I have put some lines in comment !! it's for doing some tests !! Your code is not wrong !!!Comment
Comment