Inputbox Password

tobeon

Board Regular
Joined
Jan 26, 2003
Messages
81
how can i make an input box display *s when you type in it (so it can be used for passwords)

InputBox("Please Enter New Password")

thank you
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I think ive had this problem before and had to use a form instead. You can set the passwordchar field in the properties of a text box on a form.

Chris
 
Upvote 0
Hi,

Which version of Excel are you using? If you're using 2000 or XP then you can use this code I recently wrote. Paste this in a standard module:-

Code:
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

Function InputBoxDK(Prompt, Title) 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)
    UnhookWindowsHookEx hHook

End Function

To use this code just try something like this:-

Code:
Sub Test()
    Dim x
    x = InputBoxDK("Type your password here.", "Password Required")

    If x <> "yourpassword" Then
        MsgBox "Youd didn't enter a correct password."
    End If

End Sub

Hope that helps you mate. If you use Excel 97 post back and I'll try to amend my code to suit :)
 
Upvote 0
hi
it needs to be 97 compatible unfortunatly

if you could amed it so it will work with versions 97 and up

thanks v much
 
Upvote 0
Mudface said:
I would imagine there's probably a good reason why DK's code is for 2000 and XP and not for 97.

Yes there is. It's this line in InputBoxDK

hHook = SetWindowsHookEx(WH_CBT, AddressOf NewProc, lngModHwnd, lngThreadID)

The [/I]AddressOf[/i] operator didn't come along until XL2000. There's a workaround in 97 but I can't think of it right now (mullered :) )
 
Upvote 0
Upvote 0
is there any reason why this ouwldnt work on Excel XP ?

I keep getting "Argument not Optional" on the "InputBoxDK" line in my password code!!!

Code:
Private Sub CommandButton3_Click()

Retry:
CODEPASSWORD = "PASSWORD"
Dim PassAttempt As String
PassAttempt = InputBox("Please Enter the password to Edit")

    'if the password that is entered is not correct then diplay the following message to the user
    If PassAttempt <> CODEPASSWORD Then
        If MsgBox("The password you specified was wrong!" & vbCrLf & vbCrLf & "Would you like to try again?", vbCritical + vbRetryCancel, "Incorrect Password Entered") = vbRetry Then GoTo Retry
        Exit Sub
    End If
    
    'on the chance the user keys the correct password then display the followng message
    If PassAttempt = CODEPASSWORD Then
        MsgBox "Access to Edit Allowed", vbOKOnly + vbInformation, "Correct"
        Exit Sub
    End If
    
End Sub

Please help and tell me I have missed something really obvious... Oh and the other code is in a normal module...

Thanks
 
Upvote 0
:0) Thank yuo firstly for the last post you made DK and not only that but this incredible bit of code that replicates what Microsoft should have already implemented into VBA !!!

Thanks again fella :0)

Pal

:beerchug:
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,633
Members
452,661
Latest member
Nonhle

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