我有一个包含链接表的数据库 - Staff,Courses和Training_Record。每个工作人员都有一个数字主键,每个课程和Training_Record表中的每个条目都有。 Training_Record中的Staff_ID和Course_ID参考了员工和课程中的记录。
当添加工作人员或课程时,Training_Record(字段:Staff_ID,Course_ID,Date_Taken,Notes)有工作人员,插入课程记录 - 因此添加工作人员1将插入记录(1,1 ,,,),( 1,2 ,,,)等,添加课程8将插入记录(1,8 ,,,),(2,8 ,,,)等。这很有效。
然后我有一张记录训练的表格。用户选择课程,输入日期并从列表框中选择工作人员。我有一个触发VBA代码的保存按钮。从框中拉出日期和过程,我循环列表框,将选定的工作人员连接成一个字符串。这一切都有效,并显示一个消息框,验证这一点。然后,应该运行更新SQL查询,更新Training_Record。
我遇到的问题是SQL更新。我有一个更新查询,它将在SQL查询编辑器中工作,虽然它使用写入变量:
UPDATE Training_Record
SET Date_Taken = '12/12/12'
WHERE Staff_ID IN (1,2,3,4,5) AND Course_ID = 4
这会更新Training_Record,以显示1,2,3,4和5的工作人员在2012年12月12日参加了课程4。但是,在VBA中这不起作用。这是我在VBA中的SQL查询:
strSQL = "UPDATE Training_Record" _
& "SET Date_Taken = (" & strDate & ")" _
& "WHERE Staff_ID IN (" & strCriteria & ") AND Course_ID = (" & strCourse & ")"
DoCmd.RunSQL strSQL
代码生成的错误是“运行时错误'3144':UPDATE语句中的语法错误。”并且调试器突出显示查询后面的DoCmd.RunSQL语句。整个VBA代码:
Private Sub SaveTraining_Click()
Dim db As DAO.Database
Dim VarItem As Variant
Dim strCriteria As String
Dim strDate As Variant
Dim strCourse As Variant
Dim strSQL As String
Set db = CurrentDb()
'Extract the course ID and the training date from the form
strCourse = Me!CourseID.Value
strDate = Me!TrainingDate.Value
'Dealing with empty boxes- zero length
If IsNull(strCourse) Then
MsgBox "Please select a course." _
, vbOKOnly, "No course selected"
End If
If IsNull(strDate) Then
MsgBox "Please enter a date." _
, vbOKOnly, "No date given"
End If
If StaffMembers.ItemsSelected.Count = 0 Then
MsgBox "Please select staff members." _
, vbOKOnly, "No staff members"
End If
If (Not IsNull(strCourse)) And (Not IsNull(strDate)) And (StaffMembers.ItemsSelected.Count > 0) Then
'Extract each selected member and concatenate into a string for sql query
For Each VarItem In Me!StaffMembers.ItemsSelected
strCriteria = strCriteria & "," & Me!StaffMembers.ItemData(VarItem)
Next VarItem
'Gets rid of extra comma on query string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
'Message box
MsgBox ("Staff: " & strCriteria & vbNewLine & "Date: " & strDate & vbNewLine & "Course: " & strCourse & vbNewLine & "No. Selected staff: " & StaffMembers.ItemsSelected.Count)
strSQL = "UPDATE Training_Record" _
& "SET Date_Taken = (" & strDate & ")" _
& "WHERE Staff_ID IN (" & strCriteria & ") AND Course_ID = (" & strCourse & ")"
DoCmd.RunSQL strSQL
End If
Set db = Nothing
End Sub
TL; DR我无法在VBA中运行SQL UPDATE查询
我觉得某处语法错误,但我无法找到。任何想法/建议都将非常感谢,谢谢。
答案 0 :(得分:2)
我认为你只是在行的末尾缺少空格
您的旧查询打印
UPDATE Training_RecordSET Date_Taken = ()WHERE Staff_ID IN () AND Course_ID = ()
正如您所看到的,在关键字SET
和WHERE
之前会发生名称冲突
因此将strSQL
更改为
strSQL = "UPDATE Training_Record " _
& "SET Date_Taken = (" & strDate & ") " _
& "WHERE Staff_ID IN (" & strCriteria & ") AND Course_ID = (" & strCourse & ")"
打印为(没有提供值)
UPDATE Training_Record SET Date_Taken = () WHERE Staff_ID IN () AND Course_ID = ()
在SQL语法方面是正确的
如果我是你,我还会检查Training_Record
表
通常 (这适用于类型不匹配错误),
对于使用#
示例& "SET Date_Taken = (#" & strDate & "#) ...
表示您使用单引号'
示例WHERE Operator_Name = ('" & operName & "') ...
对于数值,除了强制转换以提供正确的数据类型
之外,您不需要使用任何其他内容答案 1 :(得分:0)
我的猜测:
strSQL = "UPDATE Training_Record" _
& "SET Date_Taken = (#" & Format(strDate, "mm\/dd\/yyyy") & "#)" _
& "WHERE Staff_ID IN (" & strCriteria & ") AND Course_ID = (" & strCourse & ")"
如果staff_ID是一个字符串:
strSQL = "UPDATE Training_Record" _
& "SET Date_Taken = (#" & Format(strDate, "mm\/dd\/yyyy") & "#)" _
& "WHERE Staff_ID IN ('" & strCriteria & "') AND Course_ID = (" & strCourse & ")"