Hide control until password is entered (1 Viewer)

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
Hi there,

Hope someone can assist me regarding password on a tab in a form
I wish to hide content of tab until password is entered.

Below see me code which I got from access site the code works perfectly in the example but not when I use it on my form

Code Tags Added by UG
Please use Code Tags when posting VBA Code

Please read this for further information:-
https://www.access-programmers.co.u...e-use-code-tags-when-posting-vba-code.240420/
Please feel free to Remove this Comment

Code:
Option Compare Database

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

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" & vbNewLine & vbNewLine & "PASSWORD = password", _
                            "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

I am using Access 2007 and the code was written for 2002

Thanx in advance
 
Last edited by a moderator:

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
So what kind of errors are you getting, if any?
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
The controls is still visible. The password part is correct but the controls is visible
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
I think it's because you're not saying where the controls are in your 'for each ctl...'. Try putting
Code:
For Each Ctl in Me.Controls
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
Right then - so we need to cycle through each iteration of the for each statement. Try sticking a message box in the if ctl.tag="*" loop like this:
Code:
Msgbox ctl.name
That way at least we can if it's picking up the tags or not.

I'd also avoid using * as a tag, it's a reserved character in Access (it means 'everything')
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
Thinking about it it would probably be simpler to have a separate password form, that opens the form with the tabs on it if the correct password is entered.
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
I change it to msgbox ctl.name = "*" then

It gives me a Go To error

What else should I use instead of *
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
Some of the tabs contain passwords and banking details which I only want to acces and not the users who update details
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
Anything - say "Cheese" for example.

You're not quite right with the msgbox. What I meant was:
Code:
For Each ctl In Controls
If ctl.Tag = "I'm A Control" Then
Msgbox ctl.name & " is going to be invisible"
ctl.Visible = False
End If
Next ctl
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
It gives me a compile error because of the &
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
It shouldn't... what's the error? Does it still come up if you take the & " is...." out?
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
No change at all the controls still visible and no msgbox appear, What should be the outcome of this?
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
Have a read up on the usage of msgbox. You can't set it to be something, and it's not a property - it just pops a message box up with what you tell it to be in there. Inputbox is another interesting one that comes in handy all the time... for me anyway.

So how are we doing? Is it coming up with message boxes?
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
? no only box appearing is the input box for the password
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
Right so what we've done is said "For each control, check the tag - if it matches, display a message saying so and make the control invisible". So what's happening is that it's not finding a matching tag, so it's not displaying the box, and it's not setting visibility to false.

So basically go through the process of elimination and find out the problem, and fix it :)

Also I'm not sure if form_current is the right event to use here. Try form_open.
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
youve lost me. how will I know that there is a problem?
 

JamesMcS

Keyboard-Chair Interface
Local time
Today, 19:07
Joined
Sep 7, 2009
Messages
1,819
Have a read of my last post again. What we're doing is making it pop up a message box IF it finds a control with a tag of whatever you've put in. If it doesn't display a message box, it means it hasn't found a control with a matching tag - so that gives you one avenue to go down and find out why. Are there no controls with that tag? would be the first question.

Welcome to the world of debugging code....
 

joacro

Registered User.
Local time
Today, 21:07
Joined
Jan 25, 2010
Messages
12
I've got it. I didn't name the tags Just renamed it to cheese and a bunch of msgboxes appeared. Thank you for your help
 

Users who are viewing this thread

Top Bottom