Set PasswordChar for an InputBox entry

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Is there a way to set a passwordChar for an InputBox entry?

I have been digging around for a while now. Can't seem to find anything viable.

I want whatever i type into the InputBox look like asterisk just entering a password.

Any help is appreciated
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I am doing that but when I close that form it seems to close the parent form too. How will I stop it from closing the parent form but close only the login form?

Regards
 
Upvote 0
Hard to say without seeing your code. But you shouldn't be 'closing' the form, you should be using a command button that performs an action if the password is entered correctly.
 
Upvote 0
Hi,
I recall someone about 10 years ago publishing code to do what you want (Ivan Moala I think) but cannot find anything at moment.

As already suggested, easier to build a Userform & define the require password Char

Dave
 
Upvote 0
Code:
Option Explicit
 
 '////////////////////////////////////////////////////////////////////
 'Password masked inputbox
 'Allows you to hide characters entered in a VBA Inputbox.
 '
 'Code written by Daniel Klann
 'http://www.danielklann.com/
 'March 2003
 
 '// Kindly permitted to be amended
 '// Amended by Ivan F Moala
 '// [URL]http://www.xcelfiles.com[/URL]
 '// April 2003
 '// Works for Xl2000+ due the AddressOf Operator
 '////////////////////////////////////////////////////////////////////
 
 '********************   CALL FROM FORM *********************************
 '    Dim pwd As String
 '
 '    pwd = InputBoxDK("Please Enter Password Below!", "Database Administration Security Form.")
 '
 '    'If no password was entered.
 '    If pwd = "" Then
 '        MsgBox "You didn't enter a password!  You must enter password to 'enter the Administration Screen!" _
 '        , vbInformation, "Security Warning"
 '    End If
 '**************************************
 
 
 
 '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
 
 '// Make it public = avail to ALL Modules
 '// Lets simulate the VBA Input Function
Public Function InputBoxDK(Prompt As String, Optional Title As String, _
    Optional Default As String, _
    Optional Xpos As Long, _
    Optional Ypos As Long, _
    Optional Helpfile As String, _
    Optional Context As Long) As String
     
    Dim lngModHwnd As Long, lngThreadID As Long
     
     '// Lets handle any Errors JIC! due to HookProc> App hang!
    On Error GoTo ExitProperly
    lngThreadID = GetCurrentThreadId
    lngModHwnd = GetModuleHandle(vbNullString)
     
    hHook = SetWindowsHookEx(WH_CBT, [B]AddressOf NewProc[/B], lngModHwnd, lngThreadID)
    If Xpos Then
        InputBoxDK = InputBox(Prompt, Title, Default, Xpos, Ypos, Helpfile, Context)
    Else
        InputBoxDK = InputBox(Prompt, Title, Default, , , Helpfile, Context)
    End If
     
ExitProperly:
    UnhookWindowsHookEx hHook
     
End Function
 
Sub TestDKInputBox()
    Dim x
     
    x = InputBoxDK("Type your password here.", "Password Required")
    If x = "" Then End
    If x <> "1234" Then
        MsgBox "You didn't enter a correct password."
        End
    End If
     
    MsgBox "Welcome Creator!", vbExclamation
     
End Sub


I found this code and when I run it it says type mismatch and highlights "AddressOf NewProc"
 
Upvote 0
A suggested by the other members, I would use a userform instead of the API based approach .

I am doing that but when I close that form it seems to close the parent form too. How will I stop it from closing the parent form but close only the login form?
Show us your userform(s) code(s) ?

I found this code and when I run it it says type mismatch and highlights "AddressOf NewProc"

Are you using a 32bit or 64bit version of excel and Windows ?
 
Upvote 0
A suggested by the other members, I would use a userform instead of the API based approach .


Show us your userform(s) code(s) ?



Are you using a 32bit or 64bit version of excel and Windows ?


I am using 64 bit both Windows and excel. Excel version is 2016

I have added the PtrSafe to match the 64 bit. I just don't know why it is still failing.
 
Upvote 0
This code is for the login form
Code:
Private Sub CmdLogin_Click()
    With frmMainForm.MultiPage1
    Dim UserPins$: UserPins = txtPassWrd
    Select Case UserPins
        Case Sheets("Users").[C2], LCase(Sheets("Users ").[B2])
            Dim m, Y&
            Set m = CreateObject("WScript.Shell")
                Y = m.popup("Access granted to : " & LCase(.Pages(Index).Caption), 1, "Login required")
            Set m = Nothing
                Select Case Index
                    Case 0: .Value = 0: OfficialLoginFrm.Hide
                    Case 1: .Value = 1: OfficialLoginFrm.Hide
                    Case 2: .Value = 2: OfficialLoginFrm.Hide
                End Select
        Case Else: .Value = 4: init = 0: OfficialLoginFrm.Hide
            MsgBox "Access Denied. Try again", vbExclamation, "Failed alert"
    End Select
    End With
    txtPassWrd = ""
End Sub




This code does the calling of the login userform
Code:
Private Sub MultiPage1_Click(ByVal Index As Long)
Static init As Boolean
Static PreviousIndex&
If Not init Then init = 1: PreviousIndex = -1
    With MultiPage1
    Select Case Index
    Case 0 To 2
    If Index <> PreviousIndex Then
        OfficialLoginFrm.Show ‘ this is where I used to call the InputBox from. Those code are now found under the login user form
    End If
    End Select
        PreviousIndex = Index
    End With
End Sub
 
Upvote 0
Can you upload a workbook example to a file sharing site ?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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