create a password protected form with VB

JackBauer

Registered User.
Local time
Today, 10:20
Joined
Sep 16, 2008
Messages
15
i followed the direction on the MS Help & Support page to create a password protected form. However, the default password that is given is "PASSWORD", does anyone know how to change that default password to something else?
Thanks.
Using Code to Password Protect a Form

By using code, you can prompt for a password when a user opens a form or a report. If the correct password is entered, the form or the report is opened.

The following example shows you how you can password protect the Orders form in the sample database Northwind.mdb:
1.Start Access and then open the sample database Northwind.mdb.
2.Press ALT+F11 to start the Microsoft Visual Basic editor.
3.On the Insert menu, click Module.4.In the module sheet, type the following procedure:

Public MyPassword
Public Function KeyCode(Password As String) As Long
' This function will produce a unique key for the
' string that is passed in as the Password.
Dim I As Integer
Dim Hold As Long

For I = 1 To Len(Password)
Select Case (Asc(Left(Password, 1)) * I) Mod 4
Case Is = 0
Hold = Hold + (Asc(Mid(Password, I, 1)) * I)
Case Is = 1
Hold = Hold - (Asc(Mid(Password, I, 1)) * I)
Case Is = 2
Hold = Hold + (Asc(Mid(Password, I, 1)) * _
(I - Asc(Mid(Password, I, 1))))
Case Is = 3
Hold = Hold - (Asc(Mid(Password, I, 1)) * _
(I + Len(Password)))
End Select
Next I
KeyCode = Hold
End Function

5.Press ALT+F11 to return to Access.
6.In the Database window, under Objects, click Tables, and then click New.
7.In the New Table dialog box, double-click Design View.
8.Create a new table as follows: Table: tblPassword
---------------------------
Field Name: ObjectName
Data Type: Text
Field Size: 50
Field Name: KeyCode
Data Type: Text
Field Size: 25
Input Mask: Password

Table Properties: tblPassword
-----------------------------
PrimaryKey: ObjectName

9.Open the tblPassword table and then enter the following data: ObjectName: Orders
KeyCode: 2818

10.Create a new form in design view and save the form as frmPassword.
11.Add a single textbox to frmPassword called Text0, and a command button called CheckPassword.
12.Set the Input Mask property of Text0 to "PASSWORD" (minus the quotation marks).
13.Add the following code to the OnClick Event of the CheckPassword button and then save the form:

If IsNull(Forms!frmPassword!Text0.Value) Then
MsgBox "You cannot enter a blank Password. Try again."
Me!Text0.SetFocus
Else
MyPassword = Me!Text0.Value
DoCmd.Close acForm, "frmPassword"
End If

14.Open the Orders form in Design view.
15.If the property sheet is not visible, click Properties on the View menu.
16.Type the following event procedure in the module for the OnOpen property of the form:

Private Sub Form_Open(Cancel as Integer)
Dim Hold As Variant
Dim tmpKey As Long
Dim I As Integer
Dim rs As DAO.Recordset
Dim db As DAO.Database

On Error GoTo Error_Handler
' Prompt the user for the Password.
DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
Hold = MyPassword
' Open the table that contains the password.
Set db = CurrentDb
Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
rs.Index = "PrimaryKey"
rs.Seek "=", Me.Name
If rs.NoMatch Then
MsgBox "Sorry cannot find password information. Try Again"
Cancel = -1
Else
' Test to see if the key generated matches the key in
' the table; if there is not a match, stop the form
' from opening.
If Not (rs![keycode] = KeyCode(Cstr(Hold))) Then
MsgBox "Sorry you entered the wrong password." & _
"Try again.", vbOKOnly, "Incorrect Password"
Cancel = -1
End If
End If
rs.Close
db.Close
Exit Sub

Error_Handler:
MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
Exit Sub
End Sub

17.Close and then save the Orders form.
18.Open the Orders form and then type PASSWORD when you are prompted for a password.

Notice that the Orders form opens. The KeyCode that is generated by PASSWORD matches the KeyCode in the tblPassword table, and is dependent on the case of the letters in the password entered.

19.Close and then reopen the Orders form and then type PassWord when you are prompted for a password.

Notice that you receive the message: Sorry you entered the wrong password. Try again.
The Orders form does not open because the password procedure is case-sensitive.20.To determine what the corresponding KeyCode is for a particular string, type the following in the Immediate window and then press ENTER: ?KeyCode("TestString")

The earlier example returns 5864.21.To hide the tblPassword table in the Database window, right-click the tblPassword table, and then click Properties. In the Properties window, click to select the Hidden check box, and then click OK.
 
In VBA editor's immediate windows:

Code:
?KeyCode("MyNewPassword")

Save the output to the tblPassword.
 
it returned a string of number (i.e. -39399). I put that in the tblPassword. But i how do i generate a new password to replace the default one?
Thanks.
 
Write a function that calls the KeyCode function to verify the old password and create a password then call it one more time to verify that second input is same as the first.

Use that function with a popup form or whatever you want.
 
Banana,
I'm a novice when it comes to ACCESS and VB. How do i write the function to call the KeyCode?
Thanks much.
 
Well, probably the easiest thing for you to do is to create a new form, add three textbox with Password input mask and a command button to update the password. Make it popup & modal, then add a button somewhere on your main form to allow users to change the password.

