How to write set of code for password in module

Sangpo

Registered User.
Local time
Tomorrow, 05:44
Joined
Jul 7, 2012
Messages
35
Dear all,

I'm self learning ms access Person. I am in the course of learning more about module.I understood that module is used when we think that there is repeated codes to be use. meaning to reduce the repeated codes. I have Following set of code for allotting password for opening different forms or tables or queries for security reasons. But it is painful to write same codes for different names of form, table, queries.I wonder if the following code can be written in module so that i can call for difrent forms (such as password for opening form named"frmAddrecord,frmDeleteRecord,frmEditrecord")

Therefore I request you all to help me so that i dont have to write same code for password to open different names of form.
Thank you
Sangpo

********
Private Sub cmdOK_Click()
Set db = CurrentDb
Set rs = db.OpenRecordset("UserT", DB_OPEN_DYNASET)
rs.FindFirst "password='" & CStr(textpw) & "'"
If rs.NoMatch Then
MsgBox ("Sorry, Your Password is incorrect.")
Else
DoCmd.Close acForm, "PasswordaddF", acNosave
DoCmd.OpenForm ("addRecordF")
End If
End Sub
 
I am not sure what you are trying to do (or) how you achieve user authentication..
But if you want you can have the module take in an argument; which will be the FormName and use it to open the Form. something along the line of
Code:
Public Sub openForm(frmName as String)
Then while using to open a form use it as,
Code:
DoCmd.OpenForm (frmName)
OR
You can also return a boolena true or false from the 'user authentication' module; based on the return value you can open the Form or just give a error message..
 
I am sure nobody else could understood what I mean to say in my earlier statement. Now I am going to expalin in other way.

I actually wanted to write code for password to open form named , frmAddRecord and frmDeleteRecord. When I click control button named AddRecord, I want that it prompts asking to enter password. and as well for opening frmDeleteRecord.

I have created a table named userT for storing password. ( in the table i have these fields, id , password ( numeric and text type) Therefore I would like to request yuo all to help me to write password codes , so that some one need to enter password to open above forms ie. frmAddRecord and frmDeleteRecord. Please help me here.

sangpo
 
Okay Sangpo, I think I know what you mean now.. So you want is an Input Box with Password field.. And the value returned should be compared against the value in the Table.
Follow these simple steps..
STEP 1: Create an Unbound Form call it frmPassword_Box, with a Text field, two buttons OK and CANCEL. On the on click event of the method 'CANCEL' button close the form, on the on click method of 'OK' button make the FORM invisible.
STEP 2: In design mode change the Input Mask of the Text Box to Password.
STEP 3: Create a Function inside a module call it Call_Password_Box, then enter the code inside
Code:
DoCmd.OpenForm FormName:="frmPassword_Box", _
    View:=acNormal, _
    WindowMode:=acDialog,               
  [COLOR=YellowGreen]  [B][COLOR=SeaGreen]'Call to open the Form[/COLOR][/B][/COLOR]
    
If CurrentProject.AllForms![frmPassword_Box].IsLoaded Then
    Call_Password_Box = Forms![frmPassword_Box]!Text3.Value 
    [B][COLOR=SeaGreen]'The Password is obtained to be forwarded to the method that made the call.[/COLOR][/B]
    DoCmd.Close acForm, "frmPassword_Box"
Else
    Call_Password_Box = vbNullString          
    [B][COLOR=SeaGreen]'If Closed or Cancel is pressed a NULL String is returned.[/COLOR][/B]
End If
STEP 4: Save all changes, compile to see if there are any errors.
STEP 5: Call the method Call_Password_Box to make users enter password.

My Question,
* How will you identify users here? If you have more than one user; then in the design mode you might want to use maybe a combo box to list all users and based on the selection type the password.
* If each user is identified by individual password, using DLookUp to match ID with password will be more easier for you that using a RecordSet object.

Please post back if you find any trouble with this.
 
Last edited:
Dear Eugin Sir,

As suggested by you i have written following code inside module.Then I tried to call this function from frmpassword_box Where the code are written like this.
Private Sub cmdOK_Click()
Form Visible
Call_Password_Box
'("addRegistrationF")
'password , DoCmd.OpenForm("addRegistrationF")
End Sub) Here I get error message. Invalid use of property and cursor point the Form. This for your further support

*****************************
Public Function Call_Password_Box()
DoCmd.OpenForm FormName = "frmPassword_Box"
View = acNormal
WindowMode = acDialog
'Call to open the Form

If CurrentProject.AllForms![frmPassword_Box].IsLoaded Then
Call_Password_Box = Forms![frmPassword_Box]!Text3.Value
'The Password is obtained to be forwarded to the method that made the call.
DoCmd.Close acForm, "frmPassword_Box"
Else
Call_Password_Box = vbNullString
'If Closed or Cancel is pressed a NULL String is returned.
End If
End Function
 
Hello Sangpo, The way you are calling the function is not right.. The function I have given will return a text or a null string back to the function that made the call.. So..
Code:
Private Sub cmdOK_Click()
	If Call_Password_Box="addRegistrationF" Then
		DoCmd.OpenForm("addRegistrationF")
	Else
		MsgBox("The Password entered is wrong, you cannot open the from")
	End If
End Sub
Only if the password entered at the prompt is equal to "addRegistrationF" the DoCmd method will open the Form.
 
What is not working? does the Password Box atleast appear? Could you please let me know if you are getting any error? If so what is the error and where.
 
Sir,


