Lesson 57 of 60 – Creating a Data Entry Form
95%

Creating a Data Entry Form in Excel VBA

A data entry form makes it easier to enter student information into an Excel worksheet. Instead of typing directly into cells, we can use VBA controls such as TextBox, Label, and CommandButton.

Note: In this lesson, you will learn the basic structure of a VBA data entry form and how to save the entered information into an Excel worksheet.

1. What is a Data Entry Form?

A data entry form is a user interface that allows users to enter information without directly typing into worksheet cells.

For example, a student form can collect:

  • Student ID
  • Student Name
  • Course
  • Mobile Number
  • Marks

2. Why Use a Data Entry Form?

Data entry forms make Excel applications easier to use.

  • Users do not need to work directly with cells.
  • Input fields can be clearly labeled.
  • Validation can be added.
  • Data can be saved automatically.
  • The form can be reused for multiple records.

3. Creating a UserForm

To create a UserForm, open the VBA Editor.

Use:

Developer → Visual Basic → Insert → UserForm

Excel creates a blank UserForm where controls can be added.

4. Understanding the UserForm

A UserForm is a window that contains controls used to interact with the user.

Common controls include:

  • Label
  • TextBox
  • CommandButton
  • ComboBox
  • CheckBox
  • OptionButton

5. Adding a Label

A Label displays text that tells the user what information should be entered.

For example:

Student ID
Student Name
Course
Mobile Number

Labels help make the form easy to understand.

6. Adding a TextBox

A TextBox allows the user to enter information.

For example, create a TextBox for Student ID and set its name to:

txtStudentID

The TextBox value can later be read using its Value property.

7. Creating Student Name TextBox

Add another TextBox for the student's name.

Set its Name property to:

txtStudentName

The user can enter the student's full name in this TextBox.

8. Creating Course TextBox

Add a TextBox for the student's course.

txtCourse

The course entered by the user will later be stored in the worksheet.

9. Creating Mobile Number TextBox

Add a TextBox for the student's mobile number.

txtMobile

The value entered into this TextBox can be saved in the worksheet.

10. Adding a Save Button

Add a CommandButton to save the entered information.

Set its Name property to:

cmdSave

Set its Caption property to:

Save

11. Adding a Clear Button

A Clear button can remove the values entered in the form.

Set its Name to:

cmdClear

Set its Caption to:

Clear

12. Reading a TextBox Value

The value entered into a TextBox can be accessed using the Value property.

txtStudentID.Value

Similarly:

txtStudentName.Value
txtCourse.Value
txtMobile.Value

13. Creating the Save Button Event

Double-click the Save button to create its Click event.

Private Sub cmdSave_Click()

End Sub

Code placed inside this procedure runs when the Save button is clicked.

14. Connecting the Form to a Worksheet

Suppose our student data is stored in a worksheet named Students.

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Students")

The variable ws now represents the Students worksheet.

15. Finding the Next Empty Row

New student records should be inserted into the next available row.

Dim nextRow As Long

nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1

This finds the last used row in column A and adds 1.

16. Saving Student ID

Suppose column A contains Student ID.

ws.Cells(nextRow, 1).Value = txtStudentID.Value

The value from the Student ID TextBox is saved into column A.

17. Saving Student Name

Student Name can be saved in column B.

ws.Cells(nextRow, 2).Value = txtStudentName.Value

Each new record is written to the calculated next row.

18. Saving Course

Course information can be saved in column C.

ws.Cells(nextRow, 3).Value = txtCourse.Value

The course entered in the form is stored automatically.

19. Saving Mobile Number

Mobile Number can be saved in column D.

ws.Cells(nextRow, 4).Value = txtMobile.Value

The complete record is now being saved into the worksheet.

20. Displaying a Success Message

After saving the record, we can inform the user that the operation was successful.

MsgBox "Student record saved successfully."

This gives immediate feedback to the user.

21. Clearing the Form

The TextBox values can be cleared after saving.

txtStudentID.Value = ""
txtStudentName.Value = ""
txtCourse.Value = ""
txtMobile.Value = ""

This prepares the form for entering another student.

22. Creating the Clear Button Code

Double-click the Clear button and add the following code:

