Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using InputBox with Mask (password) 1

Status
Not open for further replies.

marreco7

Technical User
Jan 4, 2012
3
Hi..

Can anyone help me to enter the code below a mask inputbox password in the code that follows below .. thank you!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NewValue As Variant, OldValue As Variant
    If Target.Count > 1 Then Exit Sub
     'Say you want to work with the range A1:F100
    If Not Intersect(Target, Range("A1:F10")) Is Nothing Then
        NewValue = Target.Value
        Application.EnableEvents = False
        Application.Undo
        OldValue = Target.Value
        If OldValue = "" Then
    Target.Value = NewValue
   ElseIf InputBox("Caso tenha premissão entre com a senha", "Acesso restrito") = "ale" Then
    Target.Value = NewValue
Else: MsgBox "Entrada indevida, você não pode alterar o conteudo da celula.", 16, "Células Bloqueadas"
    Target.Value = OldValue
    End If
        Application.EnableEvents = True
    End If
End Sub
 
The standard way is to build a userform that imitate inputbox and have a textbox with a PasswordChar set to '*'. The input is stored in a global text variable.

combo
 
hi...thanks

Could you give me an example?
 
This is a piece of my code for unhiding sheets within a workbook, but the concepts are the same for any action that you want to take. Once you build a userform you can then program the form like this where "work" is the set password:

Code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
    Const pw As String = "work"

        ans = TextBox1
            If ans = "" Then
                MsgBox "You must enter a password!", vbCritical, "Try Again!"
                TextBox1 = Null
                TextBox1.SetFocus
            Exit Sub
          
            End If
            If ans <> pw Then
                MsgBox "Wrong Password!", vbCritical, "Try Again!"
                TextBox1 = Null
                TextBox1.SetFocus
            Exit Sub
            
            End If
        For Each wssheet In ActiveWorkbook.Worksheets
            wssheet.Visible = xlSheetVisible
        Next wssheet
        TextBox1 = Null
        TextBox1.SetFocus
        ShowCode = False
End
End Sub

Private Sub CommandButton2_Click()
    TextBox1 = Null
    TextBox1.SetFocus
    ShowCode = False
    Security.Hide
End Sub

Private Sub TextBox1_Change()
    If ShowCode = True Then
        TextBox1.PasswordChar = ""
    ElseIf ShowCode = False Then
        TextBox1.PasswordChar = "*"
    End If
End Sub

Private Sub UserForm_Activate()
    With Security
        .Top = Application.Top + 300
        .Left = Application.Left + 350
    End With
End Sub

When viewing the object, click on the text box and in the properties section of the VBA editor there is an option for "PasswordChar" you can enter anything you want in there *, &, %, $ and it will mask the entry into the text box.

The Private Sub TextBox1_Change() is used with a checkbox to either mask or unmask the entry as the user is inputting their password. If the checkbox is marked the entry is unmasked, if the checkbox is not checked the entry is masked.

 
In reference to my post.
It's just a few lines of code. Create a userform and add a textbox to it. Select this textbox and in the properties window find PasswordChar. Write any character you like and display the userform. See what happens when you fill the textbox.
Now the real project:
- create public text variable in a module,
- add a button (OK) to the userform,
- assign the textbox's text to the text variable in the button's 'Click' event procedure,
- in your code: clear text variable, display the userform, test text variable, if wrong either try again or quit the procedure.

combo
 
Hi...thanks

How do I adjust the UserForm in the code below?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewValue As Variant, OldValue As Variant
If Target.Count > 1 Then Exit Sub
'Say you want to work with the range A1:F100
If Not Intersect(Target, Range("A1:F10")) Is Nothing Then
NewValue = Target.Value
Application.EnableEvents = False
Application.Undo
OldValue = Target.Value
If OldValue = "" Then
Target.Value = NewValue
'ElseIf 'Before there was InputBox Then
Target.Value = NewValue
Else: MsgBox "Você não pode alterar o conteudo da celula.", 16, "Células Bloqueadas"
Target.Value = OldValue
End If
Application.EnableEvents = True
End If
End Sub
 
Code:
If Not Intersect(Target, Range("A1:F10")) Is Nothing Then
    ' a part of code from your initial post
Else
    'say txtInputPassword is a public text variable
    txtInputPassword=""
    frmInpPass.Show 'your userform that picks password
    If txtInputPassword="ale" then 'password ok
        '...
    Else ' wrong password
        '....
    End If
End If

combo
 
I dont seem to have this PasswordChar In the properties. I am using access 2007. Currently I am using password written to the InputMask property, however it only displays asterisks. Any idea how I can display a different character?
 
that is the password char

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
that is the password char
The rest of this post seems to say otherwise.
Is this PasswordChar property of a textbox available in Access 2007 as it is not showing in the property explorer. I can show an asterisk using the old method, but want to display a different character. Any ideas?
 
Nice. Asterisk doesnt look so great. Now all I need is the ascii for the little round ball.
 
Wish I could edit my posts. Information from that article does'nt explain how to change the asterisk on a password mask to another symbol. In fact its just the same information from the the help manual.
Cheers anyway , but I dont think Ill be able to change it short of coding something.
 
you cant change the PASSWORD mask its an Access default however you could change the font for the password text box which may give you what your looking for? You could also look at installing additional fonts however they will need to also be on your end users machine

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Yes. I looked ito this PasswordChar property it's in the Access help file as well but its not a property of MSAccess Textboxes. Looking in the object inspector I noticed that it IS a property of the MSForms 2.0 textbox. So I may use one of them ;)
 
i have been playing around with this bugs me that in vb you can set the password char but not in access. i came up with a work around although its not what i would call pretty. See code below, to test it you need one form with a text box-rename as necessary. I chose the wingdings font and used 'U' only because its a black dot (sort of) You could change the font and sub using the chr() function

Form Code
Code:
Option Compare Database
Dim strPassword As String

Private Sub Form_Load()
    strPassword = ""
End Sub

Private Sub Text0_Change()
    Dim i

    DoCmd.Requery
    If Me.Text0.Value = "" Then
        strPassword = ""
        Exit Sub
    End If
    strPassword = strPassword & Left(Nz(Me.Text0.Value, ""), 1)
    DoCmd.Requery
    Me.Text0.Value = ""
    For i = 1 To Len(strPassword)
        Me.Text0.Value = Me.Text0.Value & "U"
    Next i
End Sub

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
    If GetAsyncKeyState(VK_DELETE) <> 0 Or GetAsyncKeyState(VK_BACK) <> 0 Then
        Me.Text0 = ""
        strPassword = ""
        DoCmd.Requery
    End If

End Sub

and in a module
Code:
Declare Function GetAsyncKeyState Lib "User32.dll" (ByVal vKey As Long) As Long

Public Const VK_BACK As Long = &H8
Public Const VK_DELETE As Long = &H2E

Hope this helps

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
0149 ascii has a round ball. I can just use a MSForms textbox to get the funtionality of PasswordChar.
 
yep

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top