Open form with password

D

Doug_C

Hello all,

I was wondering if there is a way to open a particular form with a password?
I want to leave the database open to all user but just restrict ONLY one
form. Is this possible?

Thanks!!
 
K

Klatuu

You can add code to the Open event of the form to present an InputBox for the
user to enter a password, then validate the entry. If the password is
correct, allow the form to open, If not, close it.
 
K

Klatuu

This will do it. It allows 3 tries. To use it:

If not getpwd() then
Close the form
Endif

The main problem with this is there is no way to avoid the password being
displayed as the user types. The only way to get around that is to create a
simple Popup Modal form that does the same thing and format the text box for
the password as password.

Function GetPwd() As Boolean
Dim strpwd As String
Const conGoodPwd As String = "Bozo"
Dim intMaxTries As Integer

For intMaxTries = 1 To 3
strpwd = InputBox("Enter Password")
If strpwd = "" Then 'User Canceled
GetPwd = False
Exit For
End If
If strpwd = conGoodPwd Then 'Good
GetPwd = True
Exit For
End If
If MsgBox("Incorrect Password", vbExclamation + vbOKCancel, "Error")
= vbCancel Then
GetPwd = False
Exit For
End If
Next intMaxTries


End Function
 
D

Doug_C

Hi Klatuu,

I created a small from with one field and set the format for "password".
Where do I put your code in? On the small form I created to pop up or the
form I want the password to open? Also, where in the properties table should
it go? I am still learning Access but this site has been a big help. I
appreciate you working with me on this.

Thanks!!
 
K

Klatuu

Ok, here we go:

In the Properties Dialog for the text box for the password, click the box
with the ... next to the Input Mask property. Then select password from the
options.

Put this in the After Update event of the text box where you want to get the
password:

Private Sub txtPwd_AfterUpdate(Cancel As Integer)
Dim strpwd As String
Const conGoodPwd As String = "Bozo"
Dim intMaxTries As Integer

For intMaxTries = 1 To 3
If Me.txtPwd = "" Then 'User Canceled
DoCmd.Close
Exit For
End If
If Me.txtPwd = conGoodPwd Then 'Good
DoCmd.OpenForm "MyFormNameHere"
DoCmd.Close
Exit For
End If
If MsgBox("Incorrect Password", vbExclamation + vbOKCancel, "Error")
= vbCancel Then
DoCmd.Close
Exit For
Else
Cancel = True
End If
Next intMaxTries

End Sub
 
R

ron csumb

Klatuu,

I am trying to creat the same password for a form. I am just getting started
in learning codes. So I am at a lost at how this works. could you give me
step by step instructions on how to do this. I understand how to create
forms in access.
Or is there a site that can help me with adding code to create a password
for a single form.
thanks
 
C

Chris B via AccessMonster.com

Ron,
Klatuu explained quite well above on how to do this,

There is a sample database with a login form at
http://www.databasedev.co.uk/downloads.html
Look for "example of login form"
Maybe it could help to show where the codes all go?
HTH

ron said:
Klatuu,

I am trying to creat the same password for a form. I am just getting started
in learning codes. So I am at a lost at how this works. could you give me
step by step instructions on how to do this. I understand how to create
forms in access.
Or is there a site that can help me with adding code to create a password
for a single form.
thanks
Ok, here we go:
[quoted text clipped - 92 lines]
 
H

HL

Chris,

Thanks for the link below. I think that it will help me however I am having
problems with 1-2 lines of the code.

I am having problems with the line of code that states:
If Me.txtPassword.Value = DLookup("Password", "Login", "[lngUserID]=" &
Me.cboLogin.Value) Then
lngMyEmpID = Me.cboLogin.Value

and the fact that you are told to place this line of code in a module:
Public lngMyEmpID As Long

I am not familiar with modules so I would appreciate some advice on how to
create one. Additionally, the entire code that was written for the form is
listed below. Perhaps you (or someone else can read it) and help me to
understand the If statement that I mentioned above (step by step advice
please)

Private Sub cmdLogin_Click()
'Check to see if data is entered into the UserName combo box

If IsNull(Me.cboLogin) Or Me.cboLogin = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboLogin.SetFocus
Exit Sub
End If

'Check to see if data is entered into the password box

If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If

'Check value of password in Login table to see if this
'matches value chosen in combo box

If Me.txtPassword.Value = DLookup("Password", "Login", "[lngUserID]=" &
Me.cboLogin.Value) Then

lngMyEmpID = Me.cboLogin.Value

'Close logon form and open splash screen
DoCmd.Close acForm, "frmLogon", acSaveNo
'DoCmd.OpenForm "frmSplash_Screen"

Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
End If

'If User Enters incorrect password 3 times database will shutdown

intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.",
vbCritical, "Restricted Access!"
Application.Quit
End If

End Sub

Chris B via AccessMonster.com said:
Ron,
Klatuu explained quite well above on how to do this,

There is a sample database with a login form at
http://www.databasedev.co.uk/downloads.html
Look for "example of login form"
Maybe it could help to show where the codes all go?
HTH

ron said:
Klatuu,

I am trying to creat the same password for a form. I am just getting started
in learning codes. So I am at a lost at how this works. could you give me
step by step instructions on how to do this. I understand how to create
forms in access.
Or is there a site that can help me with adding code to create a password
for a single form.
thanks
Ok, here we go:
[quoted text clipped - 92 lines]
 
A

AlwaysFroosh!

All of this makes sense to me and I can get it to work, but I want to open
more than just one form with this same password form. How can I pass the name
of my form I want to open to the password form?

Thanks,
Graham
 
G

George Nicholson

Pass MyFormNameHere as the OpenArgs argument (7th argument) for OpenForm
when you open the password form:

DoCmd.OpenForm "frmPassword", , , , , , ,"MyFormNameHere"

then, replacewith:
DoCmd.OpenForm Me.OpenArgs

but I want to open
more than just one form with this same password form.

You do realize that your original post very specifically said you only
wanted to open *one* form, right?

Another variation (rather than having users constantly typing passwords
which is a pretty good way to annoy people) would be to set a global
variable to True once the password is successfully entered during any given
session. Check the variable before opening those forms. If True, open the
requested form, if False, open the password form.

HTH,
 
T

Tom Wussernark [MSFT]

if you want database security then you shoudl move to SQL Server and Access
Data Projects
 
A

AlwaysFroosh!

Thanks George.

I did finally get it to work, the problem I was having was referencing the
OpenArgs once I opened the form with opening arguments. I finally figured out
you could access it with:

Me.OpenArgs

Good tip on the global variable to remember they entered a correct pass, but
I don't think I will need to use it. The forms I am requiring a password for
access project settings which should not be changed on the fly or very oftern.

As for the opening only one form, this thread was not started by me, in my
post I said "more than one" form.

Thanks for you help though,
Graham
 
Top