PDA

View Full Version : Solved: Password entry visible



Gil
04-30-2010, 08:44 AM
Hello
I am trying to use this code to make access to other events password protected.It works ok except when you type 'MyPassword' it is visible in the entry box. Is there any code I can add to make it '**********'.


Option Explicit

'This code goes in UserForm OptionButton1, change the name to match your command buttons name.

Private Sub OptionButton1_Click()
Dim sPassword As String

sPassword = InputBox("Enter Your Password")
If sPassword = "mypassword" Then


Else
MsgBox "Password incorrect!"
Exit Sub
End If
End Sub


Many thanks for any suggestions
Gil

Bob Phillips
04-30-2010, 09:33 AM
You need to create a form, with a password textbox, and the PasswordChar property set to *

Gil
04-30-2010, 11:54 AM
Hello xld
Thanks for that info. I have created the form with a password textbox, set the PasswordChar to *.
Then I inserted the above code and when entering the password the original box appears, so I deleted the line
sPassword = InputBox("Enter Your Password") Now as soon as any character is entered in the password box it goes straight to Password incorrect.
So I tried inserting variations of

sPassword = TextBox1("Enter Your Password")
but with no good result.
Any further help will be appreciated
Gil

Bob Phillips
04-30-2010, 12:00 PM
You need to show the form, and then check the password from within the form code. The option button code needs to know the result.

Gil
04-30-2010, 03:16 PM
Hello xld
Sorry, I am lost on that one.
Gil

Bob Phillips
04-30-2010, 03:28 PM
Show us all of the code Gil, we will just be talking cross-purposes otherwise.

Gil
04-30-2010, 04:12 PM
Hello xld
Please see the attachment for what I have so far.
Gil

lynnnow
04-30-2010, 11:14 PM
try this in your code:

Private Sub ok_Click()
Dim sPassword As String
If TextBox1.Value = "mypassword" Then
Youhavebeensucessfull.Show

Else
MsgBox "Password incorrect!"
Exit Sub
End If
End Sub

Bob Phillips
05-01-2010, 02:02 AM
Hello xld
Please see the attachment for what I have so far.
Gil

I don't get the error that you describe, it waits until I hit OK before it validates it.

But you do have to test the correct thing



Private Sub ok_Click()
If TextBox1.Text = "mypassword" Then
Youhavebeensucessfull.Show

Else
MsgBox "Password incorrect!"
Exit Sub
End If
End Sub


and add some code for the cancel buttons.

Gil
05-01-2010, 02:03 AM
Hello lynnnow
Thank you for your reply and forgive me but don't I already have that code in my attachment. If it should be inserted elsewhere please advise
Gil

lynnnow
05-01-2010, 02:13 AM
Gil,

Your code in the file is:

Private Sub ok_Click()
Dim sPassword As String
If sPassword = "mypassword" Then '<<< Changed this line
Youhavebeensucessfull.Show

Else
MsgBox "Password incorrect!"
Exit Sub
End If
End Sub
I've corrected it to:

Private Sub ok_Click()
If TextBox1.Text = "mypassword" Then '<<< To this
Youhavebeensucessfull.Show

Else
MsgBox "Password incorrect!"
Exit Sub
End If
End Sub
You did not validate the value inserted in the textbox, that was the flaw in the code. As xld has pointed out, you need to add code for the Cancel button too.

HTH

Lincoln

Gil
05-01-2010, 09:36 AM
Hello
First of all may I say a big thank you to xld & lynnnow for being patient with me in my quest for the answer and for supplying the feeedback to resolve my issue.
It all works for me just fine and have attached the workbook with the updated code with an odd bit or 2 added by me, so if there are any others like me out there struggling for a similar answer then I hope it helps them as much as me.
All too often reading the posts they start with a problem for which suggestions are made and then closed as solved without an example of the solution being published for novices like me to study.
Getting back to the code one more answer if possible is if you Cancel from the Password Entry screen I notice that the Option Button1 stays selected and is inoperable.
Gil

Bob Phillips
05-01-2010, 11:45 AM
That is a problem with using just a single option button, they are designed to be used in groups, to allow a single choice. When there is only one, it is better to use a button or a checkbox.

Gil
05-01-2010, 11:53 AM
Thank you xld
I have tried the other options of different buttons. In the end I decided to add a similar button just to make the change.
But, the question always has to be asked just in case there is a neater solution.
Many thanks again and I will mark this solved.
Gil