I am getting Run time error 91 in below ** area.I couldn't figure it out where is the problem. What i try is, to avoid to enter duplicate pin number.

Code:
Public Function Custom_PIN(ByVal TableName As String, _
    ByRef EmployeeCode As String, ByVal FieldName As String, ByVal DataToCheck As String, _
    Optional ByVal CodeFieldName As String = Empty, Optional ByVal CodeFieldValue As String = Empty) As Boolean

Dim
...