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!

Messagebox/Login window focus problem

Status
Not open for further replies.

xuanb

Programmer
Apr 9, 2003
29
US
I am a PB newbie. I want to create a Login window and check for username/pw and display the appropriate error message. If incorrect clear the SingleLineEdit field and set the focus to enter a new username/pw. Here's my code:

//Local variables
String ls_usnInput, ls_pwInput, crs_name, crs_pw
Boolean lb_loginOK = false

DO
ls_usnInput = Trim (sle_username.text)
ls_pwInput = Trim (sle_password.text)

DECLARE Login CURSOR FOR
SELECT username, password
FROM Users
WHERE username = :ls_usnInput
USING LADSTrans;

OPEN Login;
FETCH Login INTO :crs_name, :crs_pw;
CLOSE Login;

crs_name = Trim (crs_name)
crs_pw = Trim (crs_pw)

If crs_name <> ls_usnInput OR ls_usnInput = &quot;&quot; Then
Messagebox (&quot;Login Error&quot;, &quot;Please enter a valid user name.&quot;)
sle_username.SelectText(1, Len(sle_username.Text))
sle_username.Clear( )
sle_username.setfocus()
ElseIf crs_pw <> ls_pwInput OR ls_pwInput = &quot;&quot; Then
Messagebox (&quot;Login Error&quot;, &quot;Please enter a valid password.&quot;)
sle_password.SelectText(1, Len(sle_password.Text))
sle_password.Clear( )
sle_password.Setfocus( )
Else
lb_loginOK = true
End If
LOOP UNTIL lb_loginOK = TRUE

Close (parent);


What currently happens is an infinite looping of the Messagebox because I can't get back to the Login window's SingleLineEdit to enter a new username. Can any1 help plz?










 
Take out the DO loop and add a Return statement inside the IF statements after sle_password.SetFocus(). The script will execute only once and will not process anything beyond the &quot;Return&quot; if it encounters an error.

Also, it seems to me that you don't really need the CURSOR to test for the existence of a user/pwd in the users table. If you select the data from the table and the SLQCA.SQLCode = 0, then the data is in the table and you have a valid uid/pwd combination.

<*** Code snippet ***>
SELECT username, password
INTO crs_name, crs_pwd
FROM Users
WHERE username = :ls_usnInput
USING LADSTrans;

IF SQLCA.SQLCode = 100 THEN
// Error msg goes here - username not found
sle_password.SetFocus()
Return
ELSEIF SQLCA.SQLCode = -1 THEN
// Error msg goes here - database error include
// (SQLCA.ErrText)
sle_password.SetFocus()
Return
END IF
<*** end code snippet ***>

Another problem I see is that your first IF statment will never be TRUE. You are selecting from the users table based on the value ls_usnInput. If the username is not found, the NULL value will be returned into crs_name. And eventhough it might make sense that NULL <> &quot;myusername&quot; should evaluate to TRUE, it doesn't. You need to use the IsNull() function is that case.
 
I tried your code snippet and modified my sql statement to WHERE username = :ls_usnInput AND password = :ls_pwInput. That part is working swell!

To improve user friendliness, how would i check for the pw separately and return focus to the pw field if the uid is correct so the user doesn't have to retype both.

Thank you so much for your code and advice!
 
Take the password out of the WHERE clause and add an ELSE clause after the ELSEIF SQLCA.SQLCode...

In the ELSE clause use an IF statement to check the value of crs_pwd against ls_pwInput.

Glad I could help!

Rob
 
Thanks again Rob! Just what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top