Using the InputBox


Written By TheVBProgramer.



This VB6 tutorial explains how the InputBox works. It Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a string containing the contents of the text box.



stringvariable = InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])


The arguments to the InputBox function are described below:





Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used.


Optional. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.


Optional. String expression displayed in the text box as the default response if no other input is provided. If you omit default, the text box is displayed empty.

xpos and ypos

Both optional. Numeric expressions that specify custom positioning of the box on screen (by default, the box is displayed in the center of the screen, which is usually desired).

helpfile and context

Both optional. Can be used if a help file has been set up for the application. If either one of these arguments are used, they both must be used.



If the user clicks OK or presses ENTER , the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string ("").



In the example given in the previous topic, an InputBox was used to obtain the user's name. Recall that a string variable (strName) was declared and used to store the result of the InputBox function:


Dim strName As String

strName = InputBox("Enter your name:", "Input Test")



Note that the InputBox function in this example used "Enter your name:" as the required prompt argument, and "Input Test" as the optional title argument (which caused that text to be displayed in the title bar of the dialog box). When the program was first run, the InputBox looked like this:




The user then entered his name into the text area of the InputBox:



When the user clicked OK, the string "Harry" was stored in the variable strName. Had the user clicked Cancel, the zero-length string ("") would have been stored in strName.



The next example shows how the default argument is used.


Let's say you need the user to enter an account number, but the majority of the time the program will be processing account "ABC-123". You could provide the string "ABC-123" as the third argument to InputBox:

strAcctID = InputBox("Enter account ID:", "Input Test", "ABC-123")


When the program is run, the text entered as the default argument shows up pre-filled and highlighted in the text entry area of the InputBox:



The user can either accept that and click OK, or they can type the "ABC-123" to enter whatever is required and then click OK.


If you wish, set up another "Try It" project, copy folder where you saved the original "Try It" project over to a new folder, open the copied project, and replace the cmdTryIt_Click event with the following code:


Private Sub cmdTryIt_Click()


Dim strAcctID As String

strAcctID = InputBox("Enter account ID:", "Input Test", "ABC-123")

Print "Data for account ID "; strAcctID; " will be processed."

End Sub


When you run the project and click the "Try It" button, the InputBox with the default entry as shown ablove will be displayed. If you accept it and click OK, the output should look like this:




Download the VB project files for the example above here.


