Password on forms

C

Cesar

Hi,
I'm using a form (frmEntryCost) to enter values to Cells in a Worksheet,
but before open this form I need to prompt for a password and if this
password is valid, then open the entry form if not, a message should show
with "wrong password, try again!" and keep asking for the password until it
matches the valid password. The real password I want it to be, let's say in
the cell Z1, where I can change it easily. What is the code for this password
to work?
Thanks
 
R

rylo

Hi

Here's a pretty crude method.


Code
-------------------
Sub ccc()
cntr = 0
correctpassword = Range("I1").Value
Do
cntr = cntr + 1
getpass = Application.InputBox("Enter the password", Type:=2)
If getpass <> correctpassword Then
MsgBox "Wrong Password, Retry"
End If
Loop Until getpass = correctpassword Or cntr > 3
If cntr > 3 Then
MsgBox "Too may attempts - crashed out"
End If

End Su
 
R

royUK

Try this


Code:
--------------------
Option Explicit

Sub pwForm()
Dim pw As String
pw = InputBox("Enter password")
If pw = "" Then Exit Sub
If pw <> Sheet1.Range("z1").Value Then Exit Sub
UserForm1.Show
End Sub
--------------------


To keep prompting you can replace Exit Sub with Call pwForm
*-Post posted before response, posts merged!*-
You can simply add UserForm1.Show to the end of Rylo's code


--
royUK

Hope that helps, RoyUK
For tips & examples visit my 'web site
' (http://www.excel-it.com)
 
K

keiji kounoike

This is a sample of using Userform. first create two Userforms. then,
put two commandbutttons, one for Enter, the other for Cancel, and one
Label for Warning and one textbox for password input in Userform1. I put
password in the code instead of cell for simplicity. Run the macro Testform.

'put the macro Testform in standard module
Sub Testform()
UserForm1.Show
End Sub

'put the code below in Userform1
Const password = "pass" '<<==Change to your Password

Private Sub UserForm_Initialize()
Me.TextBox1.PasswordChar = "*"
Me.Label1.Visible = False
Me.Label1.Caption = "Wrong Password, try again!"
End Sub

Private Sub CommandButton1_Click() '<<==For Enter
If Me.TextBox1.Value = password Then
Unload UserForm1
UserForm2.Show
Else
Me.TextBox1.SetFocus
Me.TextBox1.Value = ""
Me.Label1.Visible = True
End If
End Sub

Private Sub CommandButton2_Click() '<<==For Cancel
Unload UserForm1
End Sub

keiji
 
C

Cesar

Thanks, both worked perfect!
I tried both and both works perfect, I like the one with the forms!
 
C

Cesar

Thanks Keiji,
Just one more question,
Where do I need to write the code?
Const password ="pass"
you said below in UserForm1, but I don't know where, under click() or
activate() or where?

Thanks again
 
K

keiji kounoike

at the top part of Userform1's code module where you write the event
procedures. i mean putting it before any sub procedures or functions in
the code module(userform1).

keiji
 
K

keiji kounoike

at the top part of Userform1's code module where you write the event
procedures. i mean putting it before any sub procedures or functions in
the code module(userform1).

keiji
 
K

keiji kounoike

at the top part of Userform1's code module where you write the event
procedures. i mean putting it before any sub procedures or functions in
the code module(userform1).

keiji
 
K

keiji kounoike

at the top part of Userform1's code module where you write the event
procedures. i mean putting it before any sub procedures or functions in
the code module(userform1).

keiji
 
K

keiji kounoike

at the top part of Userform1's code module where you write the event
procedures. i mean putting it before any sub procedures or functions in
the code module(userform1).

keiji
 
K

keiji kounoike

at the top part of Userform1's code module where you write the event
procedures. i mean putting it before any sub procedures or functions in
the code module(userform1).

keiji
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top