C
carriey
Hello There, I got this code off of some website many moons ago and have used
it dozens of times in building Search Forms. For some reason, this time I
can't seem to get it to return any results. At one point I did have trouble
before with the SQL statement but have gone through all my notes and believe
that this is the same as working copies of code that I have - I even played
around with the &'s and _'s but that didn't seem to do the trick.
I have double and triple checked this but must be missing a really silly
mistake. If anyone out there with a fresh pair of eyes can spot my error I
will forever indebted to you!!!!
Option Compare Database
Option Explicit
Private Sub List_Results_DblClick(Cancel As Integer)
'Open Main Form based on the ID from List_Results
DoCmd.OpenForm "C_Frm_Main_Form", , , "[Employee_ID] = " & Me.List_Results,
, acDialog
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Search_Button_Click()
'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select Statement for the Row Source
strSQL = "SELECT C_Tbl_Employees.Employee_ID, C_Tbl_Employees.ADPEmployeeID,
Tbl_Department_Name.DepartmentName, C_Tbl_Employees.LastName,
C_Tbl_Employees.FirstName" _
& "FROM Tbl_Department_Name" _
& "RIGHT JOIN C_Tbl_Employees" _
& "ON Tbl_Department_Name.Tbl_Department_NameID =
C_Tbl_Employees.Lookup_Department"
strWhere = "WHERE"
strOrder = "ORDER BY C_Tbl_Employees.[Employee_ID];"
'Set the WHERE Clause for the Results
If Not IsNull(Me.TxtEmployID) Then
strWhere = strWhere & " (C_Tbl_Employees.[Employee_ID]) Like '*" &
Me.TxtEmployID & "*' AND"
End If
If Not IsNull(Me.txtADPID) Then
strWhere = strWhere & " (C_Tbl_Employees.[ADPEmployeeID]) Like '*" &
Me.txtADPID & "*' AND"
End If
If Not IsNull(Me.cboLast) Then
strWhere = strWhere & " (C_Tbl_Employees.[LastName]) Like '*" & Me.cboLast &
"*' AND"
End If
If Not IsNull(Me.cboFirst) Then
strWhere = strWhere & " (C_Tbl_Employees.[FirstName]) Like '*" & Me.cboFirst
& "*' AND"
End If
If Not IsNull(Me.cboDept) Then
strWhere = strWhere & " (Tbl_Department_Name.[DepartmentName]) Like '*" &
Me.cboDept & "*' AND"
End If
'Remove the last AND from the SQL statement
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Debug strSQL, strWhere and strOrder
Debug.Print strSQL
Debug.Print strWhere
Debug.Print strOrder
'Display strSQL Message Box --> remove single quote from line below if you
need to debug
'MsgBox strSQL
'Pass the SQL to the Rowsource of the listbox
Me.List_Results.RowSource = strSQL & " " & strWhere & " " & strOrder
End Sub
it dozens of times in building Search Forms. For some reason, this time I
can't seem to get it to return any results. At one point I did have trouble
before with the SQL statement but have gone through all my notes and believe
that this is the same as working copies of code that I have - I even played
around with the &'s and _'s but that didn't seem to do the trick.
I have double and triple checked this but must be missing a really silly
mistake. If anyone out there with a fresh pair of eyes can spot my error I
will forever indebted to you!!!!
Option Compare Database
Option Explicit
Private Sub List_Results_DblClick(Cancel As Integer)
'Open Main Form based on the ID from List_Results
DoCmd.OpenForm "C_Frm_Main_Form", , , "[Employee_ID] = " & Me.List_Results,
, acDialog
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Search_Button_Click()
'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select Statement for the Row Source
strSQL = "SELECT C_Tbl_Employees.Employee_ID, C_Tbl_Employees.ADPEmployeeID,
Tbl_Department_Name.DepartmentName, C_Tbl_Employees.LastName,
C_Tbl_Employees.FirstName" _
& "FROM Tbl_Department_Name" _
& "RIGHT JOIN C_Tbl_Employees" _
& "ON Tbl_Department_Name.Tbl_Department_NameID =
C_Tbl_Employees.Lookup_Department"
strWhere = "WHERE"
strOrder = "ORDER BY C_Tbl_Employees.[Employee_ID];"
'Set the WHERE Clause for the Results
If Not IsNull(Me.TxtEmployID) Then
strWhere = strWhere & " (C_Tbl_Employees.[Employee_ID]) Like '*" &
Me.TxtEmployID & "*' AND"
End If
If Not IsNull(Me.txtADPID) Then
strWhere = strWhere & " (C_Tbl_Employees.[ADPEmployeeID]) Like '*" &
Me.txtADPID & "*' AND"
End If
If Not IsNull(Me.cboLast) Then
strWhere = strWhere & " (C_Tbl_Employees.[LastName]) Like '*" & Me.cboLast &
"*' AND"
End If
If Not IsNull(Me.cboFirst) Then
strWhere = strWhere & " (C_Tbl_Employees.[FirstName]) Like '*" & Me.cboFirst
& "*' AND"
End If
If Not IsNull(Me.cboDept) Then
strWhere = strWhere & " (Tbl_Department_Name.[DepartmentName]) Like '*" &
Me.cboDept & "*' AND"
End If
'Remove the last AND from the SQL statement
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Debug strSQL, strWhere and strOrder
Debug.Print strSQL
Debug.Print strWhere
Debug.Print strOrder
'Display strSQL Message Box --> remove single quote from line below if you
need to debug
'MsgBox strSQL
'Pass the SQL to the Rowsource of the listbox
Me.List_Results.RowSource = strSQL & " " & strWhere & " " & strOrder
End Sub