Sign in with
Sign up | Sign in
Your question

Help With Visual Basic Please

Last response: in Applications
Share
February 18, 2013 11:26:03 PM

I'm trying to make this login but I'm having some trouble. This is my code so far. I put this code into the login button:

Private Sub loginbutton_Click(sender As Object, e As EventArgs) Handles loginbutton.Click
1 Dim InputUser As String
2 Dim InputPass As String
3 Dim Correctu As String
4 Dim Correctp As String
5 InputUser = txtLogin.Text
6 InputPass = txtLoginPassword.Text
7 Correctu = "Daniel"
8 Correctp = "apple"
9 If InputUser = Correctu Then islogincorrect.Text = "Login is Correct!"
10 If InputPass = Correctp Then islogincorrect.Text = "Login is Correct!" Else
11 If InputUser <> Correctu Then
12 islogincorrect.Text = "Login is Not Correct!"
13 End If
14
15 End Sub


Im trying to make it so that if the username and password match what they are supposed to be, then it will say login is correct, and if not display login is not correct. InputUser is the username box. InputPass is the password box. The Correctu is just saying what the correct username should be and Correctp is saying what the correct password should be. I'm having trouble somewhere on lines 9 down saying that if the username and password are what they are supposed to be, it would say login correct but thats not working. So if sombody could please help me that would be great. Thanks!

More about : visual basic

February 19, 2013 2:28:25 AM

You care correct on where the problem starts. You are asking two independent questions in those statements.

1) is InputUser = Daniel. If so, "Correct"
2) is InputPass = apple. If so, "Correct"

As you have noticed, you only have to answer one of those correctly in order to get a "Correct" result. You want BOTH of them to be correct. You need to "And" the two statements.

  1. If InputUser = Correctu And InputPass = Correctp Then
  2. 'Both user and password are correct
  3. isLoginCorrect.Text = "Login is Correct!"
  4. Else
  5. 'One or both are not correct
  6. isLoginCorrect.Text = "Login is Not Correct!"
  7. End If


Another way to simplify a couple of lines of code is to specify the values of your variables when you declare them:

  1. Dim Correctu as String = "Daniel"


You could eliminate all of your variables all together if you wanted. There are many ways to do it, I was just sharing some different examples. In the end you want to do what makes sense and is easy to follow. Also, you can never use too many comments!!!!

  1. If txtLogin.Text = "Daniel" And txtLoginPassword.Text = "apple" Then
  2. 'Both user and password are correct
  3. isLoginCorrect.Text = "Login is Correct!"
  4. Else
  5. 'One or both are not correct
  6. isLoginCorrect.Text = "Login is Not Correct!"
  7. End If
m
0
l
!