Hide or ***** what is being typed in an input box

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
Is there a way to enter this without seeing what is typed in?

If InputBox("Enter Password", "Password") <> pwd Then
MsgBox ("Incorrect Password!")
Exit Sub

It works well, it just shows in the input box what is being typed.
 
Hi,

do you really have the complete Code in a standard module?

Please post all your code you have in your Worbook.

Thank u.
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This is what i put in the standard module:

Option Explicit

'////////////////////////////////////////////////////////////////////
'Password masked inputbox
'Allows you to hide characters entered in a VBA Inputbox.
'
'Code written by Daniel Klann
'March 2003
'////////////////////////////////////////////////////////////////////


'API functions to be used
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 'A window has been activated

RetVal = GetClassName(wParam, strClassName, lngBuffer)

If Left$(strClassName, RetVal) = "#32770" Then 'Class name of the Inputbox

'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


Then I selected view code on the button and entered:

Option Explicit

Private Sub CommandButton1_Click()
Dim pwd As String
pwd = "test"
If InputBoxDK("Enter Password", "Password") <> pwd Then
MsgBox ("Incorrect Password!")
Exit Sub
Else
ActiveSheet.Unprotect pwd
With ActiveCell
.Value = InputBox("Enter Time")
.Locked = True
End With
ActiveSheet.Protect Password:=pwd
End If
End Sub
 
Upvote 0
I have the same code in a standard module. When I placed your code as I modified it in the ClickButton event, it worked perfectly.

lenze
 
Upvote 0
Hi, you cant have two lines with Option Explicit in the same module. Delete the second line with that just before the Private Sub CommandButton1_Click() function.

If the CommandButton1_Click code is in regards to a control on a sheet then this code should not be in the standard module but rather in the sheet module of the sheet concerned. First select the button on the sheet in design mode then double click it - this will create an empty CommandButton1_Click procedure so just copy the middle bits out of the code from the standard module then delete the CommandButton1 code in the standard module.

If you copied the code to a sheet sometimes it doesnt work as Excel loses track of the relationship between the control even though the names correct.

hth
 
Upvote 0
Parry Wrote
Hi, you cant have two lines with Option Explicit in the same module. Delete the second line with that just before the Private Sub CommandButton1_Click() function.
Nice catch Parry. I didn't notice that in his code. Also, the code I ran, and I assume fridgenep's as well. was run from a Control CommandButton in the WorkSheet module. I'm not sure what kind of button Micheld is using.

lenze
 
Upvote 0
Thank You for all responses given. I really do appreciate it.
I think I am going to retire the idea... I have done exactly as you said Parry.
It gives the same compile error every time I try.

I have heard of doing this creating a form. Does any of you have any insight to this?

Michael
 
Upvote 0
I didn't see this code in your post of the code you actually tried. Maybe I missed it.

Code:
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
 
Upvote 0
Hi, Ive sent you a PM with my email address. If you want you can send me the workbook and I may be able to see the problem.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top