@00_DATA ENTRY USERFORM VBA CODE
Option Explicit
'===SAVE DATA TO WORKSHEET
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim Last_Row As Long
Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
'======================= Validation =========================
If Me.TextBox1.Value = "" Then
MsgBox "Please enter the Name", vbCritical
Exit Sub
End If
If Me.TextBox2.Value = "" Then
MsgBox "Please enter the Address", vbCritical
Exit Sub
End If
If Me.TextBox3.Value = "" Then
MsgBox "Please enter the Mobile Number", vbCritical
Exit Sub
End If
If Me.ComboBox1.Value = "" Then
MsgBox "Please select the Gender", vbCritical
Exit Sub
End If
'===================================================
sh.Range("A" & Last_Row + 1).Value = "=IF(B" & Last_Row + 1 & "="""","""",ROW()-1)"
sh.Range("B" & Last_Row + 1).Value = Me.TextBox1.Value
sh.Range("C" & Last_Row + 1).Value = Me.TextBox2.Value
sh.Range("D" & Last_Row + 1).Value = Me.TextBox3.Value
sh.Range("E" & Last_Row + 1).Value = Me.ComboBox1.Value
sh.Range("F" & Last_Row + 1).Value = Now
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.ComboBox1.Value = ""
Call Refresh_Data
End Sub
'===CODE TO UPDATE DATA
Private Sub CommandButton2_Click()
If Me.TextBox4.Value = "" Then
MsgBox "Please select a record to update", vbCritical
Exit Sub
End If
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim Selected_Row As Long
Selected_Row = Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex, 0),
sh.Range("A:A"), 0)
'======================= Validation =========================
If Me.TextBox1.Value = "" Then
MsgBox "Please enter the Name", vbCritical
Exit Sub
End If
If Me.TextBox2.Value = "" Then
MsgBox "Please enter the Address", vbCritical
Exit Sub
End If
If Me.TextBox3.Value = "" Then
MsgBox "Please enter the Mobile Number", vbCritical
Exit Sub
End If
If Me.ComboBox1.Value = "" Then
MsgBox "Please select the Gender", vbCritical
Exit Sub
End If
'===================================================
sh.Range("B" & Selected_Row).Value = Me.TextBox1.Value
sh.Range("C" & Selected_Row).Value = Me.TextBox2.Value
sh.Range("D" & Selected_Row).Value = Me.TextBox3.Value
sh.Range("E" & Selected_Row).Value = Me.ComboBox1.Value
sh.Range("F" & Selected_Row).Value = Now
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.ComboBox1.Value = ""
Call Refresh_Data
End Sub
'===CODE TO DELETE DATA FROM WORKSHEET
Private Sub CommandButton3_Click()
If Me.ListBox1.ListIndex < 0 Then
MsgBox "Please select a record to update", vbCritical
Exit Sub
End If
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim Selected_Row As Long
Selected_Row = Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex, 0),
sh.Range("A:A"), 0)
sh.Range("A" & Selected_Row).EntireRow.Delete
Call Refresh_Data
End Sub
'===CODE TO SAVE WORKBOOK TO FOLDER
Private Sub CommandButton4_Click()
ThisWorkbook.Save
MsgBox "Data Saved"
End Sub
'===CODE TO INITIATE ACTION WHEN LIST BOX IS DOUBLE CLICK
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If Me.ListBox1.List(Me.ListBox1.ListIndex, 0) <> "" Then
Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
Me.TextBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
Me.TextBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 3)
Me.ComboBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 4)
Me.TextBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
End If
End Sub
Private Sub UserForm_Activate()
With Me.ComboBox1
.Clear
.AddItem ""
.AddItem "Male"
.AddItem "Female"
End With
Call Refresh_Data
End Sub
'===CODE TO PERFORM CERTAIN ACTIONS WHEN ACTIVITIES
'===IS CARRIED OUT IN THE USERFORM
Sub Refresh_Data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim Last_Row As Long
Last_Row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
sh.Range("F:F").NumberFormat = "D-MMM-YY HH:MM:SS AM/PM"
With Me.ListBox1
.ColumnHeads = True
.ColumnCount = 6
.ColumnWidths = "30,100,90,80,60,90"
If Last_Row = 1 Then
.RowSource = "Database!A2:F2"
Else
.RowSource = "Database!A2:F" & Last_Row
End If
End With
End Sub