Access INSERT vba Query在调试中运行,但不在正常操作下运行

时间:2013-08-29 02:14:43

标签: ms-access ms-access-2007 access-vba

我在Access 2007中有一个导入函数,它读取文本文件,然后根据行的内容将该行导入到许多表中。

当我运行该函数时,我的一个表没有完全填充(只有大约100条记录,而它应该包含大约700条记录),这会对导入函数的其余部分产生怀疑。

当我在代码中放置一个断点并停止每一行时,或者当我单步执行代码时它会正确执行。然后我放入一个计数器并休息一下,以便我可以运行代码段并发现它也失败了。任何想法都将不胜感激。

样品:

Function MainLoop
   ....
   do while not fs.eof
       strLine = fs.ReadLine
       if instr(1,strLine, "transponder") then
          ImportTransponder strLine    'Break here and run loop once or step through and functions OK
       elseif instr(1, strLine, "CHK") then
           ImportChk strLine
       ....
       end if
    loop 
    ....
End Function

Function ImportTransponder(strLine as string)
    ....
    strSQL = "INSERT INTO tbl(Field1, Field2) VALUES (Value1, Value2)
    docmd.setwarnings false
    docmd.runSQL strSQL
    docmd.setwarnings true

End Function

1 个答案:

答案 0 :(得分:2)

您可能在这里放弃了有用的故障排除信息......

docmd.setwarnings false
docmd.runSQL strSQL
docmd.setwarnings true

关闭SetWarnings意味着当尝试INSERT失败时,您可能不会收到通知。这可能是那些丢失记录的解释。

至少暂时替换该部分代码......

DoCmd.SetWarnings True ' make sure it is on
CurrentDb.Execute strSQL, dbFailOnError

您可能会因各种原因发现INSERT次尝试失败。保持SetWarnings可以让您更好地找到原因。