[英]Update SQL MS Access 2010
This is wrecking my brains for 4 hours now, 现在这使我的脑袋瘫痪了四个小时,
I have a Table named BreakSked, 我有一个名为BreakSked的表,
and I this button to update the table with the break end time with this sql: 我用这个sql来用中断结束时间更新表:
strSQL1 = "UPDATE [BreakSked] SET [BreakSked].[EndTime] = " & _
Me.Text412.Value & " WHERE [BreakSked].AgentName = " & Me.List423.Value _
& " AND [BreakSked].ShiftStatus = '1'"
CurrentDB.Execute strSQL1
Text412 holds the current system time and List423 contains the name of the person. Text412保留当前系统时间,而List423包含人员名称。
I'm always getting this 我总是得到这个
"Run-time error 3075: Syntax Error (missing operator) in query expression '03:00:00 am'
“查询表达式“ 03:00:00 am”中的运行时错误3075:语法错误(缺少运算符)
Any help please? 有什么帮助吗?
EDIT: Thanks, now my records are updating. 编辑:谢谢,现在我的记录正在更新。 But now its adding another record instead of updating the record at hand.
但是现在它添加了另一个记录,而不是更新手头的记录。 I feel so silly since my program only has two buttons and I can't figure out why this is happening.
我感到很傻,因为我的程序只有两个按钮,我不知道为什么会这样。
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub Command536_Click()
strSQL1 = "UPDATE BreakSked SET BreakSked.EndTime = '" & Me.Text412.Value & "',BreakSked.Duration = '" & durationz & "' " & vbCrLf & _
"WHERE (([BreakSked].[AgentID]='" & Me.List423.Value & "'));"
CurrentDb.Execute strSQL1
CurrentDb.Close
MsgBox "OK", vbOKOnly, "Added"
End Sub
Private Sub Command520_Click()
strSql = "INSERT INTO BreakSked (ShiftDate,AgentID,StartTime,Status) VALUES ('" & Me.Text373.Value & "', '" & Me.List423.Value & "', '" & Me.Text373.Value & "','" & Me.Page657.Caption & "')"
CurrentDb.Execute strSql
CurrentDb.Close
MsgBox "OK", vbOKOnly, "Added"
End Sub
You wouldn't need to delimit Date/Time and text values if you use a parameter query. 如果使用参数查询,则无需分隔日期/时间和文本值。
Dim strUpdate As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
strUpdate = "PARAMETERS pEndTime DateTime, pAgentName Text ( 255 );" & vbCrLf & _
"UPDATE BreakSked AS b SET b.EndTime = [pEndTime]" & vbCrLf & _
"WHERE b.AgentName = [pAgentName] AND b.ShiftStatus = '1';"
Debug.Print strUpdate ' <- inspect this in Immediate window ...
' Ctrl+g will take you there
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", strUpdate)
qdf.Parameters("pEndTime").Value = Me.Text412.Value
qdf.Parameters("pAgentName").Value = Me.List423.Value
qdf.Execute dbFailOnError
And if you always want to put the current system time into EndTime
, you can use the Time()
function instead of pulling it from a text box. 而且,如果您始终希望将当前系统时间放入
EndTime
,则可以使用Time()
函数,而不是从文本框中提取它。
'qdf.Parameters("pEndTime").Value = Me.Text412.Value
qdf.Parameters("pEndTime").Value = Time() ' or Now() if you want date and time
However, if that is the case, you could just hard-code the function name into the SQL and dispense with one parameter. 但是,在这种情况下,您可以将函数名称硬编码到SQL中,而无需使用一个参数。
"UPDATE BreakSked AS b SET b.EndTime = Time()" & vbCrLf & _
As I said in my comment you need to wrap date fields in "#" and string fields in escaped double quotes 正如我在评论中所说,您需要将日期字段包装在“#”中,并将字符串字段包装在转义的双引号中
strSQL1 = "UPDATE [BreakSked] SET [BreakSked].[EndTime] = #" & _
Me.Text412.Value & "# WHERE [BreakSked].AgentName = """ & Me.List423.Value & _
""" AND [BreakSked].ShiftStatus = '1'"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.