2014-03-27 48 views
1

我redid搜索代碼,並將其寫爲vba代碼,而不是查詢,但現在我遇到了錯誤,現在它是所有設置。代碼如下:搜索代碼錯誤

我得到的對象所需424的代碼,它是突出

If Me!txtEmpID Is Not Null Then 


Private Sub cmdSearch2_Click() 

Dim strSQL As String 

strSQL = "" 

If Me!txtEmpID Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[EmployeeID] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!txtEmpName Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[EmployeeName] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboEEOC Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[EEOC] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboGender Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Gender] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!txtDivision Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Division] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboRR Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Region] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboDD Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[District] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboJobGroupCode Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[JobGroupCOde] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!txtCenter Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Center] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!txtJobD Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[JobDesc] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboJobGroup Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[JobGroup] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboFunction Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Function1] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboMtgReadyLvl Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[MeetingReadinessRating] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboMgrReadyLvl Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[ManagerReadinessRating] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!txtFeedback Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[EmployeeFeedback] LIKE '*" & Me!txtEmpID & " * ' " 
End If 
If Me!cboDevelopment1 Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _ 
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _ 
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _ 
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment1 & " * ' " & _ 
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment1 & " * ') " 
End If 

If Me!cboDevelopment2 Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _ 
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _ 
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _ 
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment2 & " * ' " & _ 
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment2 & " * ') " 
End If 

If Me!cboDevelopment3 Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _ 
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _ 
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _ 
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment3 & " * ' " & _ 
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment3 & " * ') " 
End If 

If Me!cboDevelopment4 Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _ 
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _ 
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _ 
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment4 & " * ' " & _ 
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment4 & " * ') " 
End If 

If Me!cboDevelopment5 Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"([DevelopmentForEmployee1] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _ 
"OR [DevelopmentForEmployee2] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _ 
"OR [DevelopmentForEmployee3] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _ 
"OR [DevelopmentForEmployee4] LIKE '*" & Me!Me!cboDevelopment5 & " * ' " & _ 
"OR [DevelopmentForEmployee5] LIKE '*" & Me!Me!cboDevelopment5 & " * ') " 
End If 

If Me!txtJustification Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Justification] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!txtNotes Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Notes] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

If Me!cboChanged Is Not Null Then 
strSQL = strSQL & IIf(strSQL = "", "", " AND ") & _ 
"[Changed] LIKE '*" & Me!txtEmpID & " * ' " 
End If 

strSQL = "SELECT * FROM [CDData] WHERE " & strSQL 

DoCmd.RunSQL strSQL 

End Sub 

回答

2

在if語句稍有不同檢查空:

If Not IsNull(Me!txtEmpID) Then 

等等上。

+0

這工作,現在我得到一個錯誤閱讀:運行時錯誤3129 INVALID SQL STATEMEMENT和它有DOCMD.RUNSQL strSQL突出顯示 – user2119980

+1

@ user2119980當它給你一個錯誤值'strSQL'確保一個適當的where子句已創建。您甚至可以複製/粘貼該值(通過打印到直接窗口「strSQL」)並在查詢設計器中運行查詢。 – Brad