i have a userform with
-around 10-15 textbook to enter employee info
-1 text box to search the employee info by enter their ID
-1 listbox to show some of their info, not all
I have some stuck with my code below like:
when I search a employee, the listbox show all possibilities, by clicking the one I want in the listbox, it's always stuck with the first one instead of the one I really wanted(eg, i want the third one in the listbox)
secondly, sometimes if i want to edit(means save) after modified, I got an error #91 .
Thanks for pointing me to the right way always
COMMAND ON EDIT
COMMAND ON LISTBOX
COMMAND ON SEARCH
-around 10-15 textbook to enter employee info
-1 text box to search the employee info by enter their ID
-1 listbox to show some of their info, not all
I have some stuck with my code below like:
when I search a employee, the listbox show all possibilities, by clicking the one I want in the listbox, it's always stuck with the first one instead of the one I really wanted(eg, i want the third one in the listbox)
secondly, sometimes if i want to edit(means save) after modified, I got an error #91 .
Thanks for pointing me to the right way always
COMMAND ON EDIT
Code:
Private Sub cmdEdit_Click()
'declare the variables
Dim findvalue As Range
'error handling
On Error GoTo errHandler:
'check for values
If reg1.Value = "" Or reg2.Value = "" Then
MsgBox "There is no data to edit"
Exit Sub
End If
'edit the row
Set findvalue = Sheet2.Range("D:D").Find(What:=reg4, LookIn:=xlValues).Offset(0, -3)
'if the edit is a name then add it
Me.reg3.Value = Me.reg1.Value + ", " + Me.reg2.Value
For X = 1 To cNum
findvalue = Me.Controls("Reg" & X).Value
Set findvalue = findvalue.Offset(0, 1)
Next
'refresh the listbox
Lookup
'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
COMMAND ON LISTBOX
Code:
Private Sub lstLookup_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'declare the variables
Dim cPayroll As String
Dim I As Integer
Dim findvalue
'error block
On Error GoTo errHandler:
'get the select value from the listbox
For I = 0 To lstlookup.ListCount - 1
If lstlookup.Selected(I) = True Then
cPayroll = lstlookup.List(I, 1)
End If
Next I
'find the payroll number
Set findvalue = Sheet2.Range("C:C").Find(What:=cPayroll, LookIn:=xlValues)
If findvalue Is Nothing Then
MsgBox cPayroll & " not found"
Exit Sub
Else
Set findvalue = findvalue.Offset(, -2)
End If
'add the database values to the userform
cNum = 13
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
COMMAND ON SEARCH
Code:
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 Sheet2.Range("D:D")
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, 1)
lstlookup.List(lstlookup.ListCount - 1, 3) = rngFind.Offset(0, 2)
lstlookup.List(lstlookup.ListCount - 1, 4) = rngFind.Offset(0, 4)
lstlookup.List(lstlookup.ListCount - 1, 5) = rngFind.Offset(0, 5)
lstlookup.List(lstlookup.ListCount - 1, 6) = rngFind.Offset(0, 6)
lstlookup.List(lstlookup.ListCount - 1, 7) = rngFind.Offset(0, 7)
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.reg4.Enabled = True
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