How to Create an Input Box in Access

In this post, I’m going to show you the steps to create a simple input box in Access.

To start, I’ll review an example of an input box that you can create. Alternatively, you may jump directly to the VBA code.

Example of an Input Box

Here, you’ll be able to execute a simple VBA to perform the following:

  • Display an input box to ask you to type a number (in order to calculate the square root of that number)
  • After you typed a number, a message will be displayed with the result of the square root

Steps to Create an Input Box in Access

The first thing that you’ll need to do is to create a button to display the input box upon a click:

(1) To begin, go to the Create tab, and then click on the Form Design:

 

Create tab in MS Access

 

(2) A new form will be created. Click on the “button” icon located on the Access ribbon, and then place that button on the form itself:

 

Add a button in MS Access

 

(3) After you placed the button on the form, the Command Button Wizard will be displayed. Click on Cancel as that wizard will not be needed:

 

Command Button Wizard

 

(4) On the Property Sheet, which will be located on the right-hand-side of your screen (press F4 if you cannot see the Property Sheet), select the [Event Procedure] from the drop down-list (associated with the On Click option on the first row):

 

Property Sheet - MS Access

 

(5) Press on the “3 dots” () on the Property Sheet in order to open the VBA screen:

 

Event Procedure - MS Access

 

(6) You’ll now see the following syntax on the VBA screen:

 

Option Compare Database

Private Sub Command0_Click()

End Sub

 

(7) Copy/Insert the following code in between the Private Sub Command0_Click() and the End Sub statements:

 

Dim SquareRoot As String

SquareRoot = InputBox("What is the square root of:", "Square Root Calculator")

MsgBox "The square root of " & SquareRoot & " is: " & (SquareRoot) ^ 0.5

 

Your final VBA code in Access should look as follows:

 

How to Create an Input Box in Access

 

Let’s quickly explain the main parts of the code:

  • Dim SquareRoot As String – Here, the SquareRoot is the variable, and it is set to a string
  • SquareRoot = InputBox(“What is the square root of:”, “Square Root Calculator”) – Here, the variable SquareRoot will assume the value that you typed in the input box. The input box has this structure: InputBox(“Text to be displayed within the input box”, “The title of the input box”)
  • MsgBox “The square root of ” & SquareRoot & ” is: ” & (SquareRoot) ^ 0.5
    Finally, a MsgBox will display the following components:

    • Text: “The square root of ”
    • Variable: SquareRoot typed in the input box
    • Text: ” is:
    • The Square root result: based on the SquareRoot variable typed: (SquareRoot) ^ 0.5
    • Note that we used the symbol & after each section to concatenate all the above sections together within the message box. Also note, that you should place text within quotations, while you should not place variables (such as the SquareRoot) within quotations

 

(8) Save your VBA and the newly created Form (you can do that by pressing CTRL+S while in the VBA screen):

 

Save a form in MS Access

 

(9) Switch to the Form View so that you can click on the button you just created.

To switch to the Form View, first press on the View icon on the Access ribbon, and then select From View from the drop-down list:

 

From View - MS Access

 

(10) Press the button on the form:

 

MS Access - Button

 

(11) The input box will be displayed on your screen:

 

Creating an Input Box in Access 2016

 

(12) Type a number in the input box and then press OK. For example, type the number of 25:

 

Creating an Input Box in Access

 

(13) You’ll now see the result of your square root, which will be 5:

 

Square root calculator in Access

 

You have seen how to create an input box in Access. You may also want to check how to create a Combo Box in Access to gather user’s input.