Difference between Left() and Left$() function in Access

前端 未结 2 1057
北海茫月
北海茫月 2021-01-19 04:19

I am trying to debug a broken query. The query uses Left$([blahblah],4) instead of Left([blahblah],4).

What is the difference between the Left() and Left$() function

相关标签:
2条回答
  • 2021-01-19 04:48

    The only difference is that Left accepts Null while Left$ does not.

    If passed a string, both return a String. VarType: vbString

    If passed Null, Left returns a Null value. VarType: vbNull

    They both runs at the same speed, about 12 mio. iterations pr. second, thus of no importance. Actually, if many samples are recorded, Left in average is a fraction faster than Left$.

    So, to put it short: Use Left$ if you wish to raise an error on a parameter value of Null, otherwise save your typing.

    0 讨论(0)
  • 2021-01-19 04:53

    The trailing $ is a type declaration character for the String data type in VBA.

    The result returned from Left$() is a string, whereas Left() returns a Variant.

    You must use Left(), not Left$() if there is any chance of Null values, since the Variant can be Null but the String cannot. To demonstrate that:

    1. Press Ctrl+G to open the Immedate window.

    2. Enter: ? Left(Null,1) The answer is Null.

    3. Now enter: ? Left$(Null,1) This generates Error 94. Since the result should be Null, and the String cannot be Null, you receive the error, "Invalid use of Null".

    If you are dealing with string values, in VBA code, Left$() will be slightly more efficient, as it avoids the overhead/inefficieny associated with the Variant. However, if there is any chance that Nulls may be involved, use Left(), or else explicitly handle the Null with something such as Nz().

    left$() will return a string or generate an error if passed NULL

    left() will return a string or NULL if passed NULL

    More information on Nulls: http://allenbrowne.com/casu-11.html

    0 讨论(0)
提交回复
热议问题