Inputbox - display text as asterisk

T

Tim Childs

Hi

please can someone give me the few lines of code required to produce an
inputbox for returning text (for a password) such that each character
entered by the user is shown as an asterisk in the inputbox.

thanks

Tim
 
O

Otto Moehrbach

Tim
Here are a couple of responses given in the past to the same question.
HTH Otto

Not possible. To get a masked password, you would need to create a
UserForm
with a TextBox control. Then you can set the PasswordChar property of
the
TextBox. It would also require some simple macros to display the form
and
act on the password.
John Walkenbach

I don't think this can be done. Any macro would not have control
until
after the user had finished typing, and the prying eyes peeking. You
can

use a text box from the Control toolbox. Set its Password character
property to an asterisk. That will cause asterisks to appear in place
of
characters the user types. Then it will require some code to do stuff
with
the password. And the text box can be hidden when not needed. Or
consider
putting the textbox on a form, instead of a worksheet.
Regards from Virginia Beach,
EarlK
 
J

Jake Marx

Hi Tim,

It's not possible with a standard InputBox. You would have to create a
UserForm, drop a TextBox onto it, and set its PasswordChar property to *.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
V

Vasant Nanavati

Hi Tim:

Set the PasswordChar property of the TextBox to "*".

Regards,

Vasantg.
 
V

Vasant Nanavati

Sorry; didn't read properly. You can't do this with an InputBox; only a
TextBox.
 
T

Tim Childs

Hi Vasant, Jake, Otto

many thanks for the above responses.

Incidentally, I did try using the archives before posting but was
unsuccessful.

Tim
 
Joined
Dec 10, 2014
Messages
1
Reaction score
0
Hi Vasant, Jake, Otto

many thanks for the above responses.

Incidentally, I did try using the archives before posting but was
unsuccessful.

Tim

"Vasant Nanavati" <vasantn *AT* aol *DOT* com> wrote in message
news:#[email protected]...
> Sorry; didn't read properly. You can't do this with an InputBox; only a
> TextBox.
>
> --
>
> Vasant
>
> "Tim Childs" <[email protected]> wrote in message
> news:[email protected]...
> > Hi
> >
> > please can someone give me the few lines of code required to produce an
> > inputbox for returning text (for a password) such that each character
> > entered by the user is shown as an asterisk in the inputbox.
> >
> > thanks
> >
> > Tim
> >
> >
> >

>
>
Found one link where this problem got sorted out - congratulations
Just paste the MODULE section code under one module and see the magic

help from the website:-experts-exchange


Sub Sample()
Dim Prompt, password As String
Prompt = InputBoxDK("Please enter you password.")
End Sub


create one module and paste the below code

Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long

Private Declare Function GetModuleHandle Lib "kernel32" Alias _
"GetModuleHandleA" (ByVal lpModuleName As String) As Long

Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Private Declare Function SendDlgItemMessage Lib "user32" Alias "SendDlgItemMessageA" _
(ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long

'~~> Constants to be used in our API functions
Private Const EM_SETPASSWORDCHAR = &HCC
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
Private Const HC_ACTION = 0

Private hHook As Long

Public Function NewProc(ByVal lngCode As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim RetVal
Dim strClassName As String, lngBuffer As Long

If lngCode < HC_ACTION Then
NewProc = CallNextHookEx(hHook, lngCode, wParam, lParam)
Exit Function
End If

strClassName = String$(256, " ")
lngBuffer = 255

If lngCode = HCBT_ACTIVATE Then
RetVal = GetClassName(wParam, strClassName, lngBuffer)
'~~> Class name of the Inputbox
If Left$(strClassName, RetVal) = "#32770" Then
'~~> This changes the edit control so that it display the password character *.
'~~> You can change the Asc("*") as you please.
SendDlgItemMessage wParam, &H1324, EM_SETPASSWORDCHAR, Asc("*"), &H0
End If
End If

'~~> This line will ensure that any other hooks that may be in place are
'~~> called correctly.
CallNextHookEx hHook, lngCode, wParam, lParam

End Function

Public Function InputBoxDK(Prompt, Optional Title, Optional Default, Optional XPos, _
Optional YPos, Optional HelpFile, Optional Context) As String
Dim lngModHwnd As Long, lngThreadID As Long
lngThreadID = GetCurrentThreadId
lngModHwnd = GetModuleHandle(vbNullString)
hHook = SetWindowsHookEx(WH_CBT, AddressOf NewProc, lngModHwnd, lngThreadID)
InputBoxDK = InputBox(Prompt, Title, Default, XPos, YPos, HelpFile, Context)
UnhookWindowsHookEx hHook
End Function



 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top