How to insert a working macro in USERFORM, to hide the password with ***** asterisks

K0st4din

Active Member
Joined
Feb 8, 2012
Messages
488
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello, everyone,
thanks to Logit from the forum we were able to reach the final of my request for help.
This is the link to my inquiry:
Setting a Trial Period in Excel

However, at one point I realized that we had not done it right. The password I can write (when the time comes, you can see it).
I searched a lot, but almost everyone says that in order not to be seen, it must be made in USERFORM1.
I ask for your help, how to unite the macro working perfectly, but to go through USERFORM to hide the password ******* with asterisks.
Thank you in advance for your help.


This macro works just perfect for my needs
VBA Code:
Sub pass()
Dim mbox As String
Dim MyDate As Variant
Dim Passwd As String

MyDate = #8/30/2020#  ' Assign a date. tuk se zapisva krainata data, dokoga da byde otvoren.
Passwd = "123456" 'Assign password

Application.ScreenUpdating = False
Sheets("Sheet1").Visible = True
'Sheets("Sheet2").Visible = xlVeryHidden
Application.ScreenUpdating = True

If Date > MyDate Then
MsgBox "Oops! Test/Evaluation period of the utility has been expired." & vbCrLf & _
  "Pls ask the concern person to get the updated utility.", vbCritical, "Outdated/Expired Version"
  mbox = Application.InputBox("Pls input the password/code to continue...", "Password")

  If mbox <> Passwd Then
  MsgBox "Incorrect Password" & vbCrLf & _
  "Pls ask the concern person to get the correct password.", vbCritical, "Wrong password"

Application.Quit
With ThisWorkbook
  .Save
  .ChangeFileAccess Mode:=xlReadOnly
  'Kill .FullName
  .Close SaveChanges:=False
  End With
Else
  'Sheets("Sheet2").Visible = True
  Sheets("Sheet1").Visible = False
  End If

End If

End Sub



Code for USERFORM1
VBA Code:
Private Sub CheckBox1_Click()
If UserForm1.CheckBox1.Value = True Then
UserForm1.TextBox1.PasswordChar = ""
Else
UserForm1.TextBox1.PasswordChar = "*"
End If
End Sub

Private Sub CommandButton1_Click()
If UserForm1.CheckBox1.Value = "123" Then
Unload Me
MsgBox ("Welcome!")
Else
Unload Me
MsgBox ("Invalid password, try again!")
UserForm1.Show
End If
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub
2020-06-13_160757.jpg

2020-06-13_160845.jpg
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
No no no,
this is far different from what I am looking for. :)
The first macro works flawlessly, but when the deadline comes to open it, you will need to write a password.
In this case, when MsgBox is displayed, it is allowed to write the password, but it is visible.
For example the password is 123456 and it is visible, I want to make it ******.
Many places say that I need to have a userform in order to be in the form of *****.
So, the macro, I got it
2nd I made my Userform
3rd - I do not know how to combine them into one
Ie 100000% the same work on the first macro, but to hide (replace) the password with ******
 
Upvote 0
So, the macro, I got it
2nd I made my Userform
3rd - I do not know how to combine them into one

Try following & see if it will do what you want


Place ALL following code in your USERFORMS code page

VBA Code:
Dim Attempt As Integer
Dim RequiredPassword As String
Dim ValidPassword As Boolean
Private Sub CheckBox1_Click()
    Me.TextBox1.PasswordChar = IIf(Me.CheckBox1.Value, "", "*")
End Sub

Private Sub CommandButton1_Click()
    With Me.TextBox1
        If Len(.Text) = 0 Then Exit Sub
        ValidPassword = CBool(.Text = RequiredPassword)
        If ValidPassword Then
            Me.Hide
        Else
            MsgBox "Invalid Password", 16, "Invalid Password"
            .Text = ""
            Attempt = Attempt + 1
            If Attempt > 3 Then
                Me.Hide
            Else
                Me.Caption = "Attempt " & Attempt & " of 3"
                .SetFocus
            End If
        End If
    End With
End Sub

Private Sub CommandButton2_Click()
'cancel
    Me.Hide
End Sub

Function GetPassword(ByVal Password As String) As Boolean
    RequiredPassword = Password
    Me.Show
    GetPassword = ValidPassword
    Unload Me
End Function

Private Sub UserForm_Activate()
     With Me.TextBox1
        .Text = ""
        .SetFocus
     End With
End Sub

Private Sub UserForm_Initialize()
    Attempt = 1
    Me.Caption = "Attempt " & Attempt & " of 3"
    Me.TextBox1.PasswordChar = "*"
    ValidPassword = False
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = CloseMode = 0
    If Cancel Then Me.Hide
End Sub

Replace your current pass code with following

