• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

How to pass textbox as parameter?

Text1 is a textbox. I want to pass it as parameter, but it gets me the error below, I think this is because it treats text1 as text1.text, but how to solve it?
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Type mismatch
---------------------------
OK   Help  
---------------------------


Followed is the code,

Private Sub Command1_Click()
 Handletxt (Text1)
End Sub

Private Sub Handletxt(ByVal txt As TextBox)
 MsgBox txt.Text
End Sub
0
william007
Asked:
william007
  • 3
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You just need to remove the parenthesis around Text1:

Private Sub Command1_Click()
    Handletxt Text1
End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Or leave the parenthesis and use the Call statement:

Private Sub Command1_Click()
    Call Handletxt(Text1)
End Sub
0
 
B_M_WestonCommented:
get rid of the brackets

Private Sub Command1_Click()
 Handletxt  Text1
End Sub

Private Sub Handletxt(ByVal txt As TextBox)
 MsgBox txt.Text
End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
william007Author Commented:
Thanks,
But I am quite confusing with the bracket convention in VB.
1. What is this mean(If it do not mean it taking a parameter text1)
Handletxt (Text1)
2. Is there a guideline for when to use bracket and and when do not?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use the parenthesis when you want to STORE the return value of a function:

Private Sub Command1_Click()
    Dim str As String
    str = Foo("Echo...")
    MsgBox str
End Sub

Private Sub Command2_Click()
    Foo "Echo..."
End Sub

Private Function Foo(ByVal inValue As String) As String
     Foo = inValue
End Function



In the Command2_Click() example, we are not storing the return of Foo() so we don't need the parenthesis.

If you prefer to use parenthesis because it looks nicer but don't care about the return value, then use the Call statement:

    Call Foo("Echo...")
0
 
william007Author Commented:
Excellent, thanks:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now