【问题标题】:VBA coding on access访问时的 VBA 编码
【发布时间】:2014-10-15 12:32:50
【问题描述】:

我正在尝试查看以下代码的问题所在。

'add data to table
CurrentDb.Execute "INSERT INTO IB Students(Student Name, Gender, IB) " & _
"VALUES (" & Me.Text6 & ",'" & Me.Combo13 & "','" & Me.Text9 & "')"

'refresh data in list on form
stdfrm.Form.Requery

每当我尝试使用 add 命令时,我都会得到Runtime Error -3134,它说INSERT INTO 语句存在语法错误。

【问题讨论】:

  • 请在您的问题中添加更多措辞。该代码理论上应该可以工作。
  • 每当我尝试使用 add 命令时,我都会收到“运行时错误 - 3134”
  • 它说'INSERT INTO'语句中有语法错误
  • 这个有SQL注入漏洞,应该参数化,看看如果在Me.Text6中放一个'会发生什么

标签: ms-access vba


【解决方案1】:

可能是表名需要用括号括起来,文本值必须加引号:

'add data to table
sSQL = "INSERT INTO [IB Students] ([Student Name], Gender, IB) " & _
"VALUES ('" & Me.Text6 & "','" & Me.Combo13 & "','" & Me.Text9 & "')"

'print SQL for debug
Debug.print sSQL  

'Run query
CurrentDb.Execute sSQL

'refresh data in list on form
stdfrm.Form.Requery

注意:如果Me.Text6(和其他)未绑定到数据源,那么您必须将其用作Me.Text6.Value

编辑:在运行查询之前检查长度可能是个好主意

【讨论】:

  • [Student Name] 同上
  • 是的,你是对的@AlexK。我刚刚修改了我的答案...谢谢
  • 我仍然收到相同的 3134 运行时错误,但这次调试器指向“CurrentDb.Execute sSQL”
  • 打印出来的SQL是什么? (按 Alt+F11,然后转到即时窗口)
  • Alt+F11 转到 VBA 窗口CTRL+G 转到 立即窗口.
【解决方案2】:

当我打开即时窗口时,它会显示

    INSERT INTO [IB Students] (Student Name, Gender, IB) VALUES (Micheal,'Male','2')
INSERT INTO [IB Students] (Student Name, Gender, IB) VALUES (John,'Male','1')
INSERT INTO [IB Students] ([Student Name], Gender, IB) VALUES (,'','')
INSERT INTO [IB Students] ([Student Name], Gender, IB) VALUES (,'','')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    相关资源
    最近更新 更多