Option Explicit
'Private variables
Dim cNum As Integer
Dim X As Integer
Private Sub cmdadd_Click()
lrow = Selection.Row()
Rows(lrow).Select
Selection.Copy
Rows(lrow + 1).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
Selection.ClearContents
Dim LastRow As Long
LastRow = Sheet8.Cells(Sheet8.Rows.Count, "A").End(xlUp + 1).Row
Sheet8.Range("G" + "LastRow").Value = reg1.Text
Sheet8.Range("H" + "LastRow").Value = reg2.Text
Sheet8.Range("I" + "LastRow").Value = reg3.Text
Sheet8.Range("J" + "LastRow").Value = reg4.Text
Sheet8.Range("K" + "LastRow").Value = reg5.Text
Sheet8.Range("L" + "LastRow").Value = reg6.Text
End Sub
Private Sub cmdclose_Click()
Unload Me
End Sub
Private Sub cmdData_Click()
Sheet8.Select
Unload Me
End Sub
Private Sub cmdDelete_Click()
'declare the variables
Dim findvalue As Range
Dim cDelete As VbMsgBoxResult
'check for values
If reg1.Value = "" Or reg2.Value = "" Then
MsgBox "There is not data to delete"
Exit Sub
End If
'give the user a chance to change their mind
cDelete = MsgBox("Are you sure that you want to delete this staff member", vbYesNo + vbDefaultButton2, "Are you sure????")
If cDelete = vbYes Then
'delete the row
Set findvalue = Sheet8.Range("G:G").Find(What:=reg2, LookIn:=xlValues)
findvalue.EntireRow.Delete
End If
'clear the controls
For X = 1 To cNum
Me.Controls("Reg" & X).Value = ""
Next
'refresh the listbox
Lookup
End Sub
Sub Lookup()
'declare the variables
Dim rngFind As Range
Dim strFirstFind As String
'error statement
On Error GoTo errHandler:
'clear the listbox
lstlookup.Clear
'look up parts or all of full mname
With Sheet8.Range("G:G")
Set rngFind = .Find(txtlookup.Text, LookIn:=xlValues, lookat:=xlPart)
'if value found then set a variable for the address
If Not rngFind Is Nothing Then
strFirstFind = rngFind.Address
'add the values to the listbox
Do
If rngFind.Row > 1 Then
lstlookup.AddItem rngFind.Value
lstlookup.List(lstlookup.ListCount - 1, 1) = rngFind.Offset(0, 1)
lstlookup.List(lstlookup.ListCount - 1, 2) = rngFind.Offset(0, 2)
lstlookup.List(lstlookup.ListCount - 1, 3) = rngFind.Offset(0, 3)
lstlookup.List(lstlookup.ListCount - 1, 4) = rngFind.Offset(0, 4)
lstlookup.List(lstlookup.ListCount - 1, 5) = rngFind.Offset(0, 5)
End If
'find the next address to add
Set rngFind = .FindNext(rngFind)
Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
End If
End With
'disable payroll editing
Me.cmdedit.Enabled = False
'error block
On Error GoTo 0
Exit Sub
errHandler::
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub
Private Sub cmdLookup_Click()
Lookup
End Sub
Private Sub lstLookup_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'declare the variables
Dim cFullName As String
Dim I As String
Dim findvalue
'error block
On Error GoTo errHandler:
'get the select value from the listbox
For I = "" To lstlookup.ListCount - 1
If lstlookup.Selected(I) = True Then
cPayroll = lstlookup.List(I, 1)
End If
Next I
'find the payroll number
'find the payroll number
Set findvalue = Sheet8.Range("G:G").Find(What:=cFullName, LookIn:=xlValues).Offset(0, 5)
'add the database values to the userform
cNum = 6
For X = 1 To cNum
Me.Controls("Reg" & X).Value = findvalue
Set findvalue = findvalue.Offset(0, 1)
Next
'disable adding
Me.cmdadd.Enabled = False
Me.cmdedit.Enabled = True
'error block
On Error GoTo 0
Exit Sub
errHandler::
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub
[CODE]