We help IT Professionals succeed at work.

Access VBA show a textbox password for 4 seconds

Murray Brown
Murray Brown used Ask the Experts™
on
Hi. I tried to show the password in a text box for 4 seconds with the following Access VBA code but it doesn't work

Private Sub btnEye_Click()

      txtPW.InputMask = ""
      
      PauseApp (4)

      txtPW.InputMask = "Password"
      
End Sub

Public Sub PauseApp(PauseInSeconds As Single)
    
    Dim sngStart As Single
    
    sngStart = Timer
    
    Do While sngStart + PauseInSeconds > Timer
    Loop
    
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NoahHardware Tester and Debugger

Commented:
Hi there! :)

Just to confirm, the password is already hidden by default using the Input Mask property?
Software & Systems Engineer
Commented:
Change your code like this
Private Sub btnEye_Click()

      txtPW.InputMask = ""
      Me.Repaint
      PauseApp (4)

      txtPW.InputMask = "Password"
      
End Sub

Open in new window

I would recommend you use the sleep API instead. Looping to pause the code takes up pc resources and gives a un-responsive/sluggush interface.

In a public module declare the API like so:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Open in new window

And in your form:
Private Sub btnEye_Click()
      txtPW.InputMask = ""
      sleep 4000 'Sleep uses miliseconds
      txtPW.InputMask = "Password"
End Sub

Open in new window

You might need to combine this with either repaint as John suggested or DoEvents, or both, I'm not sure.

All that said and done, have you considered instead using the mousedown event to unhide the password and the mouseup to rehide it?
That way, the user can decide how long he wishes for the password to be visible.
NoahHardware Tester and Debugger
Commented:
You mat try something like this. You should assign the macro to a button. The macro will show and hide with only one click of the button. There is a timer to show the password for 4 seconds before automatically hiding it again. You need to replace all the "Text1" with the name of the textbox you want the button/macro to function on.

Private Sub btnShowPassword_Click()
  Dim dtWait As Date

  If Text1.InputMask = "Password" Then
    Text1.InputMask = ""
    dtWait = DateAdd("s", 4, Now) ' 4 Seconds
    Do Until Now > dtWait
       DoEvents
    Loop
    Text1.InputMask = "Password"
  End If

End Sub

Open in new window

Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012
Commented:
The critical point here is that you should do a .Repaint, which John showed.   Also have a DoEvents() in the wait loop or as Ander's pointed out, you can sleep, which is different in that the process actually goes to sleep (doesn't execute) for the 4 seconds.

Both these however can be problematic if you want the user to be able to do something in those four seconds as well.  The Do Events helps a bit because it forces the window queue to be processed (so keyboard input is processed) and the app will remain somewhat responsive.   But there's quite a bit of overhead there and your still yielding to the OS, which will make it appear sluggish.

A better technique you can use is to utilize the forms timer.   Set the timer interval for 1/10th or 1/2 a second and each time it fires off (Use the OnTimer event), check if four seconds have elapsed (you can use a global variable, a form property, or a control to hold the start time).   Once it has, you can shut off the timer by setting the interval to 0 and hide the password again.

That allows the form to be active and respond to input while the password is being shown.  Make sure you do use the .Repaint though at the start and end of the wait so the changes to the control are shown immediately.

Jim.
Mark EdwardsChief Technology Officer
Commented:
You didn't say if your textbox was bound to a password field in a table, so I'm going with.....no.
One of my pet peeves is seeing what I'm typing as I'm typing it instead of a string of ******.
The attached database file has a password textbox that only shows the user the password as they type it.
It also has a click event that momentarily displays the password (if there is one in the textbox).
You can set the decoded password visibility interval to however many seconds you want.

Play with it and see if it comes close to doing what you really want to do.  Granted, it allows those looking on to see the password, but that's what you are doing anyway, right?
If you just want to make it visible with a button click only, remove the code in the KeyUp event and put the code in the textbox click event into your button click event and adjust.
Display-PasswordTemporarily.accdb
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks everyone for the help