By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Refer to a control through a function

100+
P: 157
Expand|Select|Wrap|Line Numbers
  1.  
  2. Function Ctrl_msgbox(control as string)
  3.  
  4. msgbox [form_1].control
  5.  
  6. end function
  7.  
  8.  
  9. sub test
  10. call ctrl_msgbox(tbx_information)
  11. end sub
  12.  
Is this possible?
Jan 30 '09 #1
Share this Question
Share on Google+
4 Replies


DonRayner
Expert 100+
P: 489
A function would be used something like this.

Expand|Select|Wrap|Line Numbers
  1. Public Function Ctrl_msgbox(control As String)
  2.     Ctrl_msgbox = Forms!form_1!(control)
  3. End Function
  4.  
  5. Sub test()
  6.     Me.MyControl = Ctrl_msgbox(tbx_information.Name)
  7. End Sub
  8.  
And a sub would be used something like this. (This is probally what you're after)

Expand|Select|Wrap|Line Numbers
  1. Public Sub Ctrl_msgbox(control As String)
  2.     msgbox Forms!form_1!(control)
  3. End Function
  4.  
  5. Sub test()
  6.     Call Ctrl_msgbox(tbx_information.Name)
  7. End Sub
  8.  
Jan 30 '09 #2

FishVal
Expert 2.5K+
P: 2,653
Or it could be something more straightforward.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Function Ctrl_msgbox(ctrl As Object)
  3.     Msgbox ctrl.Value
  4. End Function
  5.  
  6.  
  7. Sub Test
  8.     Call ctrl_msgbox(Me.tbx_information) 'I guess the sub is in form module
  9. End Sub 
  10.  
Jan 30 '09 #3

100+
P: 157
Thats how simple it is :=)
Thank you
Jan 30 '09 #4

ADezii
Expert 5K+
P: 8,638
@MrDeej
You can also pass a specific Control to a Function, query its Type, then take appropriate action as follows:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function fQueryControl(ctlControl As Control)
  3.   Select Case ctlControl.ControlType
  4.     Case acCommandButton
  5.       Debug.Print ctlControl.Name & " is a Command Button"
  6.     Case acTextBox
  7.       Debug.Print ctlControl.Name & " is a Text Box"
  8.     Case acComboBox
  9.       Debug.Print ctlControl.Name & " is a Combo Box"
  10.     Case acCheckBox
  11.       Debug.Print ctlControl.Name & " is a Check Box"
  12.     Case acImage
  13.       Debug.Print ctlControl.Name & " is an Image Control"
  14.     Case acLabel
  15.       Debug.Print ctlControl.Name & " is a Image Control"
  16.     Case acListBox
  17.       Debug.Print ctlControl.Name & " is a List Box"
  18.     Case Else
  19.       Debug.Print "Don't really care!"
  20.   End Select
  21.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim ctl As Control
  3. For Each ctl In Me.Controls
  4.   Call fQueryControl(ctl)
  5. Next
  6.  
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Command7 is a Command Button
  3. Command8 is a Command Button
  4. txtSelectedFile is a Text Box
  5. Label10 is a Image Control
  6. ShippedDate is a Text Box
  7. Label16 is a Image Control
  8. cboTest is a Combo Box
  9. Label15 is a Image Control
  10. Command17 is a Command Button
  11. Command18 is a Command Button
  12. imgTest is an Image Control
  13. chkImage is a Check Box
  14. Label21 is a Image Control
  15. Command22 is a Command Button
  16. Text25 is a Text Box
  17. Don't really care!
  18. Don't really care!
  19. Label29 is a Image Control
  20.  
Jan 30 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.