The InputBox function needs to be used in a command button to prompt the user for a number.

I know that the syntax is (prompt, title), whereprompt is the message you want displayed inside the dialog box [in this case - "Enter a number"], and title is the next you want displayed in the dialog box's title bar.

Since the application also needs to show the 'student's name' on the form would I need to use the Val function to show the name and results in the lblMsg?

Now to I go about changing

Private Sub cmdSquare_Click()
    'calculate square of a number
    lblTSquare.Caption = txtNumber ^ 2
    'calculate square root of a number
    lblTroot.Caption = Sqr(txtNumber.Text)

End Sub

so that it prompts for the user for a number and does the calculations?

Recommended Answers

All 3 Replies

Try

Num = 3 'number
Res = InputBox("Calculate square of " & Num, "Student Name", "")
If Res = Sqr(Num) Then MsgBox "OK"

dim a as double
dim RetSquare as double
dim RetSquareRoot as double

a= 25 'change this to a=val(txtNumber) if you want dynamic input.
RetSquare=a ^ 2
RetSquareRoot =sqr(a)

MsgBox RetSquare
MsgBox RetSquareRoot

How do I modify this code to prompt a student for a number using the captions/labels I have used in the code in the above posts?

I was able to figure out the input box using:

Private Sub cmdSquare_Click()
    Dim i
    i = InputBox("Please enter a number.", "Input Box")
    MsgBox "The square of " & i & " is " & (i ^ 2), vbOKOnly, "Result"
    MsgBox "The square root of " & i & " is " & Sqr(i), vbOKOnly, "Result"
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.