How do I call a switchboard from within a code module?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sueb
    Contributor
    • Apr 2010
    • 379

    How do I call a switchboard from within a code module?

    I have a piece of code that is triggered from a switchboard, asks for a password, and then opens a form.

    I'd like it to open its own switchboard. What's the syntax for that?
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi Sue. It depends what you mean by 'switchboard'. If you have designed a custom switchboard form yourself it can be opened like any other form by using the DoCmd.OpenForm method in VBA from within whatever form or code segment you want to launch it.

    If you mean you want to open or customise a version of the standard wizard-designed switchboard the short answer is that you would be ill-advised to do so, as the different switchboard 'pages' you see in the switchboard are actually code-customised variations on the same page, with changes to the captions and responses for each different page or subpage displayed according to values stored within a switchboard options table built by the switchboard wizard routines.

    It is possible to customise standard switchboards to some degree, but the basic functionality of the button choices and options displayed can't be altered without losing the ability to manage the switchboard using the built-in wizard.

    -Stewart

    Comment

    • sueb
      Contributor
      • Apr 2010
      • 379

      #3
      I don't want to modify the basic functionality--I just want to create a switchboard "sub-page" that only gets call through a little piece of code.

      Here's the picture: I have a regular, main switchboard, with sub-pages and all, and one of the choices calls a piece of code that asks for a password. Once the user has entered the correct password, I'd like to put up a sub-page that has a couple of choices on it. (These choices would each just open a form.)

      That's it. I'd continue to manage all the switchboard pages through the built-in wizard, and I don't need them to do anything tricky.

      What do you think?

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        In general it is not possible to do what you ask, Sue, as the switchboard sub-pages are NOT independent. There is only one form, and the sub-menus you see are all arising from the switchboard's wizard-created code in conjunction with the table storing all of the switchboard options.

        If you look at the switchboard form's design it appears as a set of uncaptioned blank buttons. The switchboard table created by the switchboard wizard in conjunction with the active code on the form determines which 'page' is currently shown, which buttons to make visible, the captions for the buttons, and the options which are executed in response to a particular button being pressed.

        Although it would be possible to execute commands to force the switchboard to execute a particular option, in general this would mean you could not use the built-in 'manage switchboard' options to assist you any further if you go this route.

        -Stewart

        Comment

        • sueb
          Contributor
          • Apr 2010
          • 379

          #5
          I'm not clear on how calling a switchboard page would turn off the wizard. Could you help me understand that?

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            I think there may be a 'rogue' method of conditionally opening another Wizard Generated Switchboard from a Main Switchboard. I've done something similar in the past, give me a little time and I'll see if I can come up with the Code.

            Comment

            • sueb
              Contributor
              • Apr 2010
              • 379

              #7
              Thanks, ADezii!

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                1. I did come up with a little trickery that will enable you to Open another 'Wizard Generated Switchboard' only if the correct Password is entered. I did this by adding additional Code to the HandleButtonCli ck() Function in the Form Module which will:
                  1. Check the Switchboard Caption and which Button was clicked. Only 1 exact combination of each exists that will Open another Switchboard. In this case is is Button 1 on the Main Switchboard to Open the YaDa - YaDa Switchboard.
                  2. If the Password is exact, the Code will fall through and proceed as normal.
                  3. If no Password is entered or it is incorrect, Exit the HandleButtonVCl ick() Event.
                  4. Notice the additional Code in Code Line #5 thru 30, does it look familiar?
                  5. While you are at it, Uncomment Line #136 as illustrated.

                  Code:
                  Private Function HandleButtonClick(intBtn As Integer)
                  ' This function is called when a button is clicked.
                  ' intBtn indicates which button was clicked.
                  
                  '*************************************************************************************
                  'If this Routine is being called from the 'Main Switchboard', AND the
                  'Button Clicked is '1', User is attempting to Open the YaDa - YaDa Switchboard
                  If intBtn = 1 And Me.Caption = "Main Switchboard" Then
                    Dim strMsg As String
                    Dim strResponse As String
                    Const conPASSWORD As String = "ZeBrA"
                  
                    strMsg = "A Password is required in order to Open the YaDa-YaDa Switchboard." & _
                              vbCrLf & vbCrLf & "Enter the Password in the space provided below, " & _
                             "being advised that the Password is Case Sensitive."
                           
                    strResponse = InputBox$(strMsg, "Password Prompt")
                  
                    If strResponse = "" Then Exit Function      'Cancel or OK with no entry
                  
                    'Look for an 'exact match to ZeBrA (Case Sensitive)
                    If StrComp(conPASSWORD, strResponse, vbBinaryCompare) = 0 Then
                      'Allow Code to fall through and proceed
                    Else
                      MsgBox "The Password you entered (" & strResponse & ") is not correct!", _
                              vbCritical, "Incorrect Password"
                                Exit Function
                    End If
                  End If
                  '*************************************************************************************
                      ' Constants for the commands that can be executed.
                      Const conCmdGotoSwitchboard = 1
                      Const conCmdOpenFormAdd = 2
                      Const conCmdOpenFormBrowse = 3
                      Const conCmdOpenReport = 4
                      Const conCmdCustomizeSwitchboard = 5
                      Const conCmdExitApplication = 6
                      Const conCmdRunMacro = 7
                      Const conCmdRunCode = 8
                      Const conCmdOpenPage = 9
                  
                      ' An error that is special cased.
                      Const conErrDoCmdCancelled = 2501
                      
                      Dim con As Object
                      Dim rs As Object
                      Dim stSql As String
                  
                  On Error GoTo HandleButtonClick_Err
                  
                      ' Find the item in the Switchboard Items table
                      ' that corresponds to the button that was clicked.
                      Set con = Application.CurrentProject.Connection
                      Set rs = CreateObject("ADODB.Recordset")
                      stSql = "SELECT * FROM [Switchboard Items] "
                      stSql = stSql & "WHERE [SwitchboardID]=" & Me![SwitchboardID] & " AND [ItemNumber]=" & intBtn
                      rs.Open stSql, con, 1    ' 1 = adOpenKeyset
                      
                      ' If no item matches, report the error and exit the function.
                      If (rs.EOF) Then
                          MsgBox "There was an error reading the Switchboard Items table."
                          rs.Close
                          Set rs = Nothing
                          Set con = Nothing
                          Exit Function
                      End If
                      
                      Select Case rs![Command]
                          
                          ' Go to another switchboard.
                          Case conCmdGotoSwitchboard
                              Me.Filter = "[ItemNumber] = 0 AND [SwitchboardID]=" & rs![Argument]
                              
                          ' Open a form in Add mode.
                          Case conCmdOpenFormAdd
                              DoCmd.OpenForm rs![Argument], , , , acAdd
                  
                          ' Open a form.
                          Case conCmdOpenFormBrowse
                              DoCmd.OpenForm rs![Argument]
                  
                          ' Open a report.
                          Case conCmdOpenReport
                              DoCmd.OpenReport rs![Argument], acPreview
                  
                          ' Customize the Switchboard.
                          Case conCmdCustomizeSwitchboard
                              ' Handle the case where the Switchboard Manager
                              ' is not installed (e.g. Minimal Install).
                              On Error Resume Next
                              Application.Run "ACWZMAIN.sbm_Entry"
                              If (Err <> 0) Then MsgBox "Command not available."
                              On Error GoTo 0
                              ' Update the form.
                              Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
                              Me.Caption = Nz(Me![ItemText], "")
                              FillOptions
                  
                          ' Exit the application.
                          Case conCmdExitApplication
                              CloseCurrentDatabase
                  
                          ' Run a macro.
                          Case conCmdRunMacro
                              DoCmd.RunMacro rs![Argument]
                  
                          ' Run code.
                          Case conCmdRunCode
                              Application.Run rs![Argument]
                  
                          ' Open a Data Access Page
                          Case conCmdOpenPage
                              DoCmd.OpenDataAccessPage rs![Argument]
                  
                          ' Any other command is unrecognized.
                          Case Else
                              MsgBox "Unknown option."
                      
                      End Select
                  
                      ' Close the recordset and the database.
                      rs.Close
                      
                  HandleButtonClick_Exit:
                  On Error Resume Next
                      Set rs = Nothing
                      Set con = Nothing
                      Exit Function
                  
                  HandleButtonClick_Err:
                      ' If the action was cancelled by the user for
                      ' some reason, don't display an error message.
                      ' Instead, resume on the next line.
                      If (Err = conErrDoCmdCancelled) Then
                          Resume Next
                      'ElseIf Err.Number = -2147352560 Then    'Nothing
                      Else
                          MsgBox "There was an error executing the command." & Err.Number, vbCritical
                          Resume HandleButtonClick_Exit
                      End If
                  End Function
                2. Download the Attachment to really see what is going on.
                Attached Files

                Comment

                • sueb
                  Contributor
                  • Apr 2010
                  • 379

                  #9
                  Thanks for this, ADezii, but I think you have just convinced me that I don't want to do this! This is way complicated for the benefit of going outside the switchboard scheme. I guess I'll just not password-protect the new menu option. It's not like people aren't allowed to access it, but some people wouldn't know what to do with it if they stumbled into it.

                  I just thought there would be a simple way to insert a checkpoint on menu options.

                  Thanks anyway, though. I'm going to mark your answer, since it does actually answer the need. That way people will know how to do this if they have a crying need.

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    Stewart addressed the issue very eloquently in Post #4 in that Wizard Generated Switchboards are not independent entities, but a Single Form dynamically re-generated based on certain conditions. Given this, I do believe that there is no 'simple' solution to your question, only that which I have provided for you.

                    Comment

                    • sueb
                      Contributor
                      • Apr 2010
                      • 379

                      #11
                      Yes, I guess I "get" that now. :D

                      I was assuming a very different implementation, I think--sort of like just a series of linked forms or something. Hmmm... I wonder if something like that would work for me? I mean, what if I created forms that behaved like a switchboard? Maybe I'll look into that...

                      Comment

                      Working...