运行时错误 3144
Posted
技术标签:
【中文标题】运行时错误 3144【英文标题】:Runtime error 3144 【发布时间】:2016-04-05 07:30:17 【问题描述】:我正在使用这段代码,一旦我运行它,我得到 。我不知道代码中的哪些地方需要更改或不正确。你能帮我解决它吗?
CurrentDb.Execute "UPDATE WORKED_HOURS " & _
" set EMPLOYEE_ID=" & `enter code here`Me.cboEmployeeID & _
", SET FIRST_NAME='" & Me.FIRST_NAME & "'" & _
", SET LAST_NAME='" & Me.LAST_NAME & "'" & _
", Set WORK_ORDER_NO='" & Me.cboWorkOrderNo & "'" & _
", SET PROJECT_TITLE='" & Me.txtProjectTitle & "'" & _
", SET WORKED_DATE='" & Me.txtWorkedDate & "'" & _
", SET TIME_STARTED='" & Me.txtTimeStarted & "'" & _
", SET TIME_FINISHED='" & Me.txtTimeFinished & "'" & _
", SET WEEK_ENDING='" & Me.txtWeekEnding & "'" & _
" where EMPLOYEE_ID=" & Me.cboEmployeeID.Tag
【问题讨论】:
在您的第二行...enter code here
是什么意思?复制到您的 SO 问题时这是一个错误吗?您可能应该尝试搜索运行时错误 3314 并查看它发生的原因并首先尝试。
更新 SQL 语句应该只保留 一个 SET 命令。用逗号分隔每个表达式,用引号将字符串括起来,用井号括起来日期等。最好使用参数化。
【参考方案1】:
您(还)需要正确格式化您的日期和时间值。
一种方法是应用此功能。用法见内联 cmets:
' Converts a value of any type to its string representation.
' The function can be concatenated into an SQL expression as is
' without any delimiters or leading/trailing white-space.
'
' Examples:
' SQL = "Select * From TableTest Where [Amount]>" & CSql(12.5) & "And [DueDate]<" & CSql(Date) & ""
' SQL -> Select * From TableTest Where [Amount]> 12.5 And [DueDate]< #2016/01/30 00:00:00#
'
' SQL = "Insert Into TableTest ( [Street] ) Values (" & CSql(" ") & ")"
' SQL -> Insert Into TableTest ( [Street] ) Values ( Null )
'
' Trims text variables for leading/trailing Space and secures single quotes.
' Replaces zero length strings with Null.
' Formats date/time variables as safe string expressions.
' Uses Str to format decimal values to string expressions.
' Returns Null for values that cannot be expressed with a string expression.
'
' 2016-01-30. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function CSql( _
ByVal Value As Variant) _
As String
Const vbLongLong As Integer = 20
Const SqlNull As String = " Null"
Dim Sql As String
Dim LongLong As Integer
#If Win32 Then
LongLong = vbLongLong
#End If
#If Win64 Then
LongLong = VBA.vbLongLong
#End If
Select Case VarType(Value)
Case vbEmpty ' 0 Empty (uninitialized).
Sql = SqlNull
Case vbNull ' 1 Null (no valid data).
Sql = SqlNull
Case vbInteger ' 2 Integer.
Sql = Str(Value)
Case vbLong ' 3 Long integer.
Sql = Str(Value)
Case vbSingle ' 4 Single-precision floating-point number.
Sql = Str(Value)
Case vbDouble ' 5 Double-precision floating-point number.
Sql = Str(Value)
Case vbCurrency ' 6 Currency.
Sql = Str(Value)
Case vbDate ' 7 Date.
Sql = Format(Value, " \#yyyy\/mm\/dd hh\:nn\:ss\#")
Case vbString ' 8 String.
Sql = Replace(Trim(Value), "'", "''")
If Sql = "" Then
Sql = SqlNull
Else
Sql = " '" & Sql & "'"
End If
Case vbObject ' 9 Object.
Sql = SqlNull
Case vbError ' 10 Error.
Sql = SqlNull
Case vbBoolean ' 11 Boolean.
Sql = Str(Abs(Value))
Case vbVariant ' 12 Variant (used only with arrays of variants).
Sql = SqlNull
Case vbDataObject ' 13 A data access object.
Sql = SqlNull
Case vbDecimal ' 14 Decimal.
Sql = Str(Value)
Case vbByte ' 17 Byte.
Sql = Str(Value)
Case LongLong ' 20 LongLong integer (Valid on 64-bit platforms only).
Sql = Str(Value)
Case vbUserDefinedType ' 36 Variants that contain user-defined types.
Sql = SqlNull
Case vbArray ' 8192 Array.
Sql = SqlNull
Case Else ' Should not happen.
Sql = SqlNull
End Select
CSql = Sql & " "
End Function
【讨论】:
【参考方案2】:几乎每一种 SQL 方言中的更新语句都不允许使用多个 SET 命令,而只允许一个包含多个用逗号分隔的表达式的命令。考虑这样做并根据数据类型用引号 (strings)、井号 (dates) 或空字符 (integer) 正确括起来:
CurrentDb.Execute "UPDATE WORKED_HOURS " _
& " SET EMPLOYEE_ID=" & Me.cboEmployeeID _
& " , FIRST_NAME='" & Me.FIRST_NAME & "'" _
& " , LAST_NAME='" & Me.LAST_NAME & "'" _
& " , WORK_ORDER_NO='" & Me.cboWorkOrderNo & "'" _
& " , PROJECT_TITLE='" & Me.txtProjectTitle & "'" _
& " , WORKED_DATE=#" & Me.txtWorkedDate & "#" _
& " , TIME_STARTED=#" & Me.txtTimeStarted & "#" _
& " , TIME_FINISHED=#" & Me.txtTimeFinished & "#" _
& " , WEEK_ENDING=#" & Me.txtWeekEnding & "#" _
& " WHERE EMPLOYEE_ID=" & Me.cboEmployeeID.Tag
但为了获得最佳实践,请考虑使用参数化的 querydef,它甚至可以避免所需的引号或井号。 Jet/ACE SQL 允许使用PARAMETERS
子句声明参数。
Dim qdef As querydef
strSQL = "PARAMETERS [EMPLOYEE_ID] Integer, [FIRST_NAME] Text(255)," _
& " [LAST_NAME] Text(255), [WORK_ORDER_NO] Text(255)," _
& " [PROJECT_TITLE] Text(255), [WORKED_DATE] Datetime," _
& " [TIME_STARTED] Datetime, [TIME_FINISHED] Datetime," _
& " [WEEK_ENDING] Datetime;"
& " UPDATE WORKED_HOURS " _
& " SET EMPLOYEE_ID = [EMPLOYEE_ID]" _
& " , FIRST_NAME = [FIRST_NAME]" _
& " , LAST_NAME = [LAST_NAME] " _
& " , WORK_ORDER_NO = [WORK_ORDER_NO] " _
& " , PROJECT_TITLE = [PROJECT_TITLE]" _
& " , WORKED_DATE = [WORKED_DATE]" _
& " , TIME_STARTED = [TIME_STARTED]" _
& " , TIME_FINISHED = [TIME_FINISHED]" _
& " , WEEK_ENDING = [WEEK_ENDING]" _
& " WHERE EMPLOYEE_ID = [EMPLOYEE_ID]"
Set qdef = Currentdb.CreateQueryDef("", strSQL)
qdef!EMPLOYEE_ID = Me.cboEmployeeID
qdef!FIRST_NAME = Me.FIRST_NAME
qdef!LAST_NAME = Me.LAST_NAME
qdef!WORK_ORDER_NO = Me.cboWorkOrderNo
qdef!PROJECT_TITLE = Me.txtProjectTitle
qdef!WORKED_DATE = Me.txtWorkedDate
qdef!TIME_STARTED = Me.txtTimeStarted
qdef!TIME_FINISHED = Me.txtTimeFinished
qdef!WEEK_ENDING = Me.txtWeekEnding
qdef.Execute
Set qdef = Nothing
【讨论】:
以上是关于运行时错误 3144的主要内容,如果未能解决你的问题,请参考以下文章