Question : VBA code to prompt user to enter a password when the user wants to access a form or report in DESIGN view

Could someone kindly help me with coming up with VBA code that prompts a user to enter a valid password in a message box (with an OK and Cancel button) when the user wants to access a form or report in DESIGN view.  If the password is incorrect, the user is prompted with a message box (with OK and Cancel button) stating "Incorrect Password, Try Again" and loop back through the code until a valid password is entered.  Or the user has the option to click on the Cancel button to exit out of the message box and stop the VBA code.  If the password is correct, the user can access the form or report in design view.

Answer : VBA code to prompt user to enter a password when the user wants to access a form or report in DESIGN view

Hi sxxgupta,

My suggestion is to create hidden form which will be opened at a startup and run its code in a background.
Set its Timer Interval property to something other than 0. Say 300. This value is in milliseconds so do not make it too small to prevent processor overload by your database.

Place the snippet below into hidden form's code.
This will call Input Box to enter password when any form opened in design mode.
Find sample database attached.

Another option you might consider is using user level security, available in Access 2000 (not available in Access 2007 and 2010)

Generally speaking, Access does not provide effective security protection and my code (as well as any other solutions) can be bypassed by advanced user.

The only way to prevent user from editing forms at all, is to compile your database as mde file.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Option Compare Database
Option Explicit

Dim strDesignAllowedForm As String

Public Sub subCheckDesignPass(strPassword As String)
    Dim strForm As String, db As DAO.Database
    Dim doc As DAO.Document

    Set db = CurrentDb

    For Each doc In db.Containers("Forms").Documents
        strForm = doc.Name

        If SysCmd(acSysCmdGetObjectState, acForm, strForm) <> 0 Then
            If Forms(strForm).CurrentView = 0 And strForm <> strDesignAllowedForm Then
                    Forms(strForm).Visible = False
                    If InputBox("Enter Password") <> strPassword Then
                        DoCmd.Close acForm, strForm, acSaveYes
                    Else
                        Forms(strForm).Visible = True
                        strDesignAllowedForm = strForm
                    End If
            End If
        End If

    Next doc

    Set doc = Nothing
    db.Close
    Set db = Nothing
End Sub

Private Sub Form_Timer()
'set Forms' Timer Interval to value > 0
    Call subCheckDesignPass("pass") ' place password here
End Sub
 
Password for a Form Design View
 
Random Solutions  
 
programming4us programming4us