我认为我几乎在这个项目中,但我需要一些帮助我的代码的SQL部分。我对SQL部分的经验有限,需要一些语法帮助,因为我得到了'运行时错误3075查询表达式中的语法错误缺少运算符'。如果我正确地将SQL与Query SQL结合使用。查询表中的SQL是。
SELECT warehouseMetrics.Date, warehouseMetrics.Client, Teammates.[User ID], JobDescriptions.[Job Code Description], warehouseMetrics.[Perf Percent], Supervisors.Supervisor
FROM ((Teammates INNER JOIN warehouseMetrics ON Teammates.[User ID] = warehouseMetrics.[User ID]) INNER JOIN Supervisors ON Teammates.Supervisor = Supervisors.Supervisor) INNER JOIN JobDescriptions ON warehouseMetrics.[Job Code Description] = JobDescriptions.[Job Code Description]
WHERE (((warehouseMetrics.Date) Between [Forms]![MasterForm]![txtBeginDate] And [Forms]![MasterForm]![txtEndDate]))
ORDER BY warehouseMetrics.Date;
运行查询的代码是......
Dim Q As QueryDef, DB As Database
Dim Criteria1 As String
Dim Criteria2 As String
Dim Criteria3 As String
Dim Criteria4 As String
Dim itm As Variant
Dim strSQL As String
Set DB = CurrentDb()
Set QueryDef = DB.QueryDefs("MasterReportQuery")
For Each itm In Me!lstSupervisor.ItemsSelected
Criteria1 = Criteria1 & "," & Me!lstSupervisor.ItemData(itm) & ""
Next itm
For Each itm In Me!lstClient.ItemsSelected
Criteria2 = Criteria2 & "," & Me!lstClient.ItemData(itm) & ""
Next itm
For Each itm In Me!lstTeammates.ItemsSelected
Criteria3 = Criteria3 & "," & Me!lstTeammates.ItemData(itm) & ""
Next itm
For Each itm In Me!lstJobs.ItemsSelected
Criteria4 = Criteria4 & "," & Me!lstJobs.ItemData(itm) & ""
Next itm
If Len(Criteria1) = 0 Or Len(Criteria2) = 0 Or Len(Criteria3) = 0 Or Len(Criteria4) = 0 Then
MsgBox "You did not select anything"
Exit Sub
End If
Criteria1 = Right(Criteria1, Len(Criteria1) - 1)
Criteria2 = Right(Criteria2, Len(Criteria2) - 1)
Criteria3 = Right(Criteria3, Len(Criteria3) - 1)
Cirteria4 = Right(Criteria4, Len(Criteria4) - 1)
MsgBox (Criteria1)
MsgBox (Criteria2)
MsgBox (Criteria3)
MsgBox (Criteria4)
strSQL = "SELECT warehouseMetrics.Date, warehouseMetrics.Client, Teammates. [User ID]," & _
"JobDescriptions.[Job Code Description], warehouseMetrics.[Perf Percent], Supervisors.Supervisor" & _
"FROM ((Teammates INNER JOIN warehouseMetrics ON Teammates.[User ID] = warehouseMetrics.[User ID])" & _
"INNER JOIN Supervisors ON Teammates.Supervisor = Supervisors.Supervisor) INNER JOIN" & _
"JobDescriptions ON warehouseMetrics.[Job Code Description] = JobDescriptions.[Job Code Description]" & _
"WHERE warehouseMetrics.Client IN (" & Criteria2 & ")" & _
"Supervisors.Supervisor IN (" & Criteria1 & ")" & _
"Teammates.[User ID] IN (" & Criteria3 & ")" & _
"JobDescriptions.[Job Code Description] IN (" & Criteria4 & ");"
QueryDef.SQL = strSQL
DoCmd.OpenQuery "MasterReportQuery"
Set DB = Nothing
Set QueryDef = Nothing
End Sub
答案 0 :(得分:0)
Criteria变量如果是text数据类型,则需要用单引号括起来。由于您循环遍历多个项目并在SQL中为每个条件使用IN语句,因此您可能需要在for循环中添加这些项目:
For Each itm In Me!lstSupervisor.ItemsSelected
Criteria1 = Criteria1 & ", '" & & Me!lstSupervisor.ItemData(itm) & "'"
Next itm
For Each itm In Me!lstClient.ItemsSelected
Criteria2 = Criteria2 & ", '" & Me!lstClient.ItemData(itm) & "'"
Next itm
For Each itm In Me!lstTeammates.ItemsSelected
Criteria3 = Criteria3 & ", '" & Me!lstTeammates.ItemData(itm) & "'"
Next itm
For Each itm In Me!lstJobs.ItemsSelected
Criteria4 = Criteria4 & ", '" & Me!lstJobs.ItemData(itm) & "'"
Next itm
SQL语句的间距也存在一些问题:
strSQL = "SELECT warehouseMetrics.Date, warehouseMetrics.Client, Teammates.[User ID], " & _
"JobDescriptions.[Job Code Description], warehouseMetrics.[Perf Percent], Supervisors.Supervisor " & _
"FROM ((Teammates INNER JOIN warehouseMetrics ON Teammates.[User ID] = warehouseMetrics.[User ID]) " & _
"INNER JOIN Supervisors ON Teammates.Supervisor = Supervisors.Supervisor) INNER JOIN " & _
"JobDescriptions ON warehouseMetrics.[Job Code Description] = JobDescriptions.[Job Code Description] " & _
"WHERE warehouseMetrics.Client IN (" & Criteria2 & ") " & _
"Supervisors.Supervisor IN (" & Criteria1 & ") " & _
"Teammates.[User ID] IN (" & Criteria3 & ") " & _
"JobDescriptions.[Job Code Description] IN (" & Criteria4 & ");"`