Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Compare Variable to Textbox Value

Status
Not open for further replies.

milcman

Programmer
Dec 19, 2002
31
0
0
US
I am a VB.NET newbie used to VBA. I am trying to figure out how to compare a variable to the value input into a textbox.

I tried declaring the variable and setting it's value, and used nested If/Then/Else statements to compare the textbox values to the variable. Even when the proper values are input, it still evaluates to fail.

Below is the code that I have written so far:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim strUsername As String = "me"
Dim strPassword As String = "me2"

shtAttempts = shtAttempts + 1

If txtUsername Is strUsername Then
If txtPassword Is strPassword Then
MessageBox.Show("Access Granted!!")

End If
Else
If shtAttempts = 3 Then
MessageBox.Show("ACCESS DENIED!!!!!!!!!!!!!!")
Application.Exit()
Else
MessageBox.Show("The User ID / Password combination you entered is incorrect. You have a total of 3 chances to enter the proper information before being locked out.]")

End If

End If
End Sub

Clint Galliano
Halliburton Energy Services
BAROID PSL
"Done ONCE, Done RIGHT!"
 
you use the is word to compare objects not values. So do this

Code:
 If txtUsername.text = strUsername Then
  If txtPassword.text = strPassword Then

I'm not sure what the results will be if you set the textbox to password so it hides the letters.

Christiaan Baes
Belgium

"My new site" - Me
 
I think the code must be altered a little
Code:
     Public shtAttempts As Integer = 0
'==============================================
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strUsername As String = "me"
        Dim strPassword As String = "me2"
        If Not shtattempts = 3 Then
            shtattempts = shtattempts + 1

            If txtUsername.Text = strUsername Then
                If txtPassword.Text = strPassword Then
                    MessageBox.Show("Access Granted!!")
                    'do what you want here
                End If
                If shtAttempts = 3 Then
                    MessageBox.Show("ACCESS DENIED!!!!!!!!!!!!!!")
                    Application.Exit()
                Else
                    MessageBox.Show("The User ID / Password combination you entered is incorrect." & vbCrLf & _
                    "You have a total of 3 chances to enter the proper information before being locked out.")

                End If
            End If
        End If
    End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top