Sie sind auf Seite 1von 3

I've used VB6 for 9 years.

I'll post the code for how I would do it, however I'm sure there
is more than just one way to create a simple login system. It'll just take a couple changes
to the kitty's code to implement other users.

We can copy the first part of kitty's code right into the form almost without changes. Here
it is:
Dim MyName As String ' This variable contains users full name
Dim MyUName As String ' This variable contains users login name
(username)
Dim MyPass As String ' This variable contains users password
Dim Response As Integer ' This variable contains users answers from
message boxes
Dim Saved_Name() As String ' This variable contains saved username from
file
Dim Saved_Pass() As String 'This variable contains saved password from
file

The only real difference between my code and his/her's is that Saved_Name and
Saved_Pass are now arrays. Arrays can hold more than one value, each value in an array
is given a number.
Dim TotUser As Integer 'The total number of users
Dim TempName As String 'For the name
Dim TempPass As String 'For the pass
TotUser = 1
Open "pass.txt" For Input As #1 'we open file with data we need
Do Until EOF(1)
Line Input #1, TempName ' We read username from file
Line Input #1, TempPass ' We read password from file
TotUser = TotUser + 1 'Add one to TotUser
ReDim Preserve Saved_Name(1 To TotUser) 'Expand the array
Saved_Name(TotUser) = TempName
ReDim Preserve Saved_Pass(1 To TotUser) 'Expand the array
Saved_Pass(TotUser) = TempPass
Loop
Close #1 'we close the file

The ReDim statement resizes the array (creates more elements). The Preserve in the
ReDim statement means to make sure the data originally in the array stays there. Without
the preserve statement, only the last username and password would be in the array, all the
others would be "" or NULL.

The next bit of code stays the same.


Login_Name:
MyName = InputBox("Please, enter Your name.", "Introduce yourself", "")
If MyName = "" Then
Response = MsgBox("To continue with the program, You need to introduce
Yourself." & vbCrLf & "Do You wish to proceed with the programm?",
vbCritical + vbYesNo, "Error")
If Response = vbYes Then GoTo Login_Name
If Response = vbNo Then
MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We
wish You a good day", vbInformation + vbOKOnly, "End"
End
End If
End If

So does the next bit

Login_UName:
MyUName = InputBox("Good day " & MojeIme & ", please, enter Your user
name.", "Login", "")
If MyUName = "" Then
Response = MsgBox("To continue with the program, You need to enter
username." & vbCrLf & "Do You wish to proceed with the programm?",
vbCritical + vbYesNo, "Error")
If Response = vbYes Then GoTo Login_UName
If Response = vbNo Then
MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We
wish You a good day", vbInformation + vbOKOnly, "End"
End
End If
End If

and the next bit


Login_Pass:
MyPass = InputBox("Please, enter a password for user name " & MyUName &
".", "Login", "")
If MyPass = "" Then
Response = MsgBox("To continue with the program, You need to enter
Your password." & vbCrLf & "Do You wish to proceed with the programm?",
vbCritical + vbYesNo, "Error")
If Response = vbYes Then GoTo Login_Pass
If Response = vbNo Then
MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We
wish You a good day", vbInformation + vbOKOnly, "End"
End
End If
End If

Finally something actually changes!!


Dim i As Integer 'For the For Next Loop
For i = 1 To TotUser 'Check every user.
If (Saved_Name = MyUName(i)) And (Saved_Pass = MyPass(i)) Then
MsgBox "Good day " & MyName & "." & vbCrLf & "Welcome to our program,
we wish You pleasant work.", vbInformation + vbOKOnly, "Welcome"
Exit sub 'You are logged in!
Else
If i = TotUser Then
MsgBox "We are sorry, username and/or password are not correct",
vbCritical + vbOKOnly, "Error"
End
End If
End If
Next i

Place all this code together and it will work fine! Feel free to PM me with any questions
you may have.

Das könnte Ihnen auch gefallen