Direct manipulation of TextBox

M

Mike NG

This is a simplified version of my code. Basically, I have a function
which takes a source string, does some manipulation, passes back a
target (result) string, and sets the function return to true or false
bases on certain conditions

Function a_test(sSource As String, sTarget As String) As Boolean

sTarget = Left(sSource, 1)
a_test = True

End Function



The workings of the function are not important. What I am doing is
calling this on the exit of one textbox to populate another textbox on a
UserForm


Now doing this will not work

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim x As Boolean
x = a_test(TextBox1, TextBox2)
End Sub


I have to use an intermediate variable instead

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim x As Boolean
Dim y as String
x = a_test(TextBox1, y)
TextBox2 = y
End Sub


I tried the first method with .Value and .Text and these didn't work.
Surely if textboxes are read/write I can manipulate them directly...or
can I?
 
B

Bob Phillips

Mike,

You should and you can. But I think you are misunderstanding how to use
functions. The function should return the result of the manipulation, to try
and manipulate an argument and return a result is a bit obtuse. This works
fine

Function a_test(sSource As String) As String
a_test = Left(sSource, 1)
End Function


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox2.Value = a_test(TextBox1)
End Sub


very simple, no intermediates.
 
T

Tom Ogilvy

That is only true if the function is used in a spreadsheet

=myfunction(a1)

Mike is using it to work with a textbox in a userform.

Regards,
Tom Ogilvy
 
M

Mike NG

Mike,

You should and you can. But I think you are misunderstanding how to use
functions. The function should return the result of the manipulation, to try
and manipulate an argument and return a result is a bit obtuse. This works
fine

Function a_test(sSource As String) As String
a_test = Left(sSource, 1)
End Function


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox2.Value = a_test(TextBox1)
End Sub
Cheers Bob

While this is going to work, what I was trying to achieve by passing
parameters back and a function return code, was so that I could do
something like

If Not a_test(TextBo1, TextBox2) Then Goto ....

I guess I will end up with the same result, a function call and an If
statement, but at least I've avoided having to declare an intermediate
variable
 
B

Bob Phillips

Mike,

If you really want to do that, try this
Function a_test(sSource As String, ByRef sTarget As MSForms.TextBox) As
Boolean
sTarget.Value = Left(sSource, 1)
a_test = True
End Function


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If a_test(TextBox1.Value, TextBox2) Then
'do your other stuff
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top