Mask Input box text from a command button in a userform

ReignEternal

New Member
Joined
Apr 11, 2021
Messages
41
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a UserForm that I have a button that will delete a line item (after a line item is selected and then the delete button is pressed) that is password protected. This works just fine but when the password is entered, users can see the password. I would like to have the field masked. The lines in bold are the issue I am having. I've been reading through some of the forums here but they are suggesting to create a user form. Can i create a user form within a user form? Here is the most recent thread I was reading (VBA password request before sub starts)

VBA Code:
Private Sub cmdDelete_Click()

    If Selected_List = 0 Then
    
        MsgBox "No row has been selected for deletion!", vbOKOnly + vbInformation, "Edit"
        
        Exit Sub
    
    End If
    
[COLOR=rgb(0, 0, 0)][B]    Dim ans As String[/B][/COLOR]
[B][COLOR=rgb(0, 0, 0)]    ans = InputBox("Please Enter Password to Allow Deletion of Line Item!")
    [/COLOR][/B]
[COLOR=rgb(0, 0, 0)][B]    If ans = "BidSizzle" Then[/B][/COLOR]
    
        Dim iRow As Long
        
        If Selected_List = 0 Then
        
            MsgBox "No row is selected.", vbOKOnly + vbInformation, "Delete"
            Exit Sub
        End If
        
        Dim i As VbMsgBoxResult
        
        i = MsgBox("Do you want to delete the selected record?", vbYesNo + vbQuestion, "Confirmation")
        
        If i = vbNo Then Exit Sub
        
        iRow = Application.WorksheetFunction.Match(Me.lstDatabase.List(Me.lstDatabase.ListIndex, 0), _
        ThisWorkbook.Sheets("Database").Range("A:A"), 0)
        
        Worksheets("Database").Unprotect "GoldStar"
        
        ThisWorkbook.Sheets("Database").Rows(iRow).Delete
        
        Worksheets("Database").Protect "GoldStar"
        
        Call Reset
        
        MsgBox "Selected record has been deleted!", vbOKOnly + vbInformation, "Deleted"
    
    End If
    
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You create each userform in advance, but you can put a second userform on the screen through code running in the module of the first userform.
 
Upvote 0
You create each userform in advance, but you can put a second userform on the screen through code running in the module of the first userform.
Forgive my ignorance on this one. Would I use the Show function within the VBA for my first UserForm? Here is the code that calls my first Userform

VBA Code:
Sub Show_Form()
    Call DisableWB
    Worksheets("SearchData").Unprotect "GoldStar"

        frmForm.Show
        frmDelete.Show
        
    Worksheets("SearchData").Protect "GoldStar"
    Call EnableWB
End Sub

Or would I need to use the Show function within the associated frame VBA

VBA Code:
Private Sub Frame2_Click()

        frmDelete.Show

End Sub

My gut is telling me the second option is where I would want this to occur.
 
Upvote 0
Forgive my ignorance on this one. Would I use the Show function within the VBA for my first UserForm? Here is the code that calls my first Userform

VBA Code:
Sub Show_Form()
    Call DisableWB
    Worksheets("SearchData").Unprotect "GoldStar"

        frmForm.Show
        frmDelete.Show
       
    Worksheets("SearchData").Protect "GoldStar"
    Call EnableWB
End Sub

Or would I need to use the Show function within the associated frame VBA

VBA Code:
Private Sub Frame2_Click()

        frmDelete.Show

End Sub

My gut is telling me the second option is where I would want this to occur.
I just tried having it live within the associated frame and unless I click in the frames region, the userform frmDelete will not show.
 
Upvote 0
You don't need an event handler to show your second userform.
Let's create a Userform1 and be sure it carries at least a TextBox1 and a CommandButton1.
Paste the code below in the userform's code module.

VBA Code:
Option Explicit

Public Function GetUserPassWord() As String
    Me.CommandButton1.Caption = "Submit"
    With Me.TextBox1
        .Font.Size = 10
        .PasswordChar = "*"
        .Value = ""
    End With
    Me.Show
    GetUserPassWord = Me.TextBox1
End Function

Private Sub CommandButton1_Click()
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub


If you have done the foregoing, open the code module of your first userform and replace this line:
Rich (BB code):
ans = InputBox("Please Enter Password to Allow Deletion of Line Item!")

