show input box info in a message box

G

Guest

hi all,
I have a program with an input box requiring the user to type in their name.
I then need to insert this name in a message box along with some text that
will remain embedded or 'standard'. for instance; THANK YOU (USERNAME) FOR
TAKING PART IN THIS SURVEY. I have set up the input box and the msgbox but am
at a loss as to how to acheive the above. any help you could provide would be
greatly apprecitated.
 
G

Guest

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.

HTH,
Barb Reinhardt
 
R

Rick Rothstein \(MVP - VB\)

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
 
G

Guest

thank you both very much, you have been informative, cleared my query up and
helped immensely.
kindest regards
 

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