PDA

View Full Version : Pass form Object to Function



Bryanbmur
03-21-2009, 08:52 PM
Hello, I am trying to pass a object on a form to a function. When I click on CommandButton1 I get a type mismatch error, I am pulling my hair out about it please help! This is a proof of concept test, I just want to be able to pass a forms object to a function and have the function do something with the object.... The code is below:




Option Explicit
Private Sub CommandButton1_Click()
please Label1
End Sub

Public Sub please(lbl As Label)
MsgBox lbl.Caption
End Sub



THANKS for your help!

Oorang
03-21-2009, 10:05 PM
Might be you grabbed the wrong toolbar?
There are two types of labels in Excel, the ones on the "Control ToolBox" and the ones on the "Forms" tool bar. The ones on forms are not of the type MsForms.Label. The ones on the "Control Toolbox" are of the Label type.

Bob Phillips
03-22-2009, 05:36 AM
Option Explicit
Private Sub CommandButton1_Click()
please Label1
End Sub

Public Sub please(lbl As Label)
MsgBox lbl.Caption
End Sub

mikerickson
03-22-2009, 06:38 AM
Public Sub please(lbl As msForms.Label)
MsgBox lbl.Caption
End Sub

Bryanbmur
03-22-2009, 08:01 AM
Thanks so much, msforms.lable was the trick!

Bob Phillips
03-22-2009, 10:01 AM
I don't know what happened to mine, I was trying to point out that it needed MSForms.Label, but somehow I failed. Oh well!