Chr() or Chr$()

F

Frans van Zelm

Hi,

Some functions exist as fx() and as fx$(). E.g. Chr, LCase, LTrim etc.

Why? I can't find any difference in the way Chr() and Chr$() work. A
collegue said it might have something to do with calculations on
empty/unused
fields in an Access database. Again, I couldn't find the clue.

I seached Internet, MSDN, Help in Access, ... but no answer.

Can *you* explain?

Thanks, Frans van Zelm
 
A

Andy Pope

Hi,

Search for "Returning Strings from Functions" with vba help for the
following information.

Quote:
Some functions have two versions: one that returns a Variant data type
and one that returns a String data type. The Variant versions are more
convenient because variants handle conversions between different types
of data automatically. They also allow Null to be propagated through an
expression. The String versions are more efficient because they use less
memory.

Consider using the String version when:

Your program is very large and uses many variables.

You write data directly to random-access files.
The following functions return values in a String variable when you
append a dollar sign ($) to the function name. These functions have the
same usage and syntax as their Variant equivalents without the dollar sign.

Chr$ ChrB$ *Command$
CurDir$ Date$ Dir$
Error$ Format$ Hex$
Input$ InputB$ LCase$
Left$ LeftB$ LTrim$
Mid$ MidB$ Oct$
Right$ RightB$ RTrim$
Space$ Str$ String$
Time$ Trim$ UCase$

Cheers
Andy
 
D

Dave

i 'think' it is a legacy thing. the $ used to be a type identifier that
indicated that the return value was a string. in newer versions of basic it
is not required any more but the old functions are being kept for
compatibility.
 
K

Karl E. Peterson

Dave said:
i 'think' it is a legacy thing. the $ used to be a type identifier
that indicated that the return value was a string. in newer versions
of basic it is not required any more but the old functions are being
kept for compatibility.

Not really. As Andy pointed out, when you drop the $ you get a Variant returned. If
this is being assigned to a String variable or processed with other String functions
(arguably, the most common cases), you avoid the coercion from Variant to String.
It's really easy to get lazy here. Don't.
 
F

Frans van Zelm

Hi Andi,

I knew it!

No kidding. I remember to have seen this help some time ago but I took no
notice then. And the Help doesn't help with a simple link when searching
any of these functions. Strange.

Thank you for solving this little mis(t)ery.

Frans
 
D

Dave

yeah, the help is lacking. while the index has both they go to the same
function definition which says it is chr but says the return type is a
string so its really chr$. even the object browser doesn't mention the
variant return. it does show chr$ 'as string' where chr has no specific
return type... though both of them in the text say 'returns a string'.
 

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