5 Replies Latest reply on Jan 24, 2017 3:34 AM by Jaya

    Passing object by Value

    Jaya

      Hi

       

      I use the below code for formatting a bunch of command buttons.

       

      Private Sub fmtCmdBtn(ByVal obj As commandbutton, msg As String,  fillcolor, txtcolor As Double)

          With obj

              .Caption = msg

              .BackColor = fillcolor

              .ForeColor = txtcolor

           End With

      End Sub

       

      There are no errors when I use :

      fmtCmdBtn  btnTest1  "Test"  Vbred,  vbwhite '         where btnTest1 is the name of the command button

       

      However, I get a Type Mismatch error when I substitute a variable for the object.

       

      Dim myobj as Object  ' Tried  Symbol also with same error

       

      Set myobj = ThisDisplay.Symbols.Item(6)

      or

      Set myobj= ThisDisplay.Symbols("btnTest1")

       

      fmtCmdBtn  myobj  "Test"  Vbred,  vbwhite

       

      Substituting the name of the button with a variable throws up Type mismatch error. What am I doing wrong? Any help appreciated. Thanks in advance.

        • Re: Passing object by Value
          stang

          Hi Jay,

           

          This is because selecting it using ThisDisplay.Symbols will coerce it into a ProcBook Symbol, not a MS Forms CommandButton.

          I'm pretty sure we can't typecast in VBA, so we'd have to find some way to access the collection of non-symbol controls on the display.

          I'm not actually sure how to do this, I will look into it when I have some time; meanwhile maybe someone else knows.

            • Re: Passing object by Value
              Jaya

              Thanks Samuel. I take your point. However, when a ListBox can be considered as a Symbol in Symbols collection, why not a CommandButton? The below quote is from the Processbook reference manual. Any thoughts? Thanks

               

              Quote:

               

              A control that exists in a display can be manipulated with the control's properties and methods by referring to the control object by name as a property of the display, e.g., ThisDisplay.ListBox1. The control may also be manipulated as one of the members of the Symbols collection. The symbol interface for an ActiveX control symbol can be pulled out of the Symbols collection by name or z-order index, e.g., ThisDisplay.Symbols("ListBox1") or ThisDisplay.Symbols(<n>).

                • Re: Passing object by Value
                  gregor

                  Hi Jay,

                   

                  your method works for me when I call it like this:

                   

                      fmtCmdBtn CommandButton1, "Test", vbGreen, vbBlack
                  

                   

                  For ProcessBook all ActiveX Controls are of the same type. You can iterate through them like this

                   

                  Private Sub CommandButton1_Click()
                      Dim sym As Symbol
                      Dim sSymbols As String
                      For Each sym In ThisDisplay.Symbols
                          sSymbols = sSymbols & sym.Name & " - " & CStr(sym.Type) & vbCrLf
                      Next
                      MsgBox sSymbols
                  End Sub
                  
                  1 of 1 people found this helpful
              • Re: Passing object by Value
                Br@dy

                Hi Jay,

                I'm having a similar issue where I am trying to dynamically pass a button name through to an object so I can reference it to assign it's attributes.

                Have you had any luck finding a resolution for the issue?

                Cheers.