473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 9382
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MJW | last post by:
Is there a way for me to know if or which command button was just clicked that triggers the LostFocus event for the current control on a Form? I have a form that has many types of users who each...
2
by: vooose | last post by:
Does anyone know the difference between these two? The doc for LostFocus says 'Occurs when the control loses focus.' whereas for Leave it says 'Occurs when the input focus leaves the control'...
4
by: Bernard Bourée | last post by:
I have some TextBoxes created by code. I want to write a procedure to handle the lost of focus of any of these TextBox. How should I do that ? Thanks --
4
by: Wayne Wengert | last post by:
I have a form that has a cbo set to TabIndex 0 (it is the only item set to tabindex 0). On page Load if I step through the code that cbo LostFocus event fires after other initializations are...
2
by: Paul | last post by:
I have a datagrid tied to a data adapter. Everything works great, EXCEPT, the LostFocus event only fires when I click on a specific cell within the DataGrid. If I set focus on another control,...
0
by: Scott McChesney | last post by:
All - I am in the process of developing a custom control that mimics the Office 2003 "Inbox" as closely as possible. After a great deal of frustrating work trying to do a heavily custom-drawn...
6
by: Carmon | last post by:
I am using Managed C++ with a CFormView and a MaskedTextBox and have used MAKE_DELEGATE to sink the LostFocus event of this control. I have other events I've sinked off this control and they all...
14
by: teddysnips | last post by:
WINDOWS FORMS I've a form that has a textbox that allows the user to enter a string. On the LostFocus event, the textbox formats the string into a preferred format. However, if the user...
4
by: dbuchanan | last post by:
On the help page "TextBox Members" clearly there is an event for LostFocus. It is inherited from control. So without the event directly available for TextBox how do I use it? I know I could...
0
by: kirk | last post by:
I started with a System.Windows.Forms.Form and subscribed to its LostFocus event. The event fires perfect. I added a System.Windows.Forms.LinkLabel to the aforementioned Form and tested the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.