Global Password

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Global Password

Post by D Willett »

Hi. I have several area's of my database which need to be password protected.
The area's are certain forms and certain fields.
I would like a global password routine to pass into events that prevent users from access unless they have the correct password.
On opening a form I can add something like (example):

Code: Select all

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
But I don't fancy adding this into numerous events.

Does anyone know a way to add a password routine into say modUlilities ( my utilities module ) so I can call the routine whenever it is required for forms and controls?
It would make life easier. The call function would have to go into the VBA routine for each instance instead of a macro or =PasswordFunction() in the properties window.
(I think I've explained this adequately !!)

Regards
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global Password

Post by HansV »

You could create a function in a standard module:

Code: Select all

Function PasswordFunction()
    Dim PassWord As String
    PassWord = InputBox("Enter Password")
    If PassWord <> "YourPassword" Then
        MsgBox "You're not authorized", vbCritical
        DoCmd.CancelEvent
    End If
End Function
and enter

=PasswordFunction()

in the On Open event of each form where you want to ask for the password.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Global Password

Post by D Willett »

Hi Hans
Here's an instance of where I have just tested:

Code: Select all

Private Sub cmdAccounts_Click()
Call PasswordFunction
Me.sbfEST.Form.RecordSource = "qryINV"
Me.sbfEST.Form.Label22.Caption = "Invoices"
End Sub
Because the on_Click property is taken in properties by [Event Procedure] it has to go directly within the event routine in the VBE. The above prompted me for the password (which I entered wrong for the example) and then allowed the recordsource to change?

Thanks
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Global Password

Post by HansV »

Ah - I thought you wanted to use it when the form was opened. If you want to use it elsewhere, you need to change the function:

Code: Select all

Function PasswordFunction()
    Dim PassWord As String
    PassWord = InputBox("Enter Password")
    If PassWord <> "YourPassword" Then
        MsgBox "You're not authorized", vbCritical
        DoCmd.Close acForm, Application.CodeContextObject.Name
    End If
End Function
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Global Password

Post by D Willett »

Great stuff Hans, works fine now.

Regards
Cheers ...

Dave.