Rich (BB code):
Sub pass()
    Dim IsValidPassword As Boolean
    Dim MyDate As Date
    Dim Passwd As String
    
' Assign a date. tuk se zapisva krainata data, dokoga da byde otvoren.
    MyDate = #8/30/2020#
'Assign password
    Passwd = "123456"
    
    Application.ScreenUpdating = False
    Sheets("Sheet1").Visible = True
    'Sheets("Sheet2").Visible = xlVeryHidden
    Application.ScreenUpdating = True
    
    If Date > MyDate Then
        MsgBox "Oops! Test/Evaluation period of the utility has been expired." & vbCrLf & _
        "Pls ask the concern person to get the updated utility.", vbCritical, "Outdated/Expired Version"
        
        IsValidPassword = UserForm1.GetPassword(Passwd)

        If Not IsValidPassword Then
        
        MsgBox "Incorrect Password" & vbCrLf & _
                "Please ask the concern person to get the correct password.", vbCritical, "Wrong password"
                
            With ThisWorkbook
                .Save
                .ChangeFileAccess Mode:=xlReadOnly
                Kill .FullName
                .Close SaveChanges:=False
            End With
        Else

            Sheets("Sheet2").Visible = True
            Sheets("Sheet1").Visible = False
        End If
    End If
    
End Sub

Note: Pressing the Cancel button on your userform has the same effect as entering invalid password - If you need to allow user to cancel without going through the invalid password part of your code, then a Cancel parameter can added to manage this.

I have assumed that your Userform has its default name "UserForm1" but you should amend the code where shown in BOLD if a different name applies.

Hope Helpful

Dave
 
Upvote 0
Hello @dmt32
most likely I'm confusing somewhere with putting the right places in the macros, but I'm attaching photos to correct me if I'm wrong somewhere.
And to add, because I doubt it's exactly what I'm looking for.
Here's what happens to the original macro, which allows you to open the workbook until it reaches a date (in the example) 8/30/2020
On 8/31/2020, if someone tries to open the workbook, only then does this MsgBox appear to ask for a password. Before that, the workbook opens without bothering the user for passwords. But if I decide to write the password in front of him in MsgBox, he will see it.
Everyone says that in order to hide this password with ******, I need to have Userform. - >>>>> only because of *******.
I don't want Userform to appear every time I open the workbook, but I do want it when the day I wrote it passes (it's like asking for a password because the trial period has expired. I need it for something else, but maybe it's easier understood in this way). I hope to understand, if not I will write again.
If we assume that I have changed a few lines that you have replaced and the password is "123456", then why do I count these 3 attempts, but writing "123456" - tells me that the password is wrong?
And the last one: this 16 - what is it for?
From your answer, the first macro (there are several) I put them in Userform1, ViewCode, - all in one place.
Is this correct?
The second is mine, which works very well, but I have replaced the lines you said to link to Userform1.
Is this correct?
Thank you in advance
2020-06-14_210731.jpg

2020-06-14_210811.jpg

2020-06-14_210836.jpg
 
Upvote 0
P.S - As for CANCEL, the button, this USERFORM1, will be displayed when the date expires and this CANCEL button, if pressed, must close the entire workbook again so that it is not active.
 
Upvote 0
And the last one: this 16 - what is it for?

16 is the Numeric value for constant vbCritical - for msgbox function I prefer to just use numeric values to display required button(s) & icon.

From your answer, the first macro (there are several) I put them in Userform1, ViewCode, - all in one place.
Is this correct?

Yes

The second is mine, which works very well, but I have replaced the lines you said to link to Userform1.
Is this correct?

Yes

Code I posted worked ok for me & cannot immediately explain why not for you - can only guess you may have a space or something in the textbox entry
If you are able, place copy of your workbook in a dropbox & provide a link to it here & I will take a further look

Dave



Thank you in advance



[/QUOTE]
 
Upvote 0
Ok, that's great.
I will attach the test workbook directly.
Thank you so much.
I will attach another one, which contains only my macro, and you will be able to test it yourself by changing the date so that it is locked, for example from tomorrow (or when it is convenient for you).
My macro:
My macro.xlsm

New one from you:
 
Upvote 0
update code works ok - issue you have is that you are just opening the userform - but you need to call your pass code for the process to work correctly.

replace this code you have in the ThisWorkBook Module

VBA Code:
Private Sub Workbook_Open()
UserForm1.Show
End Sub

with this

VBA Code:
Private Sub Workbook_Open()
    pass
End Sub



Dave
 
Upvote 0
Oh my God,
I forgot to remove it.
In mine I did it right, in the new one I forgot it.
I will try it now, but I think everything will be fine.
If there is something I will write again, if not I want to thank you heartily for your cooperation.
As I have always said, I am infinitely happy that there are people like you who can always help us, those who are just trying to do something.
Be alive and healthy.
Thanks a lot
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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