I've touched on building forms in a previous hub (Create A Basic Login Function) so will not go through how to build a form in this hub, but you can download a "Pre-Code" version of the workbook we will be discussing by clicking the link below. This workbook will be the best way of following this guide and learning how this works.
Once you have the Workbook UserForm Database Guide open, you may need to click a button at the top that enables editing, you may also need to click a button to enable Macros if it pops up.
Setting Up The Form To Talk To The Database
Currently, you should be looking at a single sheet workbook with a small Database which gathers personal information about a person. We will be using a Form to fill this information in.
Open the VBA editor (ALT + F11), you'll see in the Project Window in the top left that there is a Form called Add. Double click on Add and you'll see the Form we are going to use to populate the database.
I've already named all of the input fields with appropriate names:
- Name Field = Name
- Date Of Birth Field = DOB
- Gender Dropdown = Gender
- Email Field = Email
- Telephone = Telephone
- T&Cs Checkbox = TCs
These are the names that will be used in the code to reference the input boxes on the form.
Now it's time to start adding the code. We don't need the Form to do anything until the Submit button is pressed, so our code will reside within that button.
Double click on the Submit button, the code window will open up, pre-populated with code ready to detect when the Submit button is pressed.
All of our code must be added between the line Private Sub CommandButton1_Click() and End Sub. First we need to find the last row in the database that contains data, by finding this we can then tell VBA to place the Form data on the next line down. This is achieved with the following code:
Now you've made your form smart enough to know where the next empty row in the database is, the next step is to start populating it with the data from the form.
Lets start with moving the Name field on to the database. Add the following code above the End Sub (Line 17):
You can test that this works now by clicking the little Play Button at the top of the VBA editor window, situated on the tool bar. You can also test by pressing F5.
When tested, the form should pop up. Type "TEST" in to the Name field and then press Submit. Check back on the Excel document and the database should now be populated with "Name" in the second row.
Return to the editor and finish populating the rest of the database by updating the "With" section of the code to the following:
Test the code again and this time populate all fields with information. You'll notice there is an issue with the Gender field, there's nothing in the drop down box! Fill everything else in and you should end up with the following in the database (or something similar depending on what you entered):
This proves that the Form is working as it should, now all we need to do is have a way of opening the Form and sorting out the options in the Gender field.
Populating Drop Down Lists On UserForms
This is actually very easy and uses the same With method used to populate the database. We need to add the Genders Male & Female to the list before the Submit button is pressed, so it cannot be added to the button's code, it must go in a it's own code that is called when the form is Activated.
Below the End Sub line of the code we added earlier, copy the following code:
(If your view in the VBA editor is not on the Code view for the form, right click on Add in the Project Window and click "View Code")
This code will be run when the UserForm it belongs to is Activated. Above the End Sub, copy the following code:
It's as simple as that. When the code is called, VBA will now add "Male" & "Female" as options in the drop down. You can add as many items as you link in this fashion, however for long lists there are better ways of doing it which I'll cover in later hubs.
Now we need to create a one line code to open the UserForm when needed
Creating The "Open Form" Code
In the Project Window of the VBA Editor (Top Left), right click on any item and select "Insert" and then "Module" (not Class Module!). A new module will appear called Module1. Double click it and in the Properties Window (Bottom Left), rename it to "Form_Open". In the blank code space to the right, copy the following code:
Once that is done, head back to the Excel document containing the database and draw a square shape to the right of the database. Right click it and edit the text to read "Add Item", you should now have something that looks similar to this:
Right click the button and select Assign Macro, from the list that appears, choose Open_Add and press OK. Now when you click the button, your form will load, you can populate it and then submit it to your database!
In case you get stuck anywhere, you can download the complete working example from the link below
How'd You Get On?
Rate This Guide
Arran Christie from USA on August 26, 2014: