Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2010
    Posts
    175

    Unanswered: Code troubles with password protecting a tab control

    Hello. I have a tab control with two tabs. I'd like to password protect the second tab. Nothing is happening when I enter the code. Here is the code I'm using:

    Private Sub TabCtl1076_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

  2. #2
    Join Date
    Oct 2010
    Posts
    175
    I fixed the code. It's working now. I forgot to change the names of the tab ctrl.

    Anyway, now the only thing not working is the invisibility. Here's what I have:

    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

    Any thoughts?

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I would have all of the users log into the application, and only show the tab if that were appropriate for the user. It seems a lot easier to hide/show a tab than to try to manage all of the controls.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  4. #4
    Join Date
    Oct 2010
    Posts
    175
    Agreed, but my preference is to just password protect the one tab.

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    put a breakpoint on the code
    step through i tline buy line
    make certain its doing what you expect as opposed to what you think its doing or what you think you have instructed the compiler to do.
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    my guess is the code is in the wrong event

    Agreed, but my preference is to just password protect the one tab.
    so you are going to ask for this password each time the user clicks this tab?

    your users are going to be chuffed to buckets at that design approach, frankly its user hostile.

    far more sensible to use the network log on (see dev asish API calls) fidn out what user group(s) that logon is entitled to and take action accordingly. that way round the user NEVER has to enter a password in your application, NEVER has to change a password in your application (so there is no risk of compromising the application).

    you are repeating code
    push the visibility code to a sub
    eg
    private sub SetVisibility(NewState as boolean)
    For Each ctl In Controls
    If ctl.Tag = "*" Then
    ctl.Visible = NewState
    End If
    next ctl
    end sub
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Oct 2010
    Posts
    175
    The only user who will have to access the tab is me. So I'm not planning to change my security setup for this.

    I have the code in the "on change" event.

    What is meant by push the visibility code to a sub?

  8. #8
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    use that sub
    then call it as

    SetVisibility(true)
    or
    SetVisibility(false)
    I'd rather be riding on the Tiger 800 or the Norton

  9. #9
    Join Date
    Oct 2010
    Posts
    175
    do you mean input that into my current code, or start a new section of code?

  10. #10
    Join Date
    Oct 2010
    Posts
    175

    Trouble setting visibility on tab control

    Hello. I can't seem to figure out why this code isn't working. I have password protected a tab, but can't get the tab control to hide the controls until the correct password is entered. Any thoughts?

    Private Sub TabCtl1076_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 TabCtl1076.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"
    TabCtl1076.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")
    TabCtl1076.Pages.Item(0).SetFocus

    Exit Sub
    End If
    End If

    End Sub

  11. #11
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    please dont start a new thread duplicating an earlier thread
    I'd rather be riding on the Tiger 800 or the Norton

  12. #12
    Join Date
    Oct 2010
    Posts
    175
    Actually, I'm not duplicating the previous thread. I didn't get an answer to this question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •