InputBox Function

InputBox Function

The InputBox function displays a message and gets information from the user.

Syntax

strVariable = InputBox(prompt, title)prompt is the text that will be displayed to the user, and title is the title of the InputBox. The result (the text that the user responds with) is stored in a variable (like strVariable).

Look at the following examples.

Example 1
' dimension
Dim strName As String
' get name from user
strName = InputBox("Please type in your name", "Enter name")
Example 2
' dimension
Dim strMessage As String
Dim strResult As String
' initialize
strMessage = "Tell me a joke!"
' get input from user
strResult = InputBox(strMessage, "The Joke Master")

Why would it be useful to pass the prompt parameter with a string variable (like strMessage) instead of simply typing it in quotes?

Class Exercise: Guesser 2.0

Let’s take a second look at our Guesser project. What do the number of guesses mean? Why are they sometimes so high?

Add a line of code for us to see the computer’s guesses (see code below), making sure to limit guesses from 1 to 10 so it does not take too long.

Can you see that the computer is not a very educated guesser?

Guesser 2.0 Code
Private Sub cmdGuess_Click()
...
    ' guess loop
    Do
        ' take a guess from 1 to 100
        intGuess = Int(Rnd * 10 + 1)
        MsgBox "My guess is " & CStr(intGuess)
        ' increment counter
        intGuesses = intGuesses + 1
    Loop Until intGuess = intSecretNumber
...
End Sub

Assignment

The Lotto assignment is due in one week. Start with an algorithm.