多插入问题

时间:2009-07-13 20:51:41

标签: sql sql-server ms-access vba syntax

我在vba访问查询中进行多次插入,如下所示:

Private Sub btnSubmit_Enter()
DoCmd.RunSQL ("insert into tblAutonumber (Dummy)values ('DummyValue')")
Dim lastAutonumber As Long
lastAutonumber = DMax("Autonumber", "tblAutonumber")
txtAutoNumber.Value = lastAutonumber
DoCmd.RunSQL ("insert into tbltesting " & _ 
    "(Empid, TestScenario, testid, owner, event, version, " & _ 
    "expresult, variation, status, homestore) values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtTestScenario.Value & "','" & _
        Me.txtAutoNumber.Value & " ' ,'" & _
        Me.txtOwner.Value & "','" & _
        Me.txtEvent.Value & "', '" & _
        Me.txtVersion.Value & "','" & _
        Me.txtExpectedResult.Value & "', '" & _
        Me.txtVariation.Value & "', '" & _
        Me.txtStatus.Value & "','" & _
        Me.txtHomeStore.Value & "')")

DoCmd.RunSQL ("insert into tblContract " & _ 
    "(Empid, testid, Day, Start1, Finish1, Store1," & _ 
    "Start2, Finish2, Store2 ) values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtAutoNumber.Value & "','Sunday ','" & _
        Me.txtContSunStart1.Value & "', '" & _
        Me.txtContSunFinish1.Value & "','" & _
        Me.txtContSunStore1.Value & "','" & _
        Me.txtContSunStart2.Value & "', '" & _
        Me.txtContSunFinish2.Value & "','" & _
        Me.txtContSunStore2.Value & "')")

DoCmd.RunSQL ("insert into tblContract " & _ 
    "(Empid, testid, Day, Start1, Finish1, Store1, " & _ 
    "Start2, Finish2, Store2 ) values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtAutoNumber.Value & "','Monday ','" & _
        Me.txtContMonStart1.Value & "', '" & _
        Me.txtContMonFinish1.Value & "','" & _
        Me.txtContMonStore1.Value & "','" & _
        Me.txtContMonStart2.Value & "', '" & _
        Me.txtContMonFinish2.Value & "','" & _
        Me.txtContMonStore2.Value & "')")

DoCmd.RunSQL ("insert into tblContract " & _ 
    "(Empid, testid, Day, Start1, Finish1, Store1," & _ 
    "Start2, Finish2, Store2 ) values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtAutoNumber.Value & "','Tuesday','" & _
        Me.txtContTueStart1.Value & "', '" & _
        Me.txtContTueFinish1.Value & "','" & _
        Me.txtContTueStore1.Value & "','" & _
        Me.txtContTueStart2.Value & "', '" & _
        Me.txtContTueFinish2.Value & "','" & _
        Me.txtContTueStore2.Value & "')")

DoCmd.RunSQL ("insert into tblContract " & _ 
    "(Empid, testid, Day, Start1, Finish1, Store1, " & _ 
    "Start2, Finish2, Store2 ) values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtAutoNumber.Value & "','Wednesday','" & _
        Me.txtContWedStart1.Value & "', '" & _
        Me.txtContWedFinish1.Value & "','" & _
        Me.txtContWedStore1.Value & "','" & _
        Me.txtContWedStart2.Value & "', '" & _
        Me.txtContWedFinish2.Value & "','" & _
        Me.txtContWedStore2.Value & "')")

DoCmd.RunSQL ("insert into tblContract " & _ 
    "(Empid, testid, Day, Start1, Finish1, Store1," & _ 
    "Start2, Finish2, Store2 ) values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtAutoNumber.Value & "','Thursday','" & _
        Me.txtContThuStart1.Value & "', '" & _
        Me.txtContThuFinish1.Value & "','" & _
        Me.txtContThuStore1.Value & "','" & _
        Me.txtContThuStart2.Value & "', '" & _
        Me.txtContThuFinish2.Value & "','" & _
        Me.txtContThuStore2.Value & "')")

DoCmd.RunSQL ("insert into tblContract " & _ 
    "(Empid, testid, Day, Start1, Finish1, Store1," & _ 
    "Start2, Finish2, Store2 )values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtAutoNumber.Value & "','Friday','" & _
        Me.txtContFriStart1.Value & "', '" & _
        Me.txtContFriFinish1.Value & "','" & _
        Me.txtContFriStore1.Value & "','" & _
        Me.txtContFriStart2.Value & "', '" & _
        Me.txtContFriFinsh2.Value & "','" & _
        Me.txtContFriStore2.Value & "')")

DoCmd.RunSQL ("insert into tblContract " & _ 
    "(Empid, testid, Day, Start1, Finish1, Store1," & _ 
    "Start2, Finish2, Store2 )values ('" & _
        Me.txtEmpNo.Value & "','" & _
        Me.txtAutoNumber.Value & "','Saturday','" & _
        Me.txtContSatStart1.Value & "', '" & _
        Me.txtContSatFinish1.Value & "','" & _
        Me.txtContSatiStore1.Value & "','" & _
        Me.txtContSatStart2.Value & "', '" & _
        Me.txtContSatFinish2.Value & "','" & _
        Me.txtContSatStore2.Value & "')")

End Sub

前两个查询工作正常(即到星期日)

但从星期一开始,执行中断了

我检查了查询中的所有变量,那里没有空值

5 个答案:

答案 0 :(得分:1)

打印你的连接,我打赌你可以看到你的错误......

答案 1 :(得分:1)

如果您的查询通过使用黄线和箭头转到代码窗口而“中断”,但没有给出任何错误消息,那么这是一个很好的症状,即编译的代码有些损坏。

如果这是你的症状,那么反编译&重新编译可能会纠正你的问题。

当您使用数据库调用Access时,可以通过添加开关/反编译来反编译。

"c:\Program Files\Microsoft office\office\msaccess.exe" 
                                  /decompile "c:\My Documents\MyDatabase.mdb"

通过转到Access中的代码窗口并使用菜单Debug-> Compile ...重新编译...

答案 2 :(得分:0)

如果没有更多地了解程序如何“打破”我的猜测是星期一的值之一包含SQL Server正在解释为语法元素的字符。换句话说,您将非转义字符串注入查询字符串。 (顺便说一下,这是SQL注入攻击的原因)。如果可能的话,你应该更喜欢参数,而不是像这样构建查询。

答案 3 :(得分:0)

如上面Colin Mackay所述,您的具体问题很可能是一堆无效的查询。

清除它......使其可见且可重复......然后将诊断程序卸载到msaccess ....

在变量中构建查询,以便您可以轻松地将它们转储出来......

您将遇到问题:

  1. 转储查询文本...(cntrl-g,然后输入? sqltxt
  2. 使用访问权限进行诊断。通过简单地将querytext复制到新查询(sql视图)并切换到设计模式或尝试运行查询来执行此操作... Access会抱怨并且通常会给您足够的提示来修复查询...
  3. 将来考虑为每个查询添加适当的错误处理。

    另外考虑使用currentdb.execute而不是docmd,因为对于后者,你可能需要采取额外的步骤来抑制确认框(根据我的msaccess2k的模糊内存)

    当前的 dlookup dmax (+ 1)是获取唯一密钥而不必求助于自动编号的便捷功能。我只是这样说,因为看起来你正在使用表来生成密钥......

    祝你好运!

答案 4 :(得分:0)

当您处于中断模式时,按Ctl + G打开调试窗口,然后键入:

? txtContTueStart1, txtContTueFinish1

等等。确保这些值没问题。