Inputbox to show *

Farah

Board Regular
Joined
Oct 4, 2005
Messages
98
i even don't know how to explain :cry: .... but let's try.

When i am entering a password in an inputbox it shows "mypassword". i want it to be "**********".


Hope i explained it correctly.

Kind Regards,
Farah
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi, Farah,

You need to create a userform with a textbox for this
in the code you can put this
Code:
Private Sub UserForm_initialize()
TextBox1.PasswordChar = "*"
End Sub

or you can set this in the properties of the textbox1

kind regards,
Erik
 
Upvote 0
Hello Farah,
While Erik's userform idea is probably the easier idea, you can indeed get an inputbox to show password characters.
I found this on DK's site quite a while ago and have been using it in several applications with no problem. (while other userforms are showing no less.)
The following goes into a standard module:
Code:
Option Explicit
'PROCEDURE TO MAKE INPUTBOXES SHOW PASSWORD CHARACTERS
'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 displays 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
And it can be called like so:
Code:
X = InputBoxDK("Enter password to continue.")
Hope it helps.
 
Upvote 0
just another approach
Code:
Option Explicit

Public mem As Variant

Sub test_password_form()
password_form
MsgBox "You entered " & """" & mem & """", 64, "PASSWORD"
End Sub

Sub password_form()
Dim MsgFrm  As Object
Dim TxtBox As MSForms.TextBox
Dim OKbtn As MSForms.CommandButton
Dim FrmCode As String

Set MsgFrm = ThisWorkbook.VBProject.VBComponents.Add(3)

With MsgFrm

.Properties("Width") = 120
.Properties("Height") = 72
.Properties("Caption") = "Please enter Password"

Set TxtBox = .Designer.Controls.Add("forms.TextBox.1")
With TxtBox
.Left = 6
.Top = 6
.Width = 102
.Height = 18
End With

Set OKbtn = .Designer.Controls.Add("forms.CommandButton.1")
With OKbtn
.Width = 48
.Height = 24
.Left = (MsgFrm.Properties("Width") - .Width) / 2
.Top = 30
.Caption = "OK"
End With

FrmCode = "Private Sub UserForm_Initialize()" & vbCrLf & "TextBox1.PasswordChar = ""*"" " & vbCrLf & "End Sub" & vbCrLf & _
"Private Sub " & OKbtn.Name & "_Click()" & vbCrLf & "mem = " & TxtBox.Name & vbCrLf & "Unload Me" & vbCrLf & "End Sub"

.CodeModule.AddFromString FrmCode
VBA.UserForms.Add(.Name).Show
End With

ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=MsgFrm
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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