Password protecting multiple forms

Quick question though

I did everything as directed but when I put in the code:

Dim varunitnumber as integer
If me.txtpassword = dlookup("[Password]","Users","[UserID]='"&me.txtusername & "'" then
VarUnitNumber=dlookup("[Unit]","Users","[UserID]='"&me.txtusername & "'"
Docmd.close acform
docmd.openform hhform ,,,"[Unit]=" & varunitnumber
Else
msgbox "Invalid Username/Password"
Endif

Nothing really happened. I can type anything into the login form and it goes directly to the other form. What am I doing wrong?

Btw the form I'm trying to password protect in the example is hhform thus why that is in the code.
 
Sorry, you will also need a field in the Users table called Unit - Which I assume you/James might be using to identify departments (this is a number otherwise change integer to variant).

I also assume your your data form is called hhform and must have a field or some reference to 'Unit', if not include this in your datasource for the form.

If the user or password are incorrect you should be getting the message 'Invalid Username/Password' not closing the log-on form

Give it a go.

Can James shed some light on what I've missed?
 
Sorry, you will also need a field in the Users table called Unit - Which I assume you/James might be using to identify departments (this is a number otherwise change integer to variant).

I also assume your your data form is called hhform and must have a field or some reference to 'Unit', if not include this in your datasource for the form.

If the user or password are incorrect you should be getting the message 'Invalid Username/Password' not closing the log-on form

Give it a go.

Can James shed some light on what I've missed?

Do I need to enter each UserID etc into the code? Is that what I'm doing wrong?

Also the button on there that I made is programmed to go to hhform. None of the other button options seemed applicable.
 
Do I need to enter each UserID etc into the code? Is that what I'm doing wrong?

Also the button on there that I made is programmed to go to hhform. None of the other button options seemed applicable.

I need to use a toogle button right? And put that code in for that button?

The other button just takes me to the form and isn't doing anything else so I assume I need the toogle button.

When I do this it keeps going back to the code and saying syntax error.
 
No
Make a new button on the logon form. It should only have one button. Not a toggle button just a regular button and don't use the wizard. then see step 4 in the earlier post.

Also to make sure the logon form opens when you start the database go to the 'database options' section and select it as the startup display form.
 
No
Make a new button on the logon form. It should only have one button. Not a toggle button just a regular button and don't use the wizard. then see step 4 in the earlier post.

Also to make sure the logon form opens when you start the database go to the 'database options' section and select it as the startup display form.

Alright new button that is just a button and I redid the coding back to where it was prior. Now I press the button after entering correct UserID/password combo and nothing happens.

I think something is wrong with my code.
 
which On Event have you put the code in?
post the name of the user exactly table as it's spelt in your database
post the name of the fields in that table exactly as they are spelt
post the data source exactly as it's being referenced in the form
I'll then recreate everything and test it all out
 
which On Event have you put the code in?
post the name of the user exactly table as it's spelt in your database
post the name of the fields in that table exactly as they are spelt
post the data source exactly as it's being referenced in the form
I'll then recreate everything and test it all out

The on click event.

UserID:
Texas
Aggies
Sully

Password:
12345
23456
34567

Unit:
A
B
C

Really you can use whatever you want. I just am doing an example access database/form so I can figure out what to do and implement it in the real database/form.
 
Alright I have a new idea. Instead of having that whole extra database attached to a form and what not. Can I just put a button on the form I'm protecting? It doesn't matter if anyone sees it, just if they enter data into it.

I added a button and put in this code:

Dim PassWord As String
PassWord = InputBox("Enter Password")
If PassWord = "YourPassword" Then
' Open Form
Exit Sub
Else
MsgBox ("You're not authorized")
DoCmd.Close acForm, Me.Name
End If

Which works great but after I enter the password the data on the form isn't entered into the database. Is there anyway to edit this code or the button so that when the correct password is entered that the data is entered into the database?
 
Now you've lost me. If everyone’s using the same password "YourPassword" then what’s the point of having it at all. I thought you wanted different departments to have access to different but similar forms/Data.

Your code only checks that a user has entered the word "YourPassWord". Not much use in terms of security or anything else for that matter but nice way to start learning. However, if it works for you then great.

It doesn't actually open a form so replace

'Open Form with
DoCmd.OpenForm "YourFormName", acNormal
(don’t forget to change "YourFormName" to the name of your form to open but leave the speech marks).


(The ‘ is used just to comment out code or to make meaningful comments. Anything after the ‘ doesn’t actually run as part of the code.


Take care and good luck.

PS just a couple of words of advice: Remember my comment about having lots of users logging in at the same time. You absolutely MUST split the backend from the front end (separate database for the tables and another for the forms, better still everyone should have their own front end) Otherwise when, not if but when, it corrupts and you have vital information in the system you could end up in very hot water. Also be sure to make regular backups.

Don't give up because you find something difficult otherwise you'll never learn.
 
OK as promised I test the code and yes there are a missing closing brackets and the opem ofrm is in the wrong place

Use this. I've tested it and it works fine.

in the On click event of the form paste this and don't forget to replave the From names.

Dim varunitnumber As Integer
If Me.txtpassword = DLookup("[Password]", "Users", "[UserID]='" & Me.txtusername & "'") Then
varunitnumber = DLookup("[Unit]", "Users", "[UserID]='" & Me.txtusername & "'")
DoCmd.OpenForm "YourFataFormName", , , "[Unit]=" & varunitnumber
DoCmd.Close acForm, "YourPaswordFormName", acSaveNo
Else
MsgBox "Invalid Username/Password"
End If

Take care
SmallTime
 
sorry I was rushing too much. The code goes in on the on click event of the button not the FORM
 
Now you've lost me. If everyone’s using the same password "YourPassword" then what’s the point of having it at all. I thought you wanted different departments to have access to different but similar forms/Data.

Your code only checks that a user has entered the word "YourPassWord". Not much use in terms of security or anything else for that matter but nice way to start learning. However, if it works for you then great.

It doesn't actually open a form so replace

'Open Form with
DoCmd.OpenForm "YourFormName", acNormal
(don’t forget to change "YourFormName" to the name of your form to open but leave the speech marks).

(The ‘ is used just to comment out code or to make meaningful comments. Anything after the ‘ doesn’t actually run as part of the code.


Take care and good luck.

PS just a couple of words of advice: Remember my comment about having lots of users logging in at the same time. You absolutely MUST split the backend from the front end (separate database for the tables and another for the forms, better still everyone should have their own front end) Otherwise when, not if but when, it corrupts and you have vital information in the system you could end up in very hot water. Also be sure to make regular backups.

Don't give up because you find something difficult otherwise you'll never learn.

I used password as the password because it was on my EXAMPLE form.

I went about it in a different way by using something similar to the following code:

Private Sub Command0_Click()
If Text3 = "Password" Then
DoCmd.RunMacro "Macro1"
Else: MsgBox ("Please Renter Correct Password"), vbOKOnly, "Error!", 0, 0
End If
End Sub

I will do this for each form and then hide the form so this is the only way to get in. It works just fine :D

As for splitting the backend and the front end I have no idea what you are talking about. I'm planning for a form for each department that goes to a database for each department that is connected to a central database. All these will be hidden and password protected of course. This isn't set in stone as of yet, because I haven't made sure it will work.

BTW who says I was giving up? I just altered strategies.

Also thank you for your help.
 
Oh, forgot to mention you can access different versions for your forms by incorporating an IF THEN clause of better still and less confusing a SELECT CASE.

If you really want to impress then go a step further by holding the [Unit] in a hidden field on a main form and filter your queries on that on that. You can then be sure that every department only sees their own data on all the other forms and reports you'll doubtlessly be producing.
 
Hmmm


If Text3 = "Password" Then
This isn't doing much except making sure everyone punches in the word "Password". How do you intent to make sure different departments only see their own forms, which I think is where we started from.

try the above code which I posted a few minutes ago.

Back to work
See ya
 
Hmmm


If Text3 = "Password" Then
This isn't doing much except making sure everyone punches in the word "Password". How do you intent to make sure different departments only see their own forms, which I think is where we started from.

try the above code which I posted a few minutes ago.

Back to work
See ya

Dude I will change that. This is just an EXAMPLE of what I will actually use. EXAMPLE meaning that password is not the actual password I will use.

The forms will be hidden. They will have to open the password form for their own department and enter the unique password upon which the macro will take them to their form. If they open the wrong password form and try to enter the password then they won't be able to get in due to an incorrect password.

I want something that I can explain and make instructions for without much issue. Reason being is I am an intern and will be leaving in less than 2 months and if they implement this then I won't be around to fix it. This works, is relatively simple, and changing the password isn't hard at all.

Thank you for your help but this is what I'm sticking with.
 

Users who are viewing this thread

Back
Top Bottom