Chris's Event Programming with Visual Basic course.
Contents
| Introduction | Tool Bars & Control Arrays |
Input
Boxes
Open a new project., place a command button
on the form and write the following code to it’s click event;
Sub Command1_Click ()
Dim Answer, DefVal, Msg, Title
' Declare variables.
Msg = "Enter
a value from 1 to 3."
' Set prompt.
Title =
"InputBox Demo"
' Set title.
DefVal
= "1"
' Set default return value.
Do
Answer = InputBox(Msg, Title, DefVal) ' Get user input.
Loop Until
Answer >= 1 And Answer <= 3
MsgBox
"You entered " & Answer
' Display message.
End Sub
If this is ran, the following dialogue appears
when the command button is clicked;
Typing a correct value prompts a message
box to be displayed from which the focus can be returned to the form, typing
an incorrect value re-displays the InputBox.
If an incorrect type, i.e. non-numeric is
input then an error occurs. This is discussed in the next tutorial.
Visual Basic and all other Microsoft products mentioned
in this series are trademarks of the Microsoft Corporation.