PDA

View Full Version : Solved: Password protecting individual userforms



GrantLund
07-12-2006, 12:50 AM
Hi Everyone!

I'm a VBA newbie and have just learnt to create userforms with text boxes from a commandbutton application within Word. The next thing I want to do is password protect some of these pop-ups so that not everyone who reads the document and clicks the commandbuttons can see what pops up (ie. I want some pop-ups to be accessible but others not). If the wrong password is typed, then "incorrect password" should display and the userform/textbox combination does not appear. If the correct password is typed then the userform/textbox info is displayed.

Is there anyway I can do this? I have tried looking at the password property but it talks about the complete document.

Any help will be much appreciated.

Rgds,
Grant
PS: You can email directly if u like.
grant.lund@sasol.com

Killian
07-12-2006, 01:58 AM
Hi and welcome to VBAX :hi:

You can use the VBA InputBox and MsgBox functions to interact with the user (gather and display information).
Public Const PW As String = "temp"

Sub ButtonActionOne()

If InputBox("Please enter password:") = PW Then
UserForm1.Show
Else
MsgBox ("Incorrect pasword")
End If

End SubObviously this simple approach has obvious limitations:
the password is displayed when it's typed in the inputbox
the password is hard-coded, unencrypted, in the code behind the document

So, it's not "secure" but as a simple means of implementing form protection, it might be sufficient

GrantLund
07-12-2006, 02:57 AM
Thank you. I have tried the following and it appears to work:

Userform13 is a form with label (enter password), open textbox (for entering password) and command button (enter).
Userform 14 is an "incorrect password" userform w/textbox.
Userform 11 is the "hidden" info.

Private Sub cmdEnter_Click()
If UserForm13.txtPassword.Text <> "Sasol" Then
UserForm13.Hide
UserForm14.Show
Rem End If
Rem txtPassword.Text = ""
Rem UserForm13.Hide
Rem End
Else
UserForm13.Hide
UserForm11.Show
End If
End Sub
Private Sub UserForm_Activate()
txtPassword.SetFocus
End Sub

I have tried this and it works for what I wanted to do. The only problem is that if someone can get into the code they can see the password and then the hidden info. I am hoping that by making the file read-only when I distribute it that only the people I give the password to will be open to see the hidden pop-up.

Many thanx though - I wasnt sure whether your response was inline with what I wanted (haven't tested it out) but thank you none-the-less.

Grant:hi:

MOS MASTER
07-12-2006, 02:00 PM
The only problem is that if someone can get into the code they can see the password and then the hidden info.

Hi Grant, :D

You can password protect your VBA project too so people can't see your source code:
* ALT+F11
* Right click on project (X)
* Choose project properties
* Choose security tab.
* Check the lock box and fill in the password.
* Save | Close and open the document.
* The project is now protected.

Remember there's no such thing as a 100% protection of Source code or documents or what ever. It's only a protection that can keep away the modal user. (an expert can always bypass it)

HTH. :whistle: