Thanks Thanks:  0
Likes Likes:  0
Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Inputbox Password

  1. #1
    Board Regular
    Join Date
    Jan 2003
    Location
    Wales UK
    Posts
    81
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Inputbox Password

    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

  2. #2
    Board Regular
    Join Date
    Aug 2002
    Location
    Newcastle
    Posts
    382
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  3. #3
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    2,940
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  4. #4
    Board Regular
    Join Date
    Jan 2003
    Location
    Wales UK
    Posts
    81
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  5. #5
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sunny, spring-like Hull
    Posts
    3,339
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I would imagine there's probably a good reason why DK's code is for 2000 and XP and not for 97.

    Juan Pablo G's solution is here http://www.mrexcel.com/board2/viewto...=16886&forum=2

  6. #6
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    2,940
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by Mudface
    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 )

  7. #7
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by Mudface
    I would imagine there's probably a good reason why DK's code is for 2000 and XP and not for 97.

    Juan Pablo G's solution is here http://www.mrexcel.com/board2/viewto...=16886&forum=2
    There is indeed

    Xl97 does not support the Addressof function, and it is not the
    easiest to simulate.

    DK, great code....if you don't mind I can use this on my Site


    Thanks DK, I have updated the code and put a version for Xl97
    here;

    http://www.xcelfiles.com/API_09.html
    Kind Regards,
    Ivan F Moala From the City of Sails

  8. #8
    Board Regular
    Join Date
    Jul 2004
    Location
    UK
    Posts
    208
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Inputbox Password

    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

  9. #9
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    2,940
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Inputbox Password

    Hi,

    You need to specify a title in this particular code. There's a more up-to-date version of the code here : http://www.danielklann.com/excel/hid...a_inputbox.htm

    HTH
    Dan

  10. #10
    Board Regular
    Join Date
    Jul 2004
    Location
    UK
    Posts
    208
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Inputbox Password

    :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


Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •