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

    Unanswered: Setting the Visibility on Multiple Tab Controls

    I have a form in which I have 6 tabs. I have been working on coding the OnChange event procedure so that when a user clicks on any tab other than the first one, they must enter a password to view or change the information. I have it working for the first tab, but am clueless how to apply this rule to the other tab controls. What happens currently is that I get a run time error 2165; you can't hide a control that has the focus.
    This has to be a simple fix. (please use simple terms, as I am a novice)Here's the code:

    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 Me.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 "Access Denied", , "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 Me.Controls
    If ctl.Tag = "*" Then
    ctl.Visible = False
    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

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so move the focus to another control, then hide the original code

    say you have 6 tabs and you want to hide all 6, called (creatively tab1, tab2, tab3, tab4, tab5 & tab6)
    then get the name of another control on your form
    lets assume its called mycontrol
    then
    mycontrol.setfocus
    tab1.visible = false
    tab2.visible = false
    tab3.visible = false
    tab4.visible = false
    tab5.visible = false
    tab6.visible = false

    the error message is telling you that you cannot change the visibility of the control that currently has the focus to invisible. IE change the focus to another control.

    if you think about it its fair enough, there is not a lot of point in hiding the control that currently has the focus without explicitly saying which control you want the focus to shift to
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Oct 2010
    Posts
    175
    Okay. I think I understand what you're saying.

    So in my situation, where in my code would I enter/change those visibility codes?

    How would I need to set that up within the code?

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    presuambly immediately before you are getting the error message you are reporting
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Oct 2010
    Posts
    175
    Could you show me? I'm not following.

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    what line reports the error?
    immediately before that block of code (ie if you are doing it in a loop BEFORE that loop starts), if the error is being reported on a single statement (ie NOT in a loop then immediately before the line of code

    I don't have time to look through your code.
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Oct 2010
    Posts
    175
    Okay. Well thanks for trying. I'm wondering if there is anyone else who could help me by looking at the short code I've posted at the beginning of this thread and tell me how to input these lines of code you suggested.

  8. #8
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    When you run your current code at what line does it fail?
    I'd rather be riding on the Tiger 800 or the Norton

  9. #9
    Join Date
    Oct 2010
    Posts
    175
    Quote Originally Posted by healdem View Post
    so move the focus to another control, then hide the original code

    say you have 6 tabs and you want to hide all 6, called (creatively tab1, tab2, tab3, tab4, tab5 & tab6)
    then get the name of another control on your form
    lets assume its called mycontrol
    then
    mycontrol.setfocus
    tab1.visible = false
    tab2.visible = false
    tab3.visible = false
    tab4.visible = false
    tab5.visible = false
    tab6.visible = false

    the error message is telling you that you cannot change the visibility of the control that currently has the focus to invisible. IE change the focus to another control.

    if you think about it its fair enough, there is not a lot of point in hiding the control that currently has the focus without explicitly saying which control you want the focus to shift to
    Hey, I know it has been a while since this post, but I never fully resolved this issue. Looking back over your responses, I think what you're telling me is that my current code doesn't specify where, besides the multiple tab control, to actually set the focus. So you're telling me to specify where to set focus.

    So I guess my only question is very simply, "where do I put the above code within my current code?" and "how do I know which control to set focus to?"

Posting Permissions

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