databasedev.co.uk - database solutions and downloads for microsoft access

Microsoft Access Form Designs

Password Protecting A Microsoft Access Tab Control

Situations may arise where you need to ensure that some of your database users do not have access to certain information in your Microsoft Access forms. The way that you have designed your Microsoft Access User Interface will determine how you approach this. The following tutorial details how to password protect a tab contained in a Microsoft Access tab control, to only allow access to those with a valid password.

Observe the following Microsoft Access form. Here you will see a form containing a Microsoft Access tab control with two tabs:

Microsoft Access tab control, consisting of 2 tab pages

From this image, we can see that the tab control contains tabs for Personal and Company. Suppose we want to restrict access to the Company tab to only users with a valid password, we would need to include the following code in the OnChange event of the tab control:

Private Sub TabCtl0_Change()

    Dim strInput As String
    Dim ctl As Control

    ' Hide controls on tab until correct password is entered
    For Each ctl In Controls
        If ctl.Tag = "*" Then
            ctl.Visible = False
        End If
    Next ctl

    ' If tab page with Tab Index of 1 is selected
    ' show InputBox asking for password
    If TabCtl0.Value = 1 Then
        strInput = InputBox("Please enter a password to access this tab", _
                            "Restricted Access")

        ' Check if value is entered into InputBox
        ' If no value entered display MsgBox
        If strInput = "" Or strInput = Empty Then
            MsgBox "No Input Provided", , "Required Data"
            TabCtl0.Pages.Item(0).SetFocus
            Exit Sub
        End If

        ' Check InputBox value and if value is a match
        ' display tab and unhide hidden fields
        If strInput = "password" Then

            For Each ctl In Controls
                If ctl.Tag = "*" Then
                    ctl.Visible = True
                End If
            Next ctl
            ' If incorrect password supplied return to tab (index 0)
        Else
            MsgBox ("Sorry, you do not have access to this information")
            TabCtl0.Pages.Item(0).SetFocus

            Exit Sub
        End If
    End If

End Sub

This code not only prevents the tab from being displayed until the correct password is entered, it also provides a mechanism for hiding controls on the tab. If we don't hide the controls before accessing the tab, these will be visible (behind the input box) until the password is entered.

Here we see the tab page, with the input box and the controls hidden:

The InputBox, displayed when trying to access the password protected tab control page.

To hide the controls in the first place, we do this using the Tag Property, and in the OnCurrent event of the form:

Private Sub Form_Current()
    
    'Hide controls tagged with "*" until password entered.
    Dim ctl As Control
    For Each ctl In Controls
        If ctl.Tag = "*" Then
            ctl.Visible = False
        End If
    Next ctl
    
End Sub

If the correct password is entered into the InputBox, the visible properties of the tagged controls are set to True. This will then display the chosen tab, including all of the initially hidden fields:

The tab control, once the correct security password has been entered

You can test this out by downloading the Microsoft Access 2000 example file from the Microsoft Access Downloads page or the Microsoft Access Forms page.