Option Explicit
Option Explicit
Sub Reset()
With frmForm
.txtID.Value = ""
.txtName.Value = ""
.optMale.Value = False
.optFemale.Value = False
'Default Color
.txtID.BackColor = vbWhite
.txtName.BackColor = vbWhite
.txtCity.BackColor = vbWhite
.txtCountry.BackColor = vbWhite
.cmbDepartment.BackColor = vbWhite
'--------------------------------
.cmbDepartment.Clear
.cmbDepartment.RowSource = "Dynamic"
.cmbDepartment.Value = ""
.txtRowNumber.Value = ""
.txtCity.Value = ""
.txtCountry.Value = ""
Call Add_SearchColumn
ThisWorkbook.Sheets("Database").AutoFilterMode = False
ThisWorkbook.Sheets("SearchData").AutoFilterMode = False
ThisWorkbook.Sheets("SearchData").Cells.Clear
'-----------------------------------------------
.lstDatabase.ColumnCount = 9
.lstDatabase.ColumnHeads = True
.lstDatabase.ColumnWidths = "30,60,75,40,60,45,55,70,70"
Else
.lstDatabase.RowSource = "Database!A2:I2"
End If
End With
End Sub
Sub Submit()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
iRow = [Counta(Database!A:A)] + 1
Else
iRow = frmForm.txtRowNumber.Value
End If
With sh
.Cells(iRow, 2) = frmForm.txtID.Value
.Cells(iRow, 3) = frmForm.txtName.Value
.Cells(iRow, 5) = frmForm.cmbDepartment.Value
.Cells(iRow, 6) = frmForm.txtCity.Value
.Cells(iRow, 7) = frmForm.txtCountry.Value
.Cells(iRow, 8) = Application.UserName
End With
End Sub
Sub Show_Form()
frmForm.Show
End Sub
Dim i As Long
Selected_List = 0
For i = 0 To frmForm.lstDatabase.ListCount - 1
Selected_List = i + 1
Exit For
End If
Next i
End Function
Sub Add_SearchColumn()
frmForm.EnableEvents = False
With frmForm.cmbSearchColumn
.Clear
.AddItem "All"
.AddItem "Gender"
.AddItem "Department"
.AddItem "City"
.AddItem "Country"
.Value = "All"
End With
frmForm.EnableEvents = True
frmForm.txtSearch.Value = ""
frmForm.txtSearch.Enabled = False
frmForm.cmdSearch.Enabled = False
End Sub
Sub SearchData()
Application.ScreenUpdating = False
Dim shDatabase As Worksheet ' Database sheet
Dim iColumn As Integer 'To hold the selected column number in Database sheet
Dim iDatabaseRow As Long 'To store the last non-blank row number available in Database sheet
Dim iSearchRow As Long 'To hold the last non-blank row number available in SearachData sheet
sColumn = frmForm.cmbSearchColumn.Value
sValue = frmForm.txtSearch.Value
End If
Else
End If
shSearchData.Cells.Clear
shDatabase.AutoFilter.Range.Copy shSearchData.Range("A1")
Application.CutCopyMode = False
frmForm.lstDatabase.ColumnCount = 9
frmForm.lstDatabase.ColumnWidths = "30, 60, 75, 40, 60, 45, 55, 70, 70"
End If
Else
End If
shDatabase.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub
ValidateEntries = True
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Print")
iEmployeeID = frmForm.txtID.Value
With frmForm
'Default Color
.txtID.BackColor = vbWhite
.txtName.BackColor = vbWhite
.txtCity.BackColor = vbWhite
.txtCountry.BackColor = vbWhite
.cmbDepartment.BackColor = vbWhite
'--------------------------------
ValidateEntries = False
.txtID.BackColor = vbRed
.txtID.SetFocus
Exit Function
End If
.txtID.BackColor = vbRed
.txtID.SetFocus
Exit Function
End If
ValidateEntries = False
.txtName.BackColor = vbRed
.txtName.SetFocus
Exit Function
End If
'Validating Gender
ValidateEntries = False
Exit Function
End If
ValidateEntries = False
.cmbDepartment.BackColor = vbRed
.cmbDepartment.SetFocus
Exit Function
End If
ValidateEntries = False
.txtCity.BackColor = vbRed
.txtCity.SetFocus
Exit Function
End If
ValidateEntries = False
.txtCountry.BackColor = vbRed
.txtCountry.SetFocus
Exit Function
End If
End With
End Function
ValidatePrintDetails = True
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Print")
iEmployeeID = frmForm.txtID.Value
With frmForm
'Default Color
.txtID.BackColor = vbWhite
.txtName.BackColor = vbWhite
.txtCity.BackColor = vbWhite
.txtCountry.BackColor = vbWhite
.cmbDepartment.BackColor = vbWhite
'--------------------------------
ValidatePrintDetails = False
.txtID.BackColor = vbRed
.txtID.SetFocus
Exit Function
End If
ValidatePrintDetails = False
.txtName.BackColor = vbRed
.txtName.SetFocus
Exit Function
End If
'Validating Gender
Exit Function
End If
ValidatePrintDetails = False
.cmbDepartment.BackColor = vbRed
.cmbDepartment.SetFocus
Exit Function
End If
ValidatePrintDetails = False
.txtCity.BackColor = vbRed
.txtCity.SetFocus
Exit Function
End If
.txtCountry.BackColor = vbRed
.txtCountry.SetFocus
Exit Function
End If
End With
End Function
Sub Print_Form()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Print")
With frmForm
sh.Range("E5").Value = .txtID.Value
sh.Range("E7").Value = .txtName.Value
sh.Range("E9").Value = IIf(.optFemale.Value = True, "Female", "Male")
sh.Range("E11").Value = .cmbDepartment.Value
sh.Range("E13").Value = .txtCity.Value
sh.Range("E15").Value = .txtCountry.Value
End With
sh.PageSetup.PrintArea = "$B$2:$I$17"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub Maximize_Restore()
If Not bState = True Then
iWidth = frmForm.Width
iHeight = frmForm.Height
iTop = frmForm.Top
iLeft = frmForm.Left
With Application
.WindowState = xlMaximized
frmForm.StartUpPosition = 0
frmForm.Left = .Left
frmForm.Top = .Top
frmForm.Width = .Width
frmForm.Height = .Height
End With
frmForm.cmdFullScreen.Caption = "Restore"
bState = True
Else
With Application
.WindowState = xlNormal
frmForm.Zoom = 100
frmForm.StartUpPosition = 0
frmForm.Left = iLeft
frmForm.Width = iWidth
frmForm.Height = iHeight
frmForm.Top = iTop
End With
bState = False
End If
End Sub
FORMS
Option Explicit
Call Reset
Else
Me.txtSearch.Value = ""
Me.txtSearch.Enabled = True
Me.cmdSearch.Enabled = True
End If
End Sub
If Selected_List = 0 Then
Exit Sub
End If
Dim i As VbMsgBoxResult
i = MsgBox("Do you want to delete the selected record?", vbYesNo + vbQuestion, "Confirmation")
ThisWorkbook.Sheets("Database").Range("A:A"), 0)
ThisWorkbook.Sheets("Database").Rows(iRow).Delete
Call Reset
End Sub
If Selected_List = 0 Then
Exit Sub
End If
'Code to update the value to respective controls
Me.txtRowNumber.Value =
Application.WorksheetFunction.Match(Me.lstDatabase.List(Me.lstDatabase.ListIndex, 0), _
ThisWorkbook.Sheets("Database").Range("A:A"), 0)
Me.txtID.Value = Me.lstDatabase.List(Me.lstDatabase.ListIndex, 1)
Me.txtName.Value = Me.lstDatabase.List(Me.lstDatabase.ListIndex, 2)
sGender = Me.lstDatabase.List(Me.lstDatabase.ListIndex, 3)
Me.optFemale.Value = True
Else
Me.optMale.Value = True
End If
Me.cmbDepartment.Value = Me.lstDatabase.List(Me.lstDatabase.ListIndex, 4)
Me.txtCity.Value = Me.lstDatabase.List(Me.lstDatabase.ListIndex, 5)
Me.txtCountry.Value = Me.lstDatabase.List(Me.lstDatabase.ListIndex, 6)
MsgBox "Please make the required changes and click on 'Save' button to update.", vbOKOnly +
vbInformation, "Edit"
End Sub
Call Maximize_Restore
End Sub
msgValue = MsgBox("Do you want to print the employee details?", vbYesNo + vbInformation,
"Print")
Call Print_Form
End If
End Sub
Call Reset
End Sub
msgValue = MsgBox("Do you want to save the data?", vbYesNo + vbInformation, "Confirmation")
Call Submit
Call Reset
End If
End Sub
Exit Sub
End If
Call SearchData
End Sub
End Sub
Call Reset
End Sub