【问题标题】:Insert SQL in VBA got runtime error 3134在 VBA 中插入 SQL 得到运行时错误 3134
【发布时间】:2014-01-12 15:43:58
【问题描述】:

SQL 插入语句给出 3134 运行时错误。为什么?

Private Sub Command0_Click()

Open "C:\Users\steven.EOPESTATE\Desktop\Sharp Sales\TRMSAVE01.txt" For Input As #1

        Do Until EOF(1)
            Dim TranSQL As String
            Line Input #1, varLine
            testvarline = Split(varLine, ",")
            If testvarline(0) = "$Tran" Then
            Debug.Print testvarline(0), testvarline(1), testvarline(2), testvarline(3), testvarline(4), testvarline(5), testvarline(6), testvarline(7), testvarline(8), testvarline(9)

            TranSQL = "Insert into Transaction ([Tran ID], [1], [2], [3], [4], [5], [6], [Date], [Time], [9])  Values (testvarline(0), testvarline(1),testvarline(2),testvarline(3),testvarline(4), testvarline(5),'testvarline(6)',# " & Format(testvarline(7), "MM/DD/YY") & "#,# " & Format(testvarline(8), "HH:MM:SS") & "#,testvarline(9)"
            DoCmd.RunSQL TranSQL

            End If

        Loop

Close #1

【问题讨论】:

    标签: sql vba insert


    【解决方案1】:

    您实际上是在尝试将testvarline(0) 插入tran ID,而不是插入值,即简化您尝试运行的插入语句:

    INSERT INTO Transaction ([Tran ID])
    VALUES (testvarline(0));
    

    这不是有效的 SQL,您需要正确构建您的插入语句:

    TranSQL = "INSERT .... VALUES(" & testvarline(0) & ", " & testvarline(1) & ...
    

    所以你实际上是把数组中的值放入插入语句中。

    不幸的是,我对 VBA 的了解并没有扩展到使用参数化查询,如果这可能的话,我会尽可能地向 SQL Injection 推荐它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多