Private Sub cmdClear_Click()

    txtStudentID.Value = ""
    txtStudentName.Value = ""
    txtCourse.Value = ""
    txtMobile.Value = ""

End Sub

Clicking Clear will remove all entered values.

23. Validating Required Fields

Before saving, we should check whether important fields are empty.

If txtStudentID.Value = "" Then
    MsgBox "Please enter Student ID."
    Exit Sub
End If

This prevents an incomplete record from being saved.

24. Validating Student Name

We can also check whether the Student Name has been entered.

If txtStudentName.Value = "" Then
    MsgBox "Please enter Student Name."
    Exit Sub
End If

Similar checks can be added for other required fields.

25. Complete Save Button Code

The Save button can now combine validation and data saving.

Private Sub cmdSave_Click()

    Dim ws As Worksheet
    Dim nextRow As Long

    If txtStudentID.Value = "" Then
        MsgBox "Please enter Student ID."
        Exit Sub
    End If

    If txtStudentName.Value = "" Then
        MsgBox "Please enter Student Name."
        Exit Sub
    End If

    Set ws = ThisWorkbook.Worksheets("Students")

    nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1

    ws.Cells(nextRow, 1).Value = txtStudentID.Value
    ws.Cells(nextRow, 2).Value = txtStudentName.Value
    ws.Cells(nextRow, 3).Value = txtCourse.Value
    ws.Cells(nextRow, 4).Value = txtMobile.Value

    MsgBox "Student record saved successfully."

    txtStudentID.Value = ""
    txtStudentName.Value = ""
    txtCourse.Value = ""
    txtMobile.Value = ""

End Sub

26. Opening the UserForm with VBA

A separate macro can be used to open the data entry form.

Sub OpenStudentForm()

    UserForm1.Show

End Sub

Replace UserForm1 with the actual name of your UserForm.

27. Adding a Date of Entry

We can also save the current date when a student record is added.

Suppose column E contains the Date of Entry.

ws.Cells(nextRow, 5).Value = Date

VBA automatically inserts the current date.

28. Improving the Data Entry Form

A professional student data entry form can include:

  • Student ID
  • Student Name
  • Father Name
  • Course
  • Mobile Number
  • Email
  • Address
  • Date of Admission
  • Save button
  • Clear button
  • Close button

29. Common Mistakes

Common mistakes while creating a UserForm include:

  • Using the wrong worksheet name.
  • Using incorrect TextBox names.
  • Saving data into the wrong columns.
  • Not checking empty fields.
  • Not finding the correct next row.
  • Forgetting to clear the form after saving.

30. Complete Student Data Entry Form

The following code provides a complete basic Save button for a student data entry form.

Private Sub cmdSave_Click()

    Dim ws As Worksheet
    Dim nextRow As Long

    If txtStudentID.Value = "" Then
        MsgBox "Please enter Student ID."
        Exit Sub
    End If

    If txtStudentName.Value = "" Then
        MsgBox "Please enter Student Name."
        Exit Sub
    End If

    If txtCourse.Value = "" Then
        MsgBox "Please enter Course."
        Exit Sub
    End If

    Set ws = ThisWorkbook.Worksheets("Students")

    nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1

    ws.Cells(nextRow, 1).Value = txtStudentID.Value
    ws.Cells(nextRow, 2).Value = txtStudentName.Value
    ws.Cells(nextRow, 3).Value = txtCourse.Value
    ws.Cells(nextRow, 4).Value = txtMobile.Value
    ws.Cells(nextRow, 5).Value = Date

    MsgBox "Student record saved successfully."

    txtStudentID.Value = ""
    txtStudentName.Value = ""
    txtCourse.Value = ""
    txtMobile.Value = ""

End Sub

This form provides a simple foundation for creating a complete Excel VBA student management application.

📌 Key Points

  • A UserForm provides a friendly interface for entering data.
  • TextBox controls are used to accept user input.
  • CommandButton controls can save and clear information.
  • The next empty row can be found using End(xlUp).
  • Worksheet Cells can be used to write form data.
  • Input validation helps prevent incomplete records.
  • The Date function can store the current date.
  • A data entry form can be expanded into a complete student management system.

🧠 Quick Quiz

Question: Which VBA control is commonly used to allow a user to enter text in a UserForm?