Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
LostFocus Event help
Old
  (#1)
Randi
Guest
 
Posts: n/a
Default LostFocus Event help - 06-04-2007, 08:57 AM

Hi All,
I have a problem on a username p***word login form. I use:
txtP***word.Text = StrConv(txtP***word.Text, vbProperCase) to validate the
proper case when someone enters their name and p***word. 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 p***word. 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 txtP***word.Text = "Guest" Then
Guest.Show
Exit Sub
End If
If rs.Fields("username") = (txtUserName.Text) And _
rs.Fields("p***word") = (txtP***word.Text) Then
Form1.Show
Unload Me
Exit Sub
Else
rs.MoveNext
End If
Loop
txtP***word.Text = ""
MsgBox "Incorrect P***word!", vbCritical
End Sub
Private Sub txtP***word_LostFocus() <--------Here
txtP***word.Text = StrConv(txtP***word.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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: LostFocus Event help
Old
  (#2)
BeastFish
Guest
 
Posts: n/a
Default Re: LostFocus Event help - 06-04-2007, 08:57 AM

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 p***word 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
txtP***word.SetFocus
End If
End Sub

Private Sub txtP***word_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" <EMAIL REMOVED> wrote in message
news:R9TCb.11784$EMAIL REMOVED...
> Hi All,
> I have a problem on a username p***word login form. I use:
> txtP***word.Text = StrConv(txtP***word.Text, vbProperCase) to validate the
> proper case when someone enters their name and p***word. 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 p***word.

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 txtP***word.Text = "Guest" Then
> Guest.Show
> Exit Sub
> End If
> If rs.Fields("username") = (txtUserName.Text) And _
> rs.Fields("p***word") = (txtP***word.Text) Then
> Form1.Show
> Unload Me
> Exit Sub
> Else
> rs.MoveNext
> End If
> Loop
> txtP***word.Text = ""
> MsgBox "Incorrect P***word!", vbCritical
> End Sub
> Private Sub txtP***word_LostFocus() <--------Here
> txtP***word.Text = StrConv(txtP***word.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
>
>



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: LostFocus Event help
Old
  (#3)
Bob Butler
Guest
 
Posts: n/a
Default Re: LostFocus Event help - 06-04-2007, 08:57 AM

"Randi" <EMAIL REMOVED> wrote in message news:<R9TCb.11784$EMAIL REMOVED>.. .
> Hi All,
> I have a problem on a username p***word login form. I use:
> txtP***word.Text = StrConv(txtP***word.Text, vbProperCase) to validate the
> proper case when someone enters their name and p***word. 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 p***word.


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 ***ume that they will run. In this
case, have the command button code do the StrConv.
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: LostFocus Event help
Old
  (#4)
Steve Gerrard
Guest
 
Posts: n/a
Default Re: LostFocus Event help - 06-04-2007, 08:57 AM


"Bob Butler" <EMAIL REMOVED> wrote in message
news:EMAIL REMOVED m...
> "Randi" <EMAIL REMOVED> wrote in message

news:<R9TCb.11784$EMAIL REMOVED>.. .
> > Hi All,
> > I have a problem on a username p***word login form. I use:
> > txtP***word.Text = StrConv(txtP***word.Text, vbProperCase) to

validate the
> > proper case when someone enters their name and p***word. 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

p***word.
>
> 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 ***ume 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 ***ume 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.



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top