Code for Password protecting tabs (1 Viewer)

YNWA

Registered User.
Local time
Today, 16:54
Joined
Jun 2, 2009
Messages
905
Hi,

I have this code that works. I have a form with 3 tabs. When I select the middle tab I am prompted for password but if I click the 3rd tab I am not.

How do I fix this?

Code is:

Code:
Private Sub TabCtl9_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 TabCtl9.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"
            TabCtl9.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")
            TabCtl9.Pages.Item(0).SetFocus

            Exit Sub
        End If
    End If

End Sub

Thanks
 

YNWA

Registered User.
Local time
Today, 16:54
Joined
Jun 2, 2009
Messages
905
Also my tab control is TabCtl9 but the 2 tabs I need passwords is ContractsTb and InvoicesTb.

Currently only asks for password when I click on the ContractsTb.
 

missinglinq

AWF VIP
Local time
Today, 11:54
Joined
Jun 20, 2003
Messages
6,423
If I understand you correctly, and you want the same password for the second and the third Page of the Tabbed Control, replace

If TabCtl9.Value = 1 Then

with

If (TabCtl9.Value = 1) Or (TabCtl9.Value = 2) Then

Linq ;0)>
 

YNWA

Registered User.
Local time
Today, 16:54
Joined
Jun 2, 2009
Messages
905
If I understand you correctly, and you want the same password for the second and the third Page of the Tabbed Control, replace

If TabCtl9.Value = 1 Then

with

If (TabCtl9.Value = 1) Or (TabCtl9.Value = 2) Then

Linq ;0)>

Works perfect.

Out of interest (and if client requests it), how would I code it if I needed to enter different passwords for each tab?
 

YNWA

Registered User.
Local time
Today, 16:54
Joined
Jun 2, 2009
Messages
905
Hi,

I have this working but I have a problem.

I have form with 3 tabs and I need password on 2 of them which the code above works. However I need to hide the fields when prompting for password.

I entered the code from databasedev tutorial along with this from there also:
Code:
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

It works fine. But the problem I have is if I click Contract tab enter password and it opens but if I click the Invoices tab I get an error

"Runtime error 2165". You cant hide a control that has the focus.

Any ideas whats going on here?

Thanks
 

missinglinq

AWF VIP
Local time
Today, 11:54
Joined
Jun 20, 2003
Messages
6,423
...Any ideas whats going on here...

Sure, you're trying to 'hide a control that has focus!' :D

You need to set the Focus to Control that doesn't have the * in its Tag Property!

If you don't have such a Control handy,
  1. Add a Textbox to the upper, left-hand portion of the Detail Section of you Form
  2. Go into Properties - Other and change the Name Property to FauxControl
  3. Select the Control and reduce it in size to a pinpoint
No replace your code with this:

Code:
Private Sub Form_Current()

    [COLOR="Red"]FauxControl.SetFocus[/COLOR]

    '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

Linq ;0)>
 

YNWA

Registered User.
Local time
Today, 16:54
Joined
Jun 2, 2009
Messages
905
Still getting the error mate.

When I debug it says the error is in this sections of other code:

Code:
Private Sub TabCtl9_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
           [COLOR="Yellow"] ctl.Visible = False[/COLOR]
        End If
    Next ctl

    ' If tab page with Tab Index of 1 is selected
    ' show InputBox asking for password
    If (TabCtl9.Value = 1) Or (TabCtl9.Value = 2) 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"
            TabCtl9.Pages.Item(0).SetFocus
            Exit Sub
        End If

        ' Check InputBox value and if value is a match
        ' display tab and unhide hidden fields
        '-----> password line below
        If strInput = "managementpass" 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")
            TabCtl9.Pages.Item(0).SetFocus

            Exit Sub
        End If
    End If

End Sub
 

missinglinq

AWF VIP
Local time
Today, 11:54
Joined
Jun 20, 2003
Messages
6,423
Once again, you have to place the Focus on a Control that doesn't have the asterisk Tag, before using the code to set the other Controls to Visible = False:

Code:
Private Sub TabCtl9_Change()

    Dim strInput As String
    Dim ctl As Control

    ' Hide controls on tab until correct password is entered 
    
    [COLOR="Red"]FauxControl.SetFocus[/COLOR]

    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 (TabCtl9.Value = 1) Or (TabCtl9.Value = 2) 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"
            TabCtl9.Pages.Item(0).SetFocus
            Exit Sub
        End If

        ' Check InputBox value and if value is a match
        ' display tab and unhide hidden fields
        '-----> password line below
        If strInput = "managementpass" 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")
            TabCtl9.Pages.Item(0).SetFocus

            Exit Sub
        End If
    End If
 
Last edited:

YNWA

Registered User.
Local time
Today, 16:54
Joined
Jun 2, 2009
Messages
905
Last issue. The above is sorted but I have something else...

When client opens the main form they are prompted for a password using this code in the Main Form code:

Code:
Private Sub Form_Load()
Dim pwd As String
pwd = InputBox("Authorized Users Only. Please enter password.")
If pwd = "mypass" Then
MsgBox "Access granted. Welcome to the Hotel Energy Database."
Else
MsgBox "Incorrect password entered. Check that you are authorized to access this area."
DoCmd.Close
End If
End Sub

On this form is also the TABBED password code as well. The other 2 tabs contain subforms.

Now the tab password works fine and hides subforms etc..

However I have a search form that opens a subform found in say TAB 2. When double clicking the record in search results, the subform loads and no password is needed.

Yet if I use the code above in it, then when I open the MAIN form, I need to enter a password for each of the subforms within it that have this code above (eg. 3 times).

Any ideas? Can I password protect a double click event?
 

Users who are viewing this thread

Top Bottom