我对下面的代码有疑问,该代码基于组合框选择或Access窗体上的文本框条目创建新查询。
有时当我运行查询时它工作正常,有时候我会遇到两个错误之一。我认为这与
有关非常感谢您提供的任何帮助。我已经尝试了几个小时的各种不同的解决方案,但我很短暂。
我收到的第一个错误是Run-time error 3167 Record is deleted
行DoCmd.OpenQuery "NewQuery"
。
我在其他时间收到的下一个错误是Run-time error 3012 Object "NewQuery" already exists
Set qdf = CurrentDb.CreateQueryDef("NewQuery",sqlquery)
行。{
谢谢。
On Error Resume Next
CurrentDb.QueryDefs.Delete "NewQuery"
If Text24 <> "" Then
sqlquery = "SELECT * FROM TABLE WHERE TABLE.WorkerID = [Forms]![UserForm]![Text24];"
Else
ComboBox_condition = ""
If ComboBox0 <> "" Then
ComboBox_condition = ComboBox_condition + " TABLE.PayGroupRegionCode = '" & ComboBox0 & "'"
End If
If ComboBox0 <> "" And ComboBox4 <> "" Then
ComboBox_condition = ComboBox_condition + " TABLE.PayGroupCountryDesc = '" & ComboBox4 & "'"
ElseIf ComboBox4 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.PayGroupCountryDesc = '" & ComboBox4 & "'"
End If
If ComboBox0 <> "" And ComboBox4 <> "" And ComboBox6 <> "" Then
ComboBox_condition = ComboBox_condition + " TABLE.JobFamilyGroup = '" & ComboBox6 & "'"
ElseIf ComboBox6 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.JobFamilyGroup = '" & ComboBox6 & "'"
End If
If ComboBox0 <> "" And ComboBox4 <> "" And ComboBox6 <> "" And ComboBox10 <> "" Then
ComboBox_condition = ComboBox_condition + " TABLE.JobTitle = '" & ComboBox10 & "'"
ElseIf ComboBox10 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.JobTitle = '" & ComboBox10 & "'"
End If
If ComboBox0 <> "" And ComboBox4 <> "" And ComboBox6 <> "" And ComboBox10 <> "" And ComboBox12 <> "" Then
ComboBox_condition = ComboBox_condition + " TABLE.JobCode = '" & ComboBox12 & "'"
ElseIf ComboBox12 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.JobCode = '" & ComboBox12 & "'"
End If
If ComboBox0 <> "" And ComboBox4 <> "" And ComboBox6 <> "" And ComboBox10 <> "" And ComboBox12 <> "" And ComboBox14 <> "" Then
ComboBox_condition = ComboBox_condition + " TABLE.CompensationGrade = '" & ComboBox14 & "'"
ElseIf ComboBox14 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.CompensationGrade = '" & ComboBox14 & "'"
End If
If ComboBox0 <> "" And ComboBox4 <> "" And ComboBox6 <> "" And ComboBox10 <> "" And ComboBox12 <> "" And ComboBox14 <> "" And ComboBox16 <> "" Then
ComboBox_condition = ComboBox_condition + " TABLE.SalaryAdministrationPlanCode = '" & ComboBox16 & "'"
ElseIf ComboBox16 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.SalaryAdministrationPlanCode = '" & ComboBox16 & "'"
End If
If ComboBox_condition <> "" Then
sqlquery = "SELECT * FROM TABLE WHERE " + ComboBox_condition
Else
sqlquery = "SELECT * FROM TABLE"
End If
End If
Set qdf = CurrentDb.CreateQueryDef("NewQuery", sqlquery)
DoCmd.OpenQuery "NewQuery"
答案 0 :(得分:1)
添加或删除querydef后,可以通过添加DB.QueryDefs.Refresh
来修复运行时错误。
Dim DB As Database
Set DB = CurrentDb
DB.QueryDefs.Delete "NewQuery"
DB.QueryDefs.Refresh
Set qdf = DB.CreateQueryDef("NewQuery", sqlquery)
DB.QueryDefs.Refresh
DoCmd.OpenQuery "NewQuery"
您的代码还存在其他一些问题。
您应该为表单控件提供有意义的名称,而不是ComboBox4
。这将有助于提高可读性。
ComboBox_condition
的构建过于复杂。避免所有这些加倍条件的最优雅的解决方案是:
始终使用AND
添加它们,包括第一个:
If ComboBox0 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.PayGroupRegionCode = '" & ComboBox0 & "'"
End If
' ...
If ComboBox12 <> "" Then
ComboBox_condition = ComboBox_condition + " AND TABLE.JobCode = '" & ComboBox12 & "'"
End If
' ...
然后在构建final语句时,添加一个始终为True的虚拟WHERE条件,因此WHERE子句始终有效:
sqlquery = "SELECT * FROM TABLE WHERE 1=1 " & ComboBox_condition
即使支票If ComboBox_condition <> ""
也可以省略。