MS Access VBA Articles

Password Protected MS Access Tab Control


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

See the following Microsoft Access form. Here is a MS Access Form containing a Microsoft Access tab control with two tabs:

MS Access Password protected Tab Control

Fig- 1.1

From this image, we can see that the tab control contains tabs for Personal and Company informations. Suppose we want to restrict access to the Company tab so that the users having a valid password can view the company informations. For this we added the following code in the OnChange event of the tab control:

Private Sub Tab_Informations_Change()
Dim strInput As String
Dim ctl As Control
' Do not show the controls on Company tab until correct password is entered.
For Each ctl In Controls
If ctl.Tag = "*" Then
ctl.Visible = False
End If
Next ctl
' If company tab with page Tab Index =1 is selected, prompt InputBox for password
If Tab_Informations.Value = 1 Then
strInput = InputBox("Enter a password to access this tab" & vbNewLine & vbNewLine & "PASSWORD = 12345", _ "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"
Tab_Informations.Pages.Item(0).SetFocus
Exit Sub
End If
' Check InputBox value and if password matched then
' display tab and show the hidden fields
If strInput = "12345" Then
For Each ctl In Controls
If ctl.Tag = "*" Then
ctl.Visible = True
End If
Next ctl
' If incorrect password entered, return to personal tab
Else
MsgBox ("Sorry, you do not have access to this information")
Tab_Informations.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 the 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 are hidden:

MS Access Password protected Tab Control

Fig- 1.2

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 we enter correct password into the InputBox, the visible properties of the tagged controls are set to True. This will then display the Company tab, including all of the initially hidden fields:

MS Access Password protected Tab Control

Fig- 1.3

If we click cancel button then it prompt a message "No Input Provided" and personal tab will be opened.Company tab remain protected untill we enter correct password.

MS Access Password protected Tab Control

Fig- 1.4.1

MS Access Password protected Tab Control

Fig- 1.4.2


DISCLAIMER

It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. Access Guru will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.