Input Box function

Usage

The code below displays the dialog box we saw in the introduction.

InputBox "Please enter the documents title","Enter Title", "Document 1"

However, we need to know what the user entered! The InputBox function returns a string, which is the text the user entered in the Input Box.

DocName As String
DocName = InputBox ("Please enter the documents title", _
                  "Enter Title", "Document 1")
Msgbox "The documents title is " & DocName

This displays an input box asking the user for a documents title. When the user clicks OK then a Message Box will be displayed showing the text the user entered. You will notice that if you press cancel, the message box is still displayed. When the cancel button is pressed, the InputBox returns an empty string. You can edit the code so the message box is not displayed if the user presses cancel, or does not type anything:

DocName As String
DocName = InputBox ("Please enter the documents title", _
                  "Enter Title", "Document 1")
' If DocName is not empty, display the message box
If Not DocName = "" Then Msgbox "The documents title is " & DocName

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson