Private Function SearchEmployeeDatabase(sFirstName As String, sLastName As String, sBadge As String, sDepartment As String, sCenter As String, sDate As String) As Integer
'Declarations Section
Dim adoConnection As ADODB.Connection 'Used to establish connection with the Employee Database
Dim adoRecordset As ADODB.Recordset 'Used to query the Employee table
Dim connString As String 'Stores connection string
Dim SQLstr As String 'Stores query
'Dim iEmployeeID As Integer 'Stores the Employee's ID in Database
Dim iResponse As Integer 'Stores response from message box
Dim boolSaveToDB As Boolean 'Stores value indicating whether to save to DB or not
Dim x As Integer 'Counter for For Loop
Dim sPreviousLastName As String 'Used in case of change in last name
On Error GoTo AdoErrors
'Establish connection with FAE database
Set adoConnection = New ADODB.Connection
connString = "Provider=microsoft.jet.oledb.4.0;Data source=" & App.Path & "\FAEDB2.2.mdb; Jet OLEDB:Database password=f.a.e.1.0"
adoConnection.Open connString
'Verifies database connection
If adoConnection.State = adStateOpen Then
'Debug.Print "Connection was established"
SQLstr = "SELECT * From Employees WHERE BadgeNumber='" & sBadge & "'"
'Create Recordset Object and Search Employee Database
Set adoRecordset = New ADODB.Recordset
With adoRecordset
.Open SQLstr, adoConnection, adOpenKeyset, adLockOptimistic, adCmdText
'Gather employee information
If .RecordCount = 1 Then
If .Fields("FName").Value = sFirstName And .Fields("LName").Value = sLastName Then
'If this is the same name,Do all of this
gbDuplicatedBadge = False
'Stores Employee ID in variable
gEmployee.EmployeeID = .Fields("EmpID").Value
'Check to see if user has changed departments or centers
If .Fields("DeptID").Value = cboDepartment.ItemData(cboDepartment.ListIndex) Then
'Do nothing
Else
'Department changed. Save new department value to DB
.Fields("DeptID").Value = cboDepartment.ItemData(cboDepartment.ListIndex)
End If
If .Fields("CenterID").Value = cboCenter.ItemData(cboCenter.ListIndex) Then
'Do nothing
Else
'Center changed. Save new department value to DB
.Fields("CenterID").Value = cboCenter.ItemData(cboCenter.ListIndex)
End If
.Update
.MoveNext
ElseIf .Fields("FName").Value = sFirstName Then 'Assumes a possible change in last name
'Ask if user's last name changed.
iResponse = MsgBox("This badge number is already in use. Has your last name changed?", vbQuestion + vbYesNo, "Duplicated Badge Numbers")
If iResponse = vbNo Then
iResponse = MsgBox("Someone is already using this badge number. Would you like to make corrections?", vbExclamation + vbYesNo, "Duplicated Badge Numbers")
If iResponse = vbNo Then
'Show message box if user does not change badge numbers.
MsgBox "A note will be placed on your exam printout. Please send a copy to the PI Department so that the duplicated badge numbers can be resolved.", vbInformation + vbOKOnly, "Duplicated Badge Numbers"
gbDuplicatedBadge = True
boolSaveToDB = True
Else
gbDuplicatedBadge = False
boolSaveToDB = False
txtBadge.SetFocus
'Close recordset and connection
adoRecordset.Close
Set adoRecordset = Nothing
adoConnection.Close
Set adoConnection = Nothing
SearchEmployeeDatabase = vbCancel
Exit Function
End If
Else
Do
'Prompt user to enter previous last name
sPreviousLastName = InputBox("Please enter your previous last name and press enter.", "Changing Last Name")
Loop While sPreviousLastName = ""
sPreviousLastName = FormatText(sPreviousLastName)
'Check user's previous last name with last name in DB
If UCase(sPreviousLastName) = UCase(.Fields("LName").Value) Then
'Place code here that will update last name
adoRecordset!lname = sLastName
adoRecordset.Update
adoRecordset.MoveNext
MsgBox "Your last name has been updated!", vbInformation + vbOKOnly, "Duplicated Badge Numbers"
boolSaveToDB = False
Else
MsgBox "There is no record of " & sPreviousLastName & " in the database.", vbExclamation + vbOKOnly, "Changing Last Name"
boolSaveToDB = False
'Close recordset and connection
adoRecordset.Close
Set adoRecordset = Nothing
adoConnection.Close
Set adoConnection = Nothing
SearchEmployeeDatabase = vbCancel
End If
End If
Else
'Ask if user would like to change badge number.
iResponse = MsgBox("Someone is already using this badge number. Would you like to make corrections?", vbExclamation + vbYesNo, "Duplicated Badge Numbers")
If iResponse = vbNo Then
'Show message box if user does not change badge numbers.
MsgBox "A note will be placed on your exam printout. Please send a copy to the PI Department so that the duplicated badge numbers can be resolved.", vbInformation + vbOKOnly, "Duplicated Badge Numbers"
gbDuplicatedBadge = True
boolSaveToDB = True
Else
gbDuplicatedBadge = False
boolSaveToDB = False
txtBadge.SetFocus
'Close recordset and connection
adoRecordset.Close
Set adoRecordset = Nothing
adoConnection.Close
Set adoConnection = Nothing
SearchEmployeeDatabase = vbCancel
Exit Function
End If
End If
ElseIf .RecordCount > 1 Then
'Ask if user would like to change badge number.
iResponse = MsgBox("Someone is already using this badge number. Would you like to make corrections?", vbExclamation + vbYesNo, "Duplicated Badge Numbers")
If iResponse = vbNo Then
'Show message box if user does not change badge numbers.
MsgBox "A note will be placed on your exam printout. Please send a copy to the PI Department so that the duplicated badge numbers can be resolved.", vbInformation + vbOKOnly, "Duplicated Badge Numbers"
'Check to see if one of the duplicated badge numbers belong to user.
For x = 0 To .RecordCount - 1
If .Fields("FName").Value = sFirstName And .Fields("LName").Value = sLastName Then
'If one of the names match, do this
'Stores Employee ID in variable
gEmployee.EmployeeID = .Fields("EmpID").Value
gbDuplicatedBadge = True
'Check to see if user has changed departments or centers
If .Fields("DeptID").Value = cboDepartment.ItemData(cboDepartment.ListIndex) And _
.Fields("CenterID").Value = cboCenter.ItemData(cboCenter.ListIndex) Then
'Do nothing
ElseIf .Fields("DeptID").Value = cboDepartment.ItemData(cboDepartment.ListIndex) Then
'Do nothing
Else
'Change save new department value to DB
adoRecordset!DeptID = cboDepartment.ItemData(cboDepartment.ListIndex)
End If
If .Fields("CenterID").Value = cboCenter.ItemData(cboCenter.ListIndex) Then
'Do nothing
Else
'Change save new department value to DB
adoRecordset!CenterID = cboCenter.ItemData(cboCenter.ListIndex)
End If
.Update
Exit For