Access VBA text shows as null on key down

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

I am trying to allow a user to log into my Access solution when they hit enter
I use the following event but for some reason the password that was typed into txtPW
shows as Null

Private Sub txtPW_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
        Call oLogin
    End If
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer

Commented:
Shouldn't Ologin have some arguments (username/password)
Anyway put a breakpoint and check exactly at what control you are pointing and what value it holds
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
In the procedure I gather the text in txtPW and even though there is text in the text box it shows as Null. Is this something to do with the KeyDown event?
John TsioumprisSoftware & Systems Engineer

Commented:
Just test it before calling the keydown..maybe the control doesn't looses the focus and Enter clears the password.
Just read the txtPW ..store it in a variable and check again.
Also After Update is a good event for this case
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
Can you show the code for oLogin?
Is the form bound? Im guessing you are moving on to the next "new and blank" record.

Instead, create a button, and have the login cod in that button. Then look at the properties of that button, find the one called "Default" and set it to true. This means this is the default button when the users presses Enter, and that code will run.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
If your oLogin fetches the password from txtPW, that will fail, as it has no value at this point.

You may have to pass it as an argument to oLogin:

Call oLogin(Me!txtPW.Text)

Open in new window

Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
I was using the After_Update evet but it conflicts with the following code that I use to show the password for some reason

Private Sub btnEye_Click()
 
   Dim dtWait As Variant
   If txtPW.InputMask = "Password" Then
        txtPW.InputMask = ""
        dtWait = DateAdd("s", 2, Now) ' 2 Seconds
        Do Until Now > dtWait
           DoEvents
        Loop
        txtPW.InputMask = "Password"
  End If
End Sub
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Then remove that.
You should never - as in never - display a password.
Chief Technology Officer
Commented:
Try using the textbox's .Text property in the KeyUp() event.  During KeyDown(), Keypress(), and KeyUp() events, there are differences in the value of txtPassword (txtPassword.Value default property) and txtPassword.Text during these events as they tell Access that the content of the control is in the process of being edited.
Keep in mind that the textbox has to have the focus to be able to read the .Text property, so you may want to write the .Text value to a variable that your login procedure uses, instead of trying to read the textbox directly.

Also, you have to decide if you really want to display the password as you wanted to do in your previous post where you got the code for the Afterupdate event.  As Gustav said, it is best practices to NEVER display a password.  However, if you don't care about best practices and are hell-bent on doing so as you originally wanted to, then you'll need to keep your Afterupdate event code.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks Mark. That worked for me

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial