View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default show input box info in a message box

UserName = InputBox (blah blah blah)
MsgBox("Thank You " & username & " for taking part in this survey")

Make sure you have a blank before and after the &. I've found that
I get errors because there's no blank.


You only need to provide a space between the variable name and the ampersand
(&) following it. The reason has to do with VB being backward compatible,
syntax-wise, with older VB's and even the DOS versions of BASIC that
preceded it. In the "old days", you could specify a variable's data type by
using suffix characters attached to the end of the variable's name. The
ampersand character happens to be one of those suffix characters. Placing an
ampersand at the end of a variable's name makes the variable a Long data
type... the others being percent sign (%) for Integer, exclamation mark (!)
for Single, pound sign (#) for Double, dollar sign ($) for String and,
although the data type is "newer", the at sign (@) for Currency. Anyway,
VB's problem comes from its considering the second ampersand in this string
(no space between it and the variable name in front of it)...

MsgBox "Thank You "&UserName&" for taking part in this survey"

to be the Long data type suffix character meaning there is no concatenation
symbol for the variable and the text following it. I know, it should be able
to figure out that UserName contains a String value so that it can't be a
Long, but it can't. Anyway, you can see this in action by putting in the
ampersand that VB is missing.

MsgBox "Thank You "&UserName&&" for taking part in this survey"

The above string of text will not raise an error when the above MsgBox
statement is typed in; instead, the automatic spacing that VB does will take
place around the second ampersand once the statement is committed. Now, of
course, you will get a type-declaration error when you attempt to actually
run this code because UserName is (presumably) Dim'med as a String and the
concatenation of a declared Long variable and a String is improper.

Anyway, that is the 'why' of the error you mentioned.

Rick