We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to pass textbox as parameter?

william007
william007 asked
on
Medium Priority
318 Views
Last Modified: 2010-05-18
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
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
You just need to remove the parenthesis around Text1:

Private Sub Command1_Click()
    Handletxt Text1
End Sub

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Or leave the parenthesis and use the Call statement:

Private Sub Command1_Click()
    Call Handletxt(Text1)
End Sub
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

Author

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?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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...")

Author

Commented:
Excellent, thanks:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.