Opening Control Tab based on Option Group Selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minjacks
    New Member
    • Jan 2016
    • 3

    Opening Control Tab based on Option Group Selection

    Hello.
    In Access 2013, I have a control on one form that has 2 tabs. On another form, I have an option group with 2 options. I am trying to write it in VBA so that I select an option, click a button and based on the selection, the other form opens to the designated tab on the control.

    Here is what I have on the Click event of the button:
    (I build some tables upon opening the other form)

    Code:
    Private Sub cmdclose_Click()
    On Error GoTo cmdclose_Click_Err
           
        DoCmd.OpenQuery "appq_cct_date", acViewNormal
        DoCmd.OpenQuery "appq_cust_date", acViewNormal
        DoCmd.OpenQuery "appq_customer", acViewNormal
        DoCmd.OpenQuery "appq_cct", acViewNormal
            
        If frmetype = "1" Then
            DoCmd.OpenForm "frm_sec_menu", acNormal
            TabCtlSEC.Pages(0).SetFocus
        ElseIf frmetype = "2" Then
            DoCmd.OpenForm "frm_sec_menu", acNormal
            TabCtlSEC.Pages(1).SetFocus
        End If
            
        
    cmdclose_Click_Exit:
        Exit Sub
        
    cmdclose_Click_Err:
        MsgBox Error$
        Resume cmdclose_Click_Exit
        
    End Sub
    The form opens but not to the corresponding page and I am getting "Object required"

    Thanks.
    Last edited by Rabbit; Jan 14 '16, 12:26 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32653

    #2
    Three points here :
    1. I've copied your code and it works fine for me. I've assumed that the objects you've referenced are all named accurately. Possibly not in your case, as that's what I suspect is causing you the main problem.
    2. Generally, when developing code, it's easier to do it without any error handling enabled. This way you get to see (and report for us of course) which line the code fails on and the full error message. Context is all.
    3. When adding in the error handling remember that the code label names need be unique only within the procedure. Thus, it makes sense to have simpler labels like :
      Code:
      Error_Handler:
      ExitPoint:
      It gives consistency to your code and makes it easier to read.

    So, first put away the error handling until you have it working reliably, then check the spellings of all items.
    Code:
    Private Sub cmdClose_Click()
    'On Error GoTo ErrorHandler
    
        DoCmd.OpenQuery "appq_cct_date", acViewNormal
        DoCmd.OpenQuery "appq_cust_date", acViewNormal
        DoCmd.OpenQuery "appq_customer", acViewNormal
        DoCmd.OpenQuery "appq_cct", acViewNormal
    
        Select Case frmEType
        Case "1", "2"            'Are they really string values?
            Call DoCmd.OpenForm("frm_sec_menu", acNormal)
            Call TabCtlSEC.Pages(Val(frmEType)).SetFocus
        End Select
    
    ExitPoint:
        Exit Sub
    
    ErrorHandler:
        Call MsgBox(Err.Description)
        Resume ExitPoint
    
    End Sub

    Comment

    • minjacks
      New Member
      • Jan 2016
      • 3

      #3
      Thanks! Turning off the error handling was very helpful.

      The code you provided did stop the 'Object required' problem, however, it wouldn't open the form.

      This is what worked for me:
      (The frmetype "1" and "2" are the option values)

      Code:
       If frmetype = "1" Then
              DoCmd.OpenForm "frm_sec_menu", acNormal
              DoCmd.GoToControl ("...name of control page 0...")
          ElseIf frmetype = "2" Then
              DoCmd.OpenForm "frm_sec_menu", acNormal
              DoCmd.GoToControl ("...name of control page 1...")
              
          End If
      Again, thank you for the reply. Even though I didn't use your proposed code, your response was very helpful for me. Have a great day!
      Last edited by Rabbit; Jan 14 '16, 05:25 PM. Reason: Please use [code] and [code] tags when posting code or formatted data.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        If you're happier doing it that way then go for it. As I say, I found even your original code worked ok in my tests. Obviously mine did too on my system. It's hard to say what might have happened in the copying.

        At the end of the day though, you have a working solution and that's the most important thing really.

        Comment

        • mbizup
          New Member
          • Jun 2015
          • 80

          #5
          A couple of comments - I can't recall the exact circumstances, but I have also had trouble with the SetFocus method on tab controls (I'm unable to reproduce that at the moment, of course :-)). Those issues led me to use other methods to control tab pages in code.

          Tab controls have a zero based Value property, which determines the page that is open:
          Code:
          Me.myTabControl.Value = SomeInteger
          'or simply:
          Me.myTabControl = SomeInteger
          With an option group using the default settings (sequential numbers starting at 1), you can control tab pages like this. The "-1" handles the zero-based tabcontrol values (Page 1 is 0, Page 2 is 1, etc):

          Code:
          myTabControl = MyOptionGroup - 1
          Originally posted by MinJacks
          MinJacks:
          I have a control on one form that has 2 tabs. On another form, I have an option group with 2 options.
          If the option group is indeed on a separate form from the tab control, I would have thought you'd need the full form reference to the tab control in any code to get this working:

          Code:
          Forms!frm_sec_menu.TabCtlSEC = (frmetype.Value - 1)
          (If it ain't broke, don't fix it of course... this topic just piqued my curiosity.)
          Last edited by NeoPa; Jan 18 '16, 06:25 PM. Reason: Updated the quote for you Miriam.

          Comment

          Working...