Update SQL MS Access 2010

2019-07-13 10:39发布

问题:

This is wrecking my brains for 4 hours now,

I have a Table named BreakSked,

and I this button to update the table with the break end time with this 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.

I'm always getting this

"Run-time error 3075: Syntax Error (missing operator) in query expression '03:00:00 am'

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

回答1:

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'"


回答2:

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.

'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.

"UPDATE BreakSked AS b SET b.EndTime = Time()" & vbCrLf & _