Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Qucikie. What's wrong with this code? 1

Status
Not open for further replies.

MFFL

Vendor
Nov 20, 2002
26
US
code snippet
------------
varNumStr = Str(varNum)
varNumStr = varNumStr + varMsg
msgBox varNumStr
Me.Title = varNumStr
----------------------------------------

me.title is a textbox in a report
but it says i can't put that value in it.
msgbox has no problem concatenating the two strings.

whats the obvious answer i can't see?
MMFL-
Mavericks Fan For Life
 
So the textbox is named "title". Try using:

Code:
 Me.title.Text = varNumStr

instead.

BTW, I'd steer clear of "title" for the name of a textbox, since it is potentially confused with a property of Me that might be called title. Try something like "txtTitle" instead?

 

a2brute,

your right, smart to rename the textbox

but can't use .text property? not sure why not?

the solution i came up was with create in the query that is the recordsource for the report.

still interested in using .text property though

for instance having difficult time changing a combotextbox9msoControlEdit).text value

temporarily solution to that is again use a table and delete and recreate my commandbar. not a good solution though because anything toggled on commandbar is untoggled.

any suggestions?
MMFL-
Mavericks Fan For Life
 
Your original problem, not being able to assign the value to the Title control, is probably caused by using the "+" operator to concatenate string expressions:
"varNumStr = varNumStr + varMsg"

Because the operands are (presumably) all Variants, which can take any kind of value, the compiler doesn't have any information about whether your "+" is an addition or a string concatenation operator. (For that matter, neither do I, but I'm assuming varMsg is text, and from that inferring that varNumStr is a "string form of a number" rather than a "number of stripes", for example.)

I believe the compiler has made its best guess and assumed the "+" was an addition operator. Then, during execution, it discovers that you're trying to make it do arithmetic on non-numeric strings, so it gives you an error.

Recommendations:
1. Always use the "&" operator for string concatenation. In old versions of Basic, "+" was the only string concatenation operator. "&" was invented as a replacement because of just this kind of problem. The only reason "+" is still allowed is for compatibility with old Basic code.
2. Use Variants only as a last resort, that is, when you need a variable to hold different data types. Variants are inefficient, because the compiled code has to constantly check what kind of data is in it this time. This makes your code larger and slower than it ought to be. It also causes this kind of problem, because you didn't have a well defined concept, when you wrote this code, of how the compiler would interpret it.

You can't access the Text property of a TextBox control unless that control has the focus. That's a consequence of a design decision made by Microsoft to conserve Windows resources. Rick Sprague
 

Thanks again Rick. You are very thorough. MMFL-
Mavericks Fan For Life
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top