with this line:
VBA Code:
ans = Userform1.GetUserPassWord

Hopefully this is what you're looking for.
Of course you are free to change the names of both userform and controls to meaningful names, this is even recommended, as long as you change the names in the code as well.
 
Upvote 0
You don't need an event handler to show your second userform.
Let's create a Userform1 and be sure it carries at least a TextBox1 and a CommandButton1.
Paste the code below in the userform's code module.

VBA Code:
Option Explicit

Public Function GetUserPassWord() As String
    Me.CommandButton1.Caption = "Submit"
    With Me.TextBox1
        .Font.Size = 10
        .PasswordChar = "*"
        .Value = ""
    End With
    Me.Show
    GetUserPassWord = Me.TextBox1
End Function

Private Sub CommandButton1_Click()
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub


If you have done the foregoing, open the code module of your first userform and replace this line:
Rich (BB code):
ans = InputBox("Please Enter Password to Allow Deletion of Line Item!")

with this line:
VBA Code:
ans = Userform1.GetUserPassWord

Hopefully this is what you're looking for.
Of course you are free to change the names of both userform and controls to meaningful names, this is even recommended, as long as you change the names in the code as well.
I apologize for the delay, I was traveling. I will try this and test it. Thank you for your patience and thank you for your guidance.
 
Upvote 0
Glad to help and thanks for the follow-up (y)
 
Upvote 0
Glad to help and thanks for the follow-up (y)
So I managed to play around with the VBA you provided and somehow I keep getting it to lock up the UserForm. What I mean is that when I enter the VBA and make the minor modifications where I believe I should be making them, when I go to test out the command button (Called Delete), I see the button text switch to Submit as, I enter the "Password" into the textbox and click on submit. Once Submit is clicked, the textbox data is cleared out but then the form locks up. So what I am going to do is shoot a temp link to my document so you can see it fully. The document does not reflect any of the edits you have pointed out. It is the fully functional version (Minus the password Masking).

 
Upvote 0
What I mean is that when I enter the VBA and make the minor modifications where I believe I should be making them ....
You were not supposed to make any modifications to the userform's code, at least no modifications other than renaming the controls used. Perhaps that's where it goes wrong.
The userform intends to replace the VBA input box and behaves similar (like a popup) returning the user input, which is - unlike the input box - displayed masked as per your query.

If the code below (being a slightly extended version of my previous code) is pasted into a Userform1 with successively a Label1, Label2, TextBox1, CommandButton1 and a CommandButton2, then it works for me ...


. . . . . . .
ReignEternal.gif



This goes in the Userform's module:
VBA Code:
Option Explicit

Public Function GetUserPassWord() As String
    Me.Caption = "Delete ..."
    Me.CommandButton1.Caption = "Cancel"
    Me.CommandButton2.Caption = "Submit"
    Me.Label1.Caption = "You're about to delete some data." & vbNewLine & "Please confirm!"
    Me.Label2.Caption = "Password:"
    
    With Me.TextBox1
        .Font.Size = 10
        .PasswordChar = "*"
        .Value = ""
    End With
    Me.Show
    GetUserPassWord = Me.TextBox1
End Function

Private Sub CommandButton1_Click()
    Me.TextBox1.Value = ""
    Me.Hide
End Sub

Private Sub CommandButton2_Click()
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub



These are the changes to be made to your current code of the cmdDelete_Click() event handler:
VBA Code:
    'ans = InputBox("Please Enter Password to Allow Deletion of Line Item!", "Password Entry")
    
    ans = UserForm1.GetUserPassWord
    
    ' MsgBox "Password entered: " & ans    ' <<<< used in the demo
 
Upvote 0
Solution
At first glance, and after read g your remarks, I see I think I was entering the vba in the wrong place and due to me probably adding confusion to myself, I thought I needed to make some edits. I hate to be a burden but I need to be educated. I know where to access basic "modules" but when you say user form module, may you show me a screen shot of where you are referring to? What I have been going into is in the main tree, just above modules, right clicking on user form (frmForm) and I have been considering that the user form module.
 
Upvote 0

Forum statistics

Threads
1,214,670
Messages
6,120,831
Members
448,990
Latest member
rohitsomani

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