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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do While loop...problem!

Status
Not open for further replies.

sparky777

Technical User
Apr 21, 2001
3
US
i would be very grateful if any body could help me out with this problem as it has been boggling my brains for the last week!

i am only a beginner, but very keen to learn.

Scenario..

i am working on a project for an assignment which is for A- level, the assignment is fairly large but am having trouble with the first stage.
i have split the program into different forms, so am working on each stage individually, then will connect together using global variables where needed.

Problem..

The first stage is a password verification, there are five companies each with a specific customer code and a password. If the password is incorrect i need the program to loop three times ( then an error message to appear at the fourth error of login ie..program is terminated) if the password and code is correct then (form2.show)

The coding i have..

Private Sub Form_Load()
Label1.Caption = "UserName"
Label2.Caption = "Password"
Command1.Caption = "Login"
Command2.Caption = "Cancel"

LoginSucceeded = False
pass = 0
End Sub

Sub Command1_Click()
Dim customer As String
Dim password As String
Dim pass As Integer
pass = 1

If customer = "10010" And password = "january" Then form2.Show

If customer = "10011" And password = "february" Then form2.Show

If customer = "10012" And password = "march" Then form2.Show

If customer = "10013" And password = "april" Then form2.Show

If customer = "10014" And password = "may" Then form2.Show

Do While pass <= 3
If Text2.Text = &quot;Password&quot; Then
LoginSucceeded = True
pass = 0
Else
MsgBox &quot;Invalid Password, try again!&quot;, , &quot;Login&quot;
txtPassword.SetFocus
pass = pass + 1
If pass > 3 Then MsgBox &quot;Alert Intruded Intruder&quot;
End If
Loop
End Sub


The main error at debug is the section...txtPassword.SetFocus(this is shown red in the coding) i think i am almost there but may need another variable or object of some kind..

Please if you can help..i will buy you a beer!

thanks.
 
txtPassword appears to NOT be a control on the form at the time of occurance of this code. It is PROBABLY a control on the form, but until the form is fully loaded, VB is not going to know (all of) the controls names.

In other lines of code you use the declared variables &quot;customer&quot; and &quot;password&quot;, but do not show any method for asigning any values to them. The 'SUSPICION' here is that you have multiple problems, and these are just the 'tip of the iceberg'.

Fundamentaly, it is hard to envision how this process is going to accomplish much of anything in a Form_Load module. Something somewhere needs to be allowed to load and accept user input before you go off attempting to validate that input.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Your loop is wrong. Go read it carefully and you will see. You are just showing a message box three times, I expect you want the person to have three goes at entering a password. So

If customer = &quot;10010&quot; And password = &quot;january&quot; Then form2.Show

If customer = &quot;10011&quot; And password = &quot;february&quot; Then form2.Show

If customer = &quot;10012&quot; And password = &quot;march&quot; Then form2.Show

If customer = &quot;10013&quot; And password = &quot;april&quot; Then form2.Show

If customer = &quot;10014&quot; And password = &quot;may&quot; Then form2.Show

If Text2.Text = &quot;Password&quot; Then
LoginSucceeded = True
pass = 0
Else
label1.caption = &quot;Invalid Password, try again!&quot;
txtPassword.SetFocus

If pass > 3 Then MsgBox &quot;Alert Intruded Intruder&quot;
End If


If you are having problems setting focus when the form gets focus, then could you just stick the warning in a label as I have shown.

Is this homework due tomorrow, first day back at school after Easter hols?
Peter Meachem
peter@accuflight.com
 
Also

You need to take this out of command1 click
Dim pass As Integer
pass = 1

Instead put this
Dim pass As Integer
in the form general declarations bit, then all the code will be able to see it.

Peter Meachem
peter@accuflight.com
 
Private Sub Form_Load()
Label1.Caption = &quot;UserName&quot;
Label2.Caption = &quot;Password&quot;
Command1.Caption = &quot;Login&quot;
Command2.Caption = &quot;Cancel&quot;

LoginSucceeded = False

End Sub

Sub Command1_Click()
Static pass as Integer
Dim customer As String
Dim password As String
Dim pass As Integer
pass = pass + 1

If pass <= 3 Then
MsgBox &quot;Alert Intruded Intruder&quot;
Else
Select Case customer
Case &quot;10010&quot;
if password = &quot;january&quot; Then LoginSucceeded = True
Case &quot;10011&quot;
if password = &quot;february&quot; Then LoginSucceeded = True
Case &quot;10012&quot;
if password = &quot;march&quot; Then LoginSucceeded = True
Case &quot;10013&quot;
if password = &quot;april&quot; Then LoginSucceeded = True
Case &quot;10014&quot;
if password = &quot;may&quot; Then LoginSucceeded = True
End Select
If LoginSucceeded then
pass = 0
form2.show
Else
MsgBox &quot;Invalid Password, try again!&quot;, , &quot;Login&quot;
txtPassword.SetFocus
End If
End If
End Sub
 
Not quite right Tim, you are defining pas in the sub and then adding 1, so it will always be 1.

Another little bug in my solution,

If customer = &quot;10014&quot; And password = &quot;may&quot; Then form2.Show

If Text2.Text = &quot;Password&quot; Then

between these lines add exit sub

Peter Meachem
peter@accuflight.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top