If you enjoyed this post, subscribe for updates (it's free)

VB6.0 codes

hi i'm carissa !
need help in codings how to make scheduling process for registration system .

visual basic 6

hi (salam)
i need (code) in program vb6 for solution nonlinear(newton method or bisection or newton-raphson),
in program was 3 textbox ,(text1=enter x0 , text2=enter qustion , text3=result ),
plese help me plese plese plese ............ .
just i need code in command button.

this is again a good

this is again a good content.
Warm thank!

how to define two input in one inputbox in vb6

Hi ,

how to define two input in one inputbox in vb6

Please help me ...

Use a secondary form

Instead of creating an input box, make a new form in the form view add two textboxes, give them the names that you want, the title that you want and resize it to appear like an input screen.
Dim form2 As New Form2 form2.Show() - only an example
Instead of saying stringVar = Inputbox .....
Display the new form and make sure that the two variables stored in the two textboxes are returned to the main form, there you can do as you please.
Hope this helps a little.







Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b, c, d, f, g, h, i
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = Val(TextBox3.Text)
d = Val(TextBox4.Text)
f = Val(TextBox5.Text)
g = Val(TextBox6.Text)
h = Val(TextBox7.Text)
i = Val(TextBox8.Text)
TextBox5.Text = f
TextBox6.Text = g
TextBox7.Text = h
TextBox8.Text = i
f = Val(c - d) / b
g = Val(c - D) / a
h = Val(a * b) + D
i = Val(-((a * b) - c))
End Sub

please correct this programe



inserting a function F(x) from input box.

I have written a code that solves equations by several different numerical methods, such as newton raphson method, bi-section method,secant method,etc
Instead of changing the equation everytime in the code, I would like to input the equation F(x), for example F(X)=x^2-4,by using the input box.
But i have problems with the inputbox, is it possible to do this with inputbox?
Below private function is called from different private subs.
instead of;
Private Function F(X)
F =X^2-4
End Function
i would like user to input F(X) by inputbox as below, but it does not work..
Private Function F(X)
F =inputbox("please write the function F(X) for finding the roots by "Newton Raphson Method",F)
End Function

Thank You

declare F as String in Global

declare F as String in Global

inputbox in visual basic 6.0

we were using a frame to group controls inside a form, inside that frame are 4 option buttons , one of those option button says input box that activates a welcome message that has a code of
reply = inputbox ("type your name please", "sample inputbox", 1100, 150)
if reply ="larry" then
msgbox " hi larry! welcome", " sample message box"
if reply <> "larry" then
msgbox "access denied, you're an intruder", " log off!"
end if

but, whenever i play it it says compile error
our teacher says we must add something, she said something about a message box.
what do you think should i add?

Hi, I figured out your

Hi, I figured out your problem. At the end of your equation add
End If
End If
Instead of
End if

Tell MsgBox What Buttons To Display

When using the Message Box in this format, you must declare what type of buttons to display. For example your message box should be:

MsgBox " hi larry! welcome", vbOKOnly, " sample message box"

vbOKOnly = OK button on your message box
vbOKCancel = OK and Cancel buttons
vbAbortRetryIgnore = Abort, Retry and Ignore buttons
vbYesNoCancel = Yes, No and Cancel buttons
vbYesNo = Yes and No buttons
vbRetryCancel = Retry and Cancel buttons

I forgot to mention...

I should have mentioned, if you want to know what button was pressed on your message box start with a variable and use this format instead.

A = MsgBox (" hi larry! welcome", vbYesNo, " sample message box")

Don't forget to use the left and right brackets. The above example displays a message box with yes and no buttons. When one of them is clicked, the corresponding value is transfered to the A variable. For example, if Yes is clicked, A will equal 6. If No is clicked, A will equal 7. The following lists the numbers that are returned when they are clicked:

OK= 1
Cancel = 2
Abort = 3
Retry = 4
Ignore = 5
Yes = 6
No = 7

Hope this helps you.

can i insert option buttons

can i insert option buttons inside the inputbox

hi you can u help me i need

hi you can u help me i need to know how to write input box code but at the end you terminate that input box with the word end

You Jealous?

You Jealous?

how to coding search name use database in vb6 n use input box?

how to coding search name use database in vb6 n use input box?


hi ,
if in inputbox user selects cancel instead of ok then i t gives error i.e. type mismatch ...
how i can solve it me out

This no longer works in 2010

This no longer works in 2010

i want to input a name

i want to input randoms name only how can i? maximum 1-10 names

Need Help

how can i make user to use inputbox to enter integer only?
if user enter other than interger, such as string
msgbox will come out say error, need to insert number only, please try again

It would go like: If

It would go like:

If isnumeric(strVariable) then
      dblVariable = strVariable
      dummy = msgBox("You must input a variable!",vbcritical)
end if

I just put vbcritical in to be fancy.

printer code

how can used the button to print a text in the textbox to a printer using visual basic 6.0


Very good tutorial for beginners, great upload!

Search codes

what are the codes that can be used to search a record using input box in vb6 which is connected by datacontrol. please help me out

try: temp=

temp= val(inputbox("enter number"))

Restrict the Characters


i want to restrict the user to enter only the numbers from 1 to 9 in the input box. Can you please
reply as soon as possible with source code....

Thanks in advance....

enter 1-9 in a input box

try to use looping . if the user enter a invalid input, loop in inputbox. if the user enter a valid input exit the loop.

if i write following

if i write following code:
dim temp as integer
temp= inputbox("enter number")
' if i click on Ok button without giving default value and without entering any value in the input box, then program gives error, how can i debug it

how does one fix the problem

how does one fix the problem 'InputBox' is a namespace and cannot be used as an expression?

How to put helpfile [, helpfile, context]

Thanks for above information.

Can You Please provide me some example for putting Helpfile [, helpfile, context] in Inputbox.

Thnaks & Regards.

hi this is user

thank u very much

thank you very much!

thank you very much!


Pretty cool thanks

Reply to comment (VB6)

Very nice post. I simply stumbled upon your weblog and wanted to mention that I've truly loved browsing your blog posts. After all I will be subscribing on your rss feed and I am hoping you write once more very soon!