469,306 Members | 1,977 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

LostFocus Event help

Hi All,
I have a problem on a username password login form. I use:
txtPassword.Text = StrConv(txtPassword.Text, vbProperCase) to validate the
proper case when someone enters their name and password. It work when you
use the button to continue, but if you hit enter on your keyboard the event
apparently doesnt lose focus and it says you entered the wrong password. Is
there an easy fix for this. I pointed out the problem code below. Any help
would be appreciated.

Thanks,
Kelsey

Option Explicit

Public LoginSucceeded As Boolean

Private Sub cmdCancel_Click()
'set the global var to false
'to denote a failed login
LoginSucceeded = False
Unload Me
End Sub

Private Sub cmdOK_Click()
Dim db As Database
Dim rs As DAO.Recordset

Set db = OpenDatabase(App.Path & "\testlogin.mdb")
Set rs = db.OpenRecordset("login")

Do While Not rs.EOF
If txtUserName.Text = "Guest" And txtPassword.Text = "Guest" Then
Guest.Show
Exit Sub
End If
If rs.Fields("username") = (txtUserName.Text) And _
rs.Fields("password") = (txtPassword.Text) Then
Form1.Show
Unload Me
Exit Sub
Else
rs.MoveNext
End If
Loop
txtPassword.Text = ""
MsgBox "Incorrect Password!", vbCritical
End Sub
Private Sub txtPassword_LostFocus() <--------Here
txtPassword.Text = StrConv(txtPassword.Text, vbProperCase)
End Sub

Private Sub txtUserName_LostFocus()
txtUserName.Text = StrConv(txtUserName.Text, vbProperCase)
End Sub
Private Sub frmLogin_Load()
Data1.DatabaseName = (App.Path & "\testlogin.mdb")
Data1.RecordSource = "login"
End Sub
Jul 17 '05 #1
3 9172
Are the textboxes bound? Do you have any code behind the text boxes'
KeyPress event that you didn't show here? I ask because you said it throws
an invalid password when you press enter. The LostFocus event won't fire
until that control loses focus (who'd a thunk) when another control gets
focus.

If you wanted to, you can turn the enter key into a forward tab by
specifying which control to set focus to when the enter key is detected in
the textboxes' respective KeyPress events. Something like...

Private Sub txtUserName_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0 ' bye bye beep
txtPassword.SetFocus
End If
End Sub

Private Sub txtPassword_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0 ' bye bye beep
cmdOK.SetFocus
End If
End Sub

Or to simplify it, you can just do this in each KeyPress event...
If KeyAscii = 13 Then
KeyAscii = 0 ' bye bye beep
SendKeys "{TAB}"
End If

"Randi" <RS******@stny.rr.com> wrote in message
news:R9******************@twister.nyroc.rr.com...
Hi All,
I have a problem on a username password login form. I use:
txtPassword.Text = StrConv(txtPassword.Text, vbProperCase) to validate the
proper case when someone enters their name and password. It work when you
use the button to continue, but if you hit enter on your keyboard the event apparently doesnt lose focus and it says you entered the wrong password. Is there an easy fix for this. I pointed out the problem code below. Any help would be appreciated.

Thanks,
Kelsey

Option Explicit

Public LoginSucceeded As Boolean

Private Sub cmdCancel_Click()
'set the global var to false
'to denote a failed login
LoginSucceeded = False
Unload Me
End Sub

Private Sub cmdOK_Click()
Dim db As Database
Dim rs As DAO.Recordset

Set db = OpenDatabase(App.Path & "\testlogin.mdb")
Set rs = db.OpenRecordset("login")

Do While Not rs.EOF
If txtUserName.Text = "Guest" And txtPassword.Text = "Guest" Then
Guest.Show
Exit Sub
End If
If rs.Fields("username") = (txtUserName.Text) And _
rs.Fields("password") = (txtPassword.Text) Then
Form1.Show
Unload Me
Exit Sub
Else
rs.MoveNext
End If
Loop
txtPassword.Text = ""
MsgBox "Incorrect Password!", vbCritical
End Sub
Private Sub txtPassword_LostFocus() <--------Here
txtPassword.Text = StrConv(txtPassword.Text, vbProperCase)
End Sub

Private Sub txtUserName_LostFocus()
txtUserName.Text = StrConv(txtUserName.Text, vbProperCase)
End Sub
Private Sub frmLogin_Load()
Data1.DatabaseName = (App.Path & "\testlogin.mdb")
Data1.RecordSource = "login"
End Sub

Jul 17 '05 #2
"Randi" <RS******@stny.rr.com> wrote in message news:<R9******************@twister.nyroc.rr.com>.. .
Hi All,
I have a problem on a username password login form. I use:
txtPassword.Text = StrConv(txtPassword.Text, vbProperCase) to validate the
proper case when someone enters their name and password. It work when you
use the button to continue, but if you hit enter on your keyboard the event
apparently doesnt lose focus and it says you entered the wrong password.


Neither the LostFocus nor Validate events are reliable IMO because
they do not fire consistently. Clicking toolbar buttons, menu items
and pressing enter to trigger the default command button are just a
few cases where they don't happen. You can use them for simple
validation if you want but never assume that they will run. In this
case, have the command button code do the StrConv.
Jul 17 '05 #3

"Bob Butler" <bu*******@earthlink.net> wrote in message
news:fa*************************@posting.google.co m...
"Randi" <RS******@stny.rr.com> wrote in message news:<R9******************@twister.nyroc.rr.com>.. .
Hi All,
I have a problem on a username password login form. I use:
txtPassword.Text = StrConv(txtPassword.Text, vbProperCase) to validate the proper case when someone enters their name and password. It work when you use the button to continue, but if you hit enter on your keyboard the event apparently doesnt lose focus and it says you entered the wrong

password.
Neither the LostFocus nor Validate events are reliable IMO because
they do not fire consistently. Clicking toolbar buttons, menu items
and pressing enter to trigger the default command button are just a
few cases where they don't happen. You can use them for simple
validation if you want but never assume that they will run. In this
case, have the command button code do the StrConv.


I would say the events are reliable, but that controls don't lose focus
in some situations where you might expect them to. I agree that you
should never assume they will be fired, and always put required
processing in a procedure that will definitely run.

LostFocus doesn't fire here because in fact, the textbox never loses
focus. If the default button only popped up a message, focus would
return to the textbox, not the default command button. Similarly,
Validate doesn't fire because it only fires just before a possible loss
of focus.

In many cases, it makes sense that selecting a menu or toolbar button
does not change the focus. If I click the Paste button, for instance, I
appreciate the fact that the focus stays where it is, and the paste
occurs where I intended it.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by vooose | last post: by
4 posts views Thread by Bernard Bourée | last post: by
4 posts views Thread by Wayne Wengert | last post: by
reply views Thread by Scott McChesney | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.