Yes I get password box . but when I click Ok I get error message as InValid use of properties

If I remove the code Form visible
then error message i get is as argument not optional .

Can pleaese the following code if I have written in sequence?



Public Function Call_Password_Box(frmPassword_Box)
FormName = "frmPassword_Box"
View = acNormal
WindowMode = acDialog
OpenArgs = strOpenArgs
'Call to open the Form

If CurrentProject.AllForms![frmPassword_Box].IsLoaded Then
Call_Password_Box = Forms![frmPassword_Box]!text3.Value
'The Password is obtained to be forwarded to the method that made the call.
DoCmd.Close acForm, "frmPassword_Box"
DoCmd.OpenForm = "frmPassword_Box"
Else
Call_Password_Box = vbNullString
'If Closed or Cancel is pressed a NULL String is returned.
End If
End Function
 
The problem is because you have changed the code, that I gave you. If you want to make changes fine, but make sure what you are changing.. So try the following code..
Code:
Public Function Call_Password_Box()
    DoCmd.OpenForm FormName:= "frmPassword_Box", _
    View := acNormal, _
    WindowMode := acDialog
    'Call to open the Form
    If CurrentProject.AllForms![frmPassword_Box].IsLoaded Then
        Call_Password_Box = Forms![frmPassword_Box]!text3.Value
        'The Password is obtained to be forwarded to the method that made the call.
        DoCmd.Close acForm, "frmPassword_Box"
    Else
        Call_Password_Box = vbNullString
        'If Closed or Cancel is pressed a NULL String is returned.
    End If
End Function
As mentioned earlier, the Form Visible property to be set to false inside the on_click event of the OK button.. You have to write the coding as...
Code:
Private Sub OK_OnClick()
        Forms!frmPassword_Box.Visible=False
End Sub
Private Sub Cancel_OnClick()
        DoCmd.Close acForm, "frmPassword_Box"
End Sub
Please wrap your code by using the
Code:
 tags..
 
Last edited:
Eugin Sir,


I gave copied the code you have provide. Now no action when I click ok button and same when i enter the password also. when I click the cancel button the form closes.

My password is store in table called userT and password is dra. So where in below mention Dlookup code insert. ? Thank for your untired support to solve my problems.


If DLookup("count(*)", "UserT", "password= '" & CStr(textpw) & "'") = 0 Then
MsgBox "Sorry, Your Password is incorrect."
 
What are you trying to do here again? If you want to fins the count, why dont you use DCount?
If DLookup("count(*)", "UserT", "password= '" & CStr(textpw) & "'") = 0 Then
MsgBox "Sorry, Your Password is incorrect."
I am confused. Could you please upload a copy of your DB, by removing all sensitive information? So I can look at what you are actually trying to achieve? If not a copy of the actual, try recreating a mock version.. I would like to help you but I think we are in different pages.
 
i have forgot to add ome more button the main form. so im uploading the db gain for better understanding of problems.

thank you sir
 

Attachments

Hello Sangpo, I have made the changes now the Form works fine.. It approves a password, if cancel is pressed it shows a message box.. Check the attachment...

Well you have followed my code very blindly, You have to change the variables to what you have declared as, example you have your OK button name as 'cmdOK' I showed OK_OnClick as an example of what it should be.. Also you have removed , _ which is what is used for breaking the control of a single sentence. instead of writing DoCmd.OpenForm frmPassword_Box, acNormal, , , acDialog we write it so it is understandable.

You did not make the call behind the button click at all that is the reason you did not have the frmPassword_Box prompting, you have to adapt the code presented to what your Form looks like, what names you call the controls as..

Hope this helps..
 

Attachments

Thank you sir, I have downlaod the corrected db of mine. But here, it is not opening the form called addRegistrationF.

Refer the mainMenuRegistrationF. Here when I click add registration, button, it must prompt for password input. If the password i have stored in the table userT matches with the one we input in password box must open the form called addregistrationF. Like wise if i click the add mah button, it it must prompt to input password and the table named called mahT must be opened it the passwored in the userT matched with the one we input.



Please look into this too.

thank u La
 
At the moment almost anyone can get into every part of your Database.

What other methods are you going to employ to secure it.

Do you not think that this is not very user friendly. People will soon tire of typing in their password.
 
sir this is for both security reason and learning . No matter sir for getting into very part of my db, because is for pr acting only. I think many people like me who is learning VBA access would be benefited and all credits owe by you . Thank you
 
Dear Sangpo, I have looked into your DB made all changes that will quiet be enough for you work along from here. If you want the password box to pop up then on click event of the button write the code that I have places behind the other buttons. If you want to compare with the Password list inside the table use a DLookUp, but on the design of your form and Password table it is not what you want to achieve.

You have userID and Password both same, and in that case no two person can have the same password.. If they do, how are you going to identify them? Everything you do should be concrete for a longer time.. If you still want to see if the entered password is in the table use a DCount in the IF condition as,
Code:
If DCount("passWord","UserT","passWord= '" & Call_Password_Box() & "'") <>0 Then
        DoCmd.OpenForm [I][B]frmName[/B][/I]
Else
        Call MsgBox("WRONG PASSWORD", vbCritical)
End IF
 
Sir, eugin,

Not it works fine. so Thumbs up for you. all those in learning stage surely must have benefited and thank you so much. I applogise for wasting most of your valuable time making me to understand. U know since it is self learning, it very difficult under the programming terms .

Thank you.
 

Users who are viewing this thread

Back
Top Bottom