Then there is the code that executes when the user clicks on
the OK button:-

Run your login box dialog and play with it for a bit.
Notice that you can enter any username you choose but the password you have
to enter is the word password. I.e. This is from the line:-
If txtPassword
= "password" Then
You will see a few problems we need to correct.
-
The Cancel button works fine but the
OK button just hides the Login dialog. It
doesn't display the main application form.
-
Only the password is checked, the code does
not check to see if the username is correct too.
First of all, let's fix it so that if the user inputs the
correct password, then the login dialog is closed and the main application
form is loaded.
First check line
19. You might not have a frmTroubleShooting to show.
Change this name to the name of the from you want to show next.
Here is an explanation of the code:-
Line 2:
Public LoginSucceeded As
Boolean
This line declares a Boolean variable called
LoginSucceeded. If this variable has a value of false
it means that the Login information the user typed in was incorrect.
If it has a value of true it means that the Login information
the user typed in was correct.
~~~~~~ user clicks on the
Cancel button ~~~~~~
Line 3:
Private Sub cmdCancel_Click()
This block of code from
Private Sub cmdCancel_Click() to End Sub
runs if the user clicks on the Cancel button.
Line 4:
LoginSucceeded = False
This line sets the LoginSucceeded
variable to false because the user clicked cancel.
Line 5:
Unload Me
Since the user clicked the Cancel button,
may as well unload the form.
Line 17:
Private Sub Form_Unload(Cancel As Integer)
This block of code from
Private Sub Form_Unload(Cancel As Integer) to
End Sub runs if the form is unloaded.
Line 18:
If LoginSucceeded = True
Then
Check to see if the variable LoginSucceeded
has a value of true or false. Since the
user clicked on the Cancel button it will have a value of
false. Therefore the next line that loads the main
application form will not be executed.
~~~~~~ user clicks on the OK button
~~~~~~
Line 7:
Private Sub cmdOK_Click()
This block of code from
Private Sub cmdOK_Click() to End Sub
runs if the user clicks the OK button.
Line 8:
If txtPassword = "password"
Then
LoginSucceeded = True
Unload Me
This first line checks to see if the text the user
entered in the txtPassword textbox matches the word
password. If it does match, the next two lines are
executed. The LoginSucceeded variable is set to
true because the user typed in the correct password.
The Login Dialog is unloaded. Then
Line 17 will be run as the form is
unloaded.
Line 10:
Else
MsgBox "Invalid Password, try again!", , "Login"
If the user entered an incorrect password, a message box
is displayed telling them.
Although this may seem like a lot of code to
understand, a good way to try an understand is to put different break points
in your code and to step through each line one at a time.
Now we need to fix the code so it also checks for a
username.
Change you line of code...
If txtPassword =
"password" Then
to
If txtPassword =
"password" AND txtUserName = "user"
Then
Now it will check both the username and password match.