这是我的设想。我有一个Access 2010数据库分成前端和后端,使用VBA代码处理我的数据。直到上个星期,当我做每月的批量插入时,我没有任何问题。插入是通过将csv文件作为Excel电子表格读取并将数据插入访问表(并不是所有的数据都将被插入,因此每一行必须单独处理)来完成的。当我周五运行时,我突然开始随机获取3073个错误(操作必须使用可更新的查询)。它可以发生在数据库中的任何一个表上。
我在错误处理程序中使用debug和设置断点重新运行,并发现如果从断点移回查询执行命令并逐步执行它,则不会得到错误。下面是一个示例插入:
INSERT INTO OtherMeasures (HapId, SurveyTypeId, Score, Comments)
VALUES (86792, 9, 21.00, '').在这里,创建SQL的VBA代码:
Private Sub storeOthType(ByVal hapId As Long, ByVal othType As String, _
ByVal othScore As Integer, ByVal othComment As String, _
ByRef db As DAO.Database)
Dim sql As String
On Error GoTo foundError
Dim typeId As Integer
typeId = lookupId(othType, "Description", "DataTypes", False, db)
sql = "INSERT INTO OtherMeasures (HapId, SurveyTypeId, Score, " + _
Comments) " + _
"VALUES (" + CStr(hapId) + ", " + CStr(typeId) + ", " + _
Format(othScore, "##0.00") + ", '" + _
Replace(othComment, "'", "''") + "')"
db.Execute sql, dbFailOnError
Exit Sub
foundError:
MsgBox "Error in storeOthType: " + CStr(Err.Number) + ", " + _
Err.description + ". SQL: " + sql + "."
logError "Error in storeOthType: " + CStr(Err.Number) + ", " + _
Err.description + ". SQL: " + sql + "."
End Sub 'storeOthType有人知道是什么原因造成的吗?我们计划将后端移到SQL Server,我怀疑它将解决这个问题,但在此之前,如果我能够解决这个问题,那就太好了。
发布于 2017-03-06 18:48:43
它可能是用于空字符串的'‘,而表期望空字符串为Null。
您可能会发现这个函数非常方便,可以避免这样的情况:
' 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请研究典型用法的在线评论。
https://stackoverflow.com/questions/42630902
复制相似问题