LostFocus Event help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randi

    LostFocus Event help

    Hi All,
    I have a problem on a username password login form. I use:
    txtPassword.Tex t = StrConv(txtPass word.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(Ap p.Path & "\testlogin.mdb ")
    Set rs = db.OpenRecordse t("login")

    Do While Not rs.EOF
    If txtUserName.Tex t = "Guest" And txtPassword.Tex t = "Guest" Then
    Guest.Show
    Exit Sub
    End If
    If rs.Fields("user name") = (txtUserName.Te xt) And _
    rs.Fields("pass word") = (txtPassword.Te xt) Then
    Form1.Show
    Unload Me
    Exit Sub
    Else
    rs.MoveNext
    End If
    Loop
    txtPassword.Tex t = ""
    MsgBox "Incorrect Password!", vbCritical
    End Sub
    Private Sub txtPassword_Los tFocus() <--------Here
    txtPassword.Tex t = StrConv(txtPass word.Text, vbProperCase)
    End Sub

    Private Sub txtUserName_Los tFocus()
    txtUserName.Tex t = StrConv(txtUser Name.Text, vbProperCase)
    End Sub


    Private Sub frmLogin_Load()
    Data1.DatabaseN ame = (App.Path & "\testlogin.mdb ")
    Data1.RecordSou rce = "login"
    End Sub


  • BeastFish

    #2
    Re: LostFocus Event help

    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_Key Press(KeyAscii As Integer)
    If KeyAscii = 13 Then
    KeyAscii = 0 ' bye bye beep
    txtPassword.Set Focus
    End If
    End Sub

    Private Sub txtPassword_Key Press(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" <RSaddler@stny. rr.com> wrote in message
    news:R9TCb.1178 4$JW3.8837@twis ter.nyroc.rr.co m...[color=blue]
    > Hi All,
    > I have a problem on a username password login form. I use:
    > txtPassword.Tex t = StrConv(txtPass word.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[/color]
    event[color=blue]
    > apparently doesnt lose focus and it says you entered the wrong password.[/color]
    Is[color=blue]
    > there an easy fix for this. I pointed out the problem code below. Any[/color]
    help[color=blue]
    > 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(Ap p.Path & "\testlogin.mdb ")
    > Set rs = db.OpenRecordse t("login")
    >
    > Do While Not rs.EOF
    > If txtUserName.Tex t = "Guest" And txtPassword.Tex t = "Guest" Then
    > Guest.Show
    > Exit Sub
    > End If
    > If rs.Fields("user name") = (txtUserName.Te xt) And _
    > rs.Fields("pass word") = (txtPassword.Te xt) Then
    > Form1.Show
    > Unload Me
    > Exit Sub
    > Else
    > rs.MoveNext
    > End If
    > Loop
    > txtPassword.Tex t = ""
    > MsgBox "Incorrect Password!", vbCritical
    > End Sub
    > Private Sub txtPassword_Los tFocus() <--------Here
    > txtPassword.Tex t = StrConv(txtPass word.Text, vbProperCase)
    > End Sub
    >
    > Private Sub txtUserName_Los tFocus()
    > txtUserName.Tex t = StrConv(txtUser Name.Text, vbProperCase)
    > End Sub
    >
    >
    > Private Sub frmLogin_Load()
    > Data1.DatabaseN ame = (App.Path & "\testlogin.mdb ")
    > Data1.RecordSou rce = "login"
    > End Sub
    >
    >[/color]


    Comment

    • Bob Butler

      #3
      Re: LostFocus Event help

      "Randi" <RSaddler@stny. rr.com> wrote in message news:<R9TCb.117 84$JW3.8837@twi ster.nyroc.rr.c om>...[color=blue]
      > Hi All,
      > I have a problem on a username password login form. I use:
      > txtPassword.Tex t = StrConv(txtPass word.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.[/color]

      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.

      Comment

      • Steve Gerrard

        #4
        Re: LostFocus Event help


        "Bob Butler" <butlerbob@eart hlink.net> wrote in message
        news:fa10fb0.03 12140710.786efa d4@posting.goog le.com...[color=blue]
        > "Randi" <RSaddler@stny. rr.com> wrote in message[/color]
        news:<R9TCb.117 84$JW3.8837@twi ster.nyroc.rr.c om>...[color=blue][color=green]
        > > Hi All,
        > > I have a problem on a username password login form. I use:
        > > txtPassword.Tex t = StrConv(txtPass word.Text, vbProperCase) to[/color][/color]
        validate the[color=blue][color=green]
        > > proper case when someone enters their name and password. It work[/color][/color]
        when you[color=blue][color=green]
        > > use the button to continue, but if you hit enter on your keyboard[/color][/color]
        the event[color=blue][color=green]
        > > apparently doesnt lose focus and it says you entered the wrong[/color][/color]
        password.[color=blue]
        >
        > 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.[/color]

        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.



        Comment

        Working...