September 17, 2016

How to code command button of user form to create database

How to create a database quickly and easily by coding the ‘add data’ command button which does the following:
(i) First checks whether all controls have been filled with data
(ii) Checks if any duplicate entry has been made
(iii) Clears the data of controls once the data has been transferred to the Excel worksheet

Code add button to create database
Code add command button to create a database in Excel

Watch the video below:

You can watch this video also on YouTube.

Her’s the complete VBA code:

Private Sub UserForm_Initialize()
myCtl6.List = Array(“arts”, “science”)
End Sub
Private Sub CommandButton1_Click()
Dim ctlNum As Long
Dim erow As Long
Dim intmyCtl As Integer
ctlNum = 6
For intmyCtl = 1 To ctlNum
If Me.Controls(“myCtl” & intmyCtl).Value = “” Then
MsgBox “You haven’t entered data in all controls”
Exit Sub
End If
If WorksheetFunction.CountIf(Sheet1.Range(“A:A”), Me.myCtl1.Value) > 0 Then
MsgBox “Duplicate! This Admission Number is already in use!”
Exit Sub
End If
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For intmyCtl = 1 To ctlNum
Cells(erow, intmyCtl) = Me.Controls(“myCtl” & intmyCtl).Value
For intmyCtl = 1 To ctlNum
Me.Controls(“myCtl” & intmyCtl).Value = “”
End Sub

3 thoughts on “How to code command button of user form to create database

  1. Hello Sir,
    I am using Excel VBA to code a software for my Computer Science project. I need your help with some of the coding, how can I contact you?

Comments are closed.