Computing.Net > Forums > Programming > vb question

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

vb question

Reply to Message Icon

Original Message
Name: jlittle
Date: April 10, 2005 at 16:41:20 Pacific
Subject: vb question
OS: win xp pro
CPU/Ram: 2.6ghz p4/512mb
Comment:

Simple at least to most of the people here. Anyway I need to write a simple password validation program. The password is set in the code. My problem is that after 3 attempts at the pass the program is supposed to shut down. I am having problems setting a counter. After 3 attempts it just keeps letting me enter a password. Here is the code I have for the program. Any suggestions would help this noob greatly. Thanks.


Dim pass As Integer
Dim valid As String

valid = "xyz"
If Me.txtUser.Text = "" Then
MsgBox "enter a username"
Me.txtUser.SetFocus
End If
If CStr(Me.txtPass.Text) = "xyz" Then
MsgBox "access granted"

End If
If CStr(Me.txtPass.Text) <> "xyz" Then
MsgBox "invalid password"
Me.txtPass.Text = ""
Me.txtPass.SetFocus

pass = pass + 1
If pass > 3 Then
MsgBox " you lose, do not pass go do not collect two hundred dollars"

End If
End If


Report Offensive Message For Removal


Response Number 1
Name: bcalvanese
Date: April 10, 2005 at 18:27:12 Pacific
Reply: (edit)

Dim pass As Integer
Dim valid As String

These need to be declared in the declaration ssection or the form load section. By putting them in the On_Click event you are re dimming and setting them to 0 and "xyz" every time you click on the button.

Try this code in the On_Click event...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
valid = "xyz"
If Me.txtUser.Text = "" Then
MsgBox("enter a username")
Me.txtUser.Focus()
End If
If CStr(Me.txtPass.Text) = "xyz" Then
MsgBox("access granted")
End If
Select Case (CStr(Me.txtPass.Text))
Case "xyz"
MsgBox("access granted")
Case Else
If pass < 3 Then
MsgBox("invalid password")
Me.txtPass.Text = ""
Me.txtPass.Focus()
pass = pass + 1
Else
MsgBox(" you lose, do not pass go do not collect two hundred dollars")
End
End If
End Select
End Sub

I used VB.NET so you may have to tweek the syntax a little. Like Focus() I believe is SetFocus in prior versions.

Hope this helps...
Bob


Report Offensive Follow Up For Removal

Response Number 2
Name: jlittle
Date: April 10, 2005 at 18:48:24 Pacific
Reply: (edit)

Thanks alot bob that helps greatly.


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software