In this short guide, I’ll show you the steps to create a simple input box in Access.
For demonstration purposes, I’ll create the following input box. The goal is to get the square root based on the value typed in the box.
Steps to Create an Input Box in Access
Step 1: Add a button to launch the input box
To begin, go to the Create tab, and then click on the Form Design:
A new Access Form will be created. Click on the “button” icon located on the Access ribbon, and then place that button on the form itself:
You’ll then see the Command Button Wizard. Click on Cancel as the wizard will not be needed:
Right-click on the button, and then select ‘Build Event…‘
Next, select the Code Builder option, and then click on OK:
Step 2: Add a VBA
Now, you’ll see the following screen:
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 would look like this:
Let’s quickly review the main components of the code:
- Dim SquareRoot As String – Here, the SquareRoot variable is set to a string
- SquareRoot = InputBox(“What is the square root of:”, “Square Root Calculator”) – Here, the variable SquareRoot will assume the value 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”)
- The MsgBoxwill display the square root result based on this formula: (SquareRoot) ^ 0.5
Note that we used the symbol & after each section to concatenate all the above parts 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
Step 3: Save the VBA
Finally, save your VBA and the newly created Form (you can do that by pressing CTRL+S while on the VBA screen):
Step 4: Launch the input box in Access
To launch your input box, you’ll need to 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 the From View option:
Click on the button on the form:
The input box will be displayed on your screen:
Type a number in the input box and then press OK. For example, type the number of 25:
You’ll now see the result of your square root, which will be 5:
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.