Masking Password in input box


Posted by Michael on February 03, 2002 6:18 AM

I use an input box to have users enter a password.
Is there any way to have their input masked as it is entered. I currently have input showing up as it is typed in but would prefer that it is converted to asterisks.

Posted by Jacob on February 03, 2002 4:48 PM

Hi

You cant do this with a input box, but a user form will do it fine. Just make a userform with a textbox and on the textbox properties there is a password char property just put * in there.

HTH

Jacob



Posted by Ivan F Moala on February 03, 2002 10:16 PM

This is a Q that comes up reguarly.
Here is a way to do this without you
creating a userform yourself....code should
be explainatory...........

Post if unsure;

Option Explicit
Public OK As Boolean
Public Const sMyPassWord As String = "test"

Function GetPassWord(Title As String)
'---------------------------------------------------------------------------------------
' Procedure : GetPassWord
' DateTime : 4/02/02 19:04
' Author : Ivan F Moala
' Purpose : Creates a Dynamic UF to Test for aPassword
' : so there is no need to create one.
'---------------------------------------------------------------------------------------
Dim TempForm
Dim NewTextBox As MSForms.TextBox
Dim NewCommandButton1 As MSForms.CommandButton
Dim NewCommandButton2 As MSForms.CommandButton
Dim x As Integer

' Hide VBE window to prevent screen flashing
Application.VBE.MainWindow.Visible = False

' Create a Temp UserForm
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)

' Add a TextBox
Set NewTextBox = TempForm.Designer.Controls.Add("forms.textbox.1")
With NewTextBox
.PasswordChar = "*"
.Width = 140
.Height = 20
.Left = 48
.Top = 18
End With

' Add the OK button
Set NewCommandButton1 = TempForm.Designer.Controls.Add("forms.CommandButton.1")
With NewCommandButton1
.Caption = "OK"
.Height = 18
.Width = 66
.Left = 126
.Top = 66
End With

' Add the Cancel button
Set NewCommandButton2 = TempForm.Designer.Controls.Add("forms.CommandButton.1")
With NewCommandButton2
.Caption = "Cancel"
.Height = 18
.Width = 66
.Left = 30
.Top = 66
End With

' Add event-handler subs for the CommandButtons & Userform
With TempForm.CodeModule
x = .CountOfLines
.insertlines x + 0, "Sub CommandButton2_Click()"
.insertlines x + 1, "OK = False: Unload Me"
.insertlines x + 2, "End Sub"

.insertlines x + 3, "Sub CommandButton1_Click()"
.insertlines x + 4, "If TextBox1 = sMyPassWord Then OK = True: Unload Me"
.insertlines x + 5, "End Sub"

.insertlines x + 6, "Private Sub UserForm_Initialize()"
.insertlines x + 7, "Application.EnableCancelKey = xlErrorHandler"
.insertlines x + 8, "End Sub"
End With

' Adjust the form
With TempForm
.Properties("Caption") = Title
.Properties("Width") = 240
.Properties("Height") = 120
NewCommandButton1.Left = 46
NewCommandButton2.Left = 126
End With

' Show the form
VBA.UserForms.Add(TempForm.Name).Show

' Delete the form
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm

' Pass the Variable back to the calling procedure
GetPassWord = OK

End Function

Sub ThisIsHowToUseIt()
'>>> This is the Main line <<<<br>Dim OKToProceed As Variant
OKToProceed = GetPassWord("Password Entry")
If OKToProceed = False Then End
'>>>-----------------------<<<<p>'>>> Your routine goes here <<<<p>MsgBox "My routine is running now"

End Sub

HTH


Ivan