执行插入时MS-Access运行时错误3073

时间:2017-03-06 16:38:32

标签: vba ms-access

这是我的情景。我将Access 2010数据库拆分为前端和后端,使用VBA代码处理我的数据。直到上周,当我每月进行质量插入时,我没有遇到任何问题。通过将csv文件作为Excel电子表格读取并将数据插入Access表(未插入所有数据,因此必须单独处理每行)来完成插入。当我星期五跑,我突然开始得到随机的3073错误(操作必须使用可更新的查询)。它可以发生在数据库中的任何表上。

我在我的错误处理程序中重新使用调试并设置断点,并发现如果我从断点移回查询执行命令并单步执行它,我不会收到错误。这是一个示例插入:

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,我怀疑它会解决这个问题,但是在这种情况发生之前,如果我能够解决这个问题会很好......

1 个答案:

答案 0 :(得分:1)

可能是使用''对于表格期望 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

请研究典型用法的在线评论。