Password chars on Inputbox

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi guys

I'm looking for a way of entering a password to ensure someone is entitled to run a mcaro. I've got the code sort for that, but the password is written on screen when you type it.
I've seen this thread :-
http://www.mrexcel.com/forum/showthread.php?t=308923
which hides the password using * but on a userform.
Can you do the same thing on an inputbox, or can someone suggest something similar without using userforms?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Richard, why not simply use

Rich (BB code):
If Environ("Username") = "andrewm" then
 
Your code here

or

Code:
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]Private Sub workbook_open()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]Dim x As String<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]x = Environ(“Username”)<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]Select Case x<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]    Case "andrewm", "gallagd"<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]        Application.ScreenUpdating = False<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]*<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]*<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]*<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]        Application.ScreenUpdating = True<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]    Case Else<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]        Exit Sub<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]End Select<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
[FONT=Verdana][SIZE=1][COLOR=black][COLOR=black][FONT=Verdana]End Sub<o:p></o:p>[/FONT][/COLOR][/COLOR][/SIZE][/FONT]
 
Upvote 0
A solution with API:
Code:
'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
 Dim 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
   Public Function InputBoxDK(Prompt, Optional Title, Optional Default, Optional XPos, Optional YPos, Optional HelpFile, Optional Context) As String
   Dim lngModHwnd  As Long
   Dim 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

 Sub macro1()
 InputBoxDK "Please enter the password", "Info", "123456"
 End Sub

Best Regards
Northwolves
 
Upvote 0

Forum statistics

Threads
1,215,346
Messages
6,124,417
Members
449,157
Latest member
mytux

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