In first button's code, you would put down the code needed to query the tblPassword to verify that old password is correct, check if second & third textbox matches before changing the record in tblPassword.

Give it a whirl and see how far you can get along, and if you're stuck on a specific step, post back.


Also, I just want to be 100% sure; is there a particular reason why you don't want to use database share password or User Level Security password? Are you aware that this is not quite foolproof? Good enough if you just want to keep honest people from accidentally logging as someone else, but definitely not a malicious user.
 
I've tried to implement user-security level but failed. I kept getting errors couldn't get the solutions that I want. This password protected form is the closest thing that I can do. I'm aware of its security limitation. However, I don't have much options here. This database im creating is intended for 2 groups of people. One group can view/edit all forms/tables. The other group would have limited access. Therefore, the password protected form is to prevent the 2nd group from accessing certain forms/tables. I'll be glad to hear if you have any ideas/suggestions.

Back to this password protected form. What I'm looking to accomplish is to create a different password other than the default "PASSWORD". I don't necessary need a "change password form" for users to change it. I'd like to change it myself and let users know what the password is. Is there a way to do that instead of creating a new form?
Thanks.
 
I thought I answered that in my post #2; when you put that number in the tblPassword, it's now ready to go. Just open the protected form, type in the new password, and it should work.

Also, for ULS, the guide I usually recommend to novices is one from Mr. Jack MacDonald; I've posted the link in the FAQ section of this forum.
 
im not sure what im missing here. I put in the generated number in the tblPassword. Then i open the protected form, type in the new password, it didn't work. Advice?
 
Hi,

I ve made all these steps, but I have a problem!
Into tblPassword I typed KeyCode 2818 (as the example says)
But when it asks for Password to see the form, doesn't accept this password (2818), accepts only to type PASSWORD to get opened!
My password shouldn't be 2818 to get open the form?
what I made wrong?


Also I took the value from immediate window (5864) and put it to
tblPassword
field Keycode,
instead of 2818!
and doesn't open with noone password!

Now which is my password i can't understand!!!!

Thanks
 
I thought I answered that in my post #2; when you put that number in the tblPassword, it's now ready to go. Just open the protected form, type in the new password, and it should work.

Also, for ULS, the guide I usually recommend to novices is one from Mr. Jack MacDonald; I've posted the link in the FAQ section of this forum.
Sorry, I can't find the link. Can you please hypelink the location.
 
well, probably the easiest thing for you to do is to create a new form, add three textbox with password input mask and a command button to update the password. Make it popup & modal, then add a button somewhere on your main form to allow users to change the password.

In first button's code, you would put down the code needed to query the tblpassword to verify that old password is correct, check if second & third textbox matches before changing the record in tblpassword.

Give it a whirl and see how far you can get along, and if you're stuck on a specific step, post back.
can you please explain in a step by step method as it would be helpful to understand the tips and trick by novices like me?
 
thanks for providing the link in reply to my question. but, i must admit that,being a novice to vba, the article, which does not contain any code , is too complicated for me to understand. Your help in resovling this problem, while running the command button in the frmPassword enclosed herewith, would go a long way.
what I want to achieve is to enable others to change the default password and to enter a new password of their choice, simply by entering new password in the utility form which would be invoked by a command button in some other form.
I have created the required forms as directed earlier in your Post #2 and Post #3, in reply to a question by some other person of this same forum.
Hope, i have made it clear.
thank U.
 

Attachments

Re: create a password protected form with VB
thanks for providing the link in reply to my question. but, i must admit that,being a novice to vba, the article, which does not contain any code , is too complicated for me to understand. Your help in resovling this problem, while running the command button in the frmPassword enclosed herewith, would go a long way.
what I want to achieve is to enable others to change the default password and to enter a new password of their choice, simply by entering new password in the utility form which would be invoked by a command button in some other form.
I have created the required forms as directed earlier in your Post #2 and Post #3, in reply to a question by some other person of this same forum.
Hope, i have made it clear.
thank U.
 
thanks for providing the link in reply to my question. but, i must admit that,being a novice to vba, the article, which does not contain any code , is too complicated for me to understand. Your help in resovling this problem, while running the command button in the frmPassword enclosed herewith, would go a long way.
what I want to achieve is to enable others to change the default password and to enter a new password of their choice, simply by entering new password in the utility form which would be invoked by a command button in some other form.
I have created the required forms as directed earlier in your Post #2 and Post #3, in reply to a question by some other person of this same forum.
Hope, i have made it clear.
thank U.
THIS WITH REFERENCE TO Mr. Banana's suggestion given in reply to a
question as to how to create a "Password change form" in this forum.
(ref. dt. 16/9/2008). as suggested by Mr.JackBauer Post dt. 16-Sep-2008) I created a form and tried to generate a unique key as instructed in the coding procedure. but I do not know how to generate a unique key for the newly changed password string. I am herewith attaching the .mdb file for view and correct the code I have written. the unique key generated for the string "PASSWORD", as per the coding, is 2818 and the same should be change on the string value being changed to any thing. but in my project it remains the same. it should be updated in the pwvalue textbox and in the corresponding field of the related table as well.
But running the programmed shows debug window from which I am not able
to make out anything. Please help.
thank u
 

Attachments

Users who are viewing this thread

Back
Top Bottom