Refer to a control through a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrDeej
    New Member
    • Apr 2007
    • 157

    #1

    Refer to a control through a function

    Code:
     
    Function Ctrl_msgbox(control as string)
     
    msgbox [form_1].control
     
    end function
     
     
    sub test
    call ctrl_msgbox(tbx_information)¨
    end sub
    Is this possible?
  • DonRayner
    Recognized Expert Contributor
    • Sep 2008
    • 489

    #2
    A function would be used something like this.

    Code:
    Public Function Ctrl_msgbox(control As String)
        Ctrl_msgbox = Forms!form_1!(control)
    End Function
     
    Sub test()
        Me.MyControl = Ctrl_msgbox(tbx_information.Name)
    End Sub
    And a sub would be used something like this. (This is probally what you're after)

    Code:
    Public Sub Ctrl_msgbox(control As String)
        msgbox Forms!form_1!(control)
    End Function
     
    Sub test()
        Call Ctrl_msgbox(tbx_information.Name)
    End Sub

    Comment

    • FishVal
      Recognized Expert Specialist
      • Jun 2007
      • 2656

      #3
      Or it could be something more straightforward .

      Code:
       
      Function Ctrl_msgbox(ctrl As Object)
          Msgbox ctrl.Value
      End Function
       
       
      Sub Test
          Call ctrl_msgbox(Me.tbx_information) 'I guess the sub is in form module
      End Sub

      Comment

      • MrDeej
        New Member
        • Apr 2007
        • 157

        #4
        Thats how simple it is :=)
        Thank you

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by MrDeej
          Thats how simple it is :=)
          Thank you
          You can also pass a specific Control to a Function, query its Type, then take appropriate action as follows:
          Code:
           
          Public Function fQueryControl(ctlControl As Control)
            Select Case ctlControl.ControlType
              Case acCommandButton
                Debug.Print ctlControl.Name & " is a Command Button"
              Case acTextBox
                Debug.Print ctlControl.Name & " is a Text Box"
              Case acComboBox
                Debug.Print ctlControl.Name & " is a Combo Box"
              Case acCheckBox
                Debug.Print ctlControl.Name & " is a Check Box"
              Case acImage
                Debug.Print ctlControl.Name & " is an Image Control"
              Case acLabel
                Debug.Print ctlControl.Name & " is a Image Control"
              Case acListBox
                Debug.Print ctlControl.Name & " is a List Box"
              Case Else
                Debug.Print "Don't really care!"
            End Select
          Code:
           
          Dim ctl As Control
          For Each ctl In Me.Controls
            Call fQueryControl(ctl)
          Next
          OUTPUT:
          Code:
           
          Command7 is a Command Button
          Command8 is a Command Button
          txtSelectedFile is a Text Box
          Label10 is a Image Control
          ShippedDate is a Text Box
          Label16 is a Image Control
          cboTest is a Combo Box
          Label15 is a Image Control
          Command17 is a Command Button
          Command18 is a Command Button
          imgTest is an Image Control
          chkImage is a Check Box
          Label21 is a Image Control
          Command22 is a Command Button
          Text25 is a Text Box
          Don't really care!
          Don't really care!
          Label29 is a Image Control

          Comment

          Working...