2

我得到一个连续的循环(我相信)。运行代码时,得到MsgBox提示,点击ok,程序就这样跑来跑去,永不结束?起初我认为这是与文件的连接错误,但如果是这种情况,我应该在 ADO 尝试连接到文件时收到错误,对吗?该文件不是那么大,只有 70 行。我设置 MsgBox 的方式,它应该提示在循环的每次迭代中单击 OK,但我从来没有收到另一个 MsgBox。建议?

' The following section reads from the elec_copy field's hyperlink
' It scans the Excel file for items it needs to include into the table
' It enters those cells into the TABLE 'items_needed_table'
'
' Selects row by row, and if the item has been marked TRUE, inserts
' That row into the TABLE 'items_needed_table'

' Open a connection to Excel
On Error Resume Next

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & elec_copy.Value & ";" & _
        "Extended Properties=""Excel 12.0 Macro;HDR=Yes;"";"

' Decalre a RecordSet Object
Set objRecordSet = CreateObject("ADODB.Recordset")

' Grab all Rows in the Plain_VDR Sheet where 'needed' column == TRUE
objRecordset.Open "SELECT line_no,desc,weeks FROM [Plain_VDR$] WHERE needed = TRUE", _
    objConnection, adOpenStatic, adLockOptimistic, adCmdText

' Write the information pulled, into the TABLE 'items_needed_table' in Access Database
Do Until objRecordset.EOF
    MsgBox("" & qd.Parameters("p2"))
    Set qd = data_base.CreateQueryDef("")
    qd.sql = "INSERT INTO items_needed_table(pr_no, line_no, desc, weeks) " & _
        "Values([p1],[p2],[p3],[p4])"
    qd.Parameters("p1").Value = pr_num.Value
    qd.Parameters("p2").Value = objRecorset.Fields.Item("line_no")
    qd.Parameters("p3").Value = objRecordset.Fields.Item("desc")
    qd.Parameters("p4").Value = objRecordset.Fields.Item("weeks")
    qd.Execute
    objRecordset.MoveNext
Loop

' Close Database connection
data_base.Close

提前感谢您的帮助!弥敦道

4

1 回答 1

4

去掉 On Error Resume Next 这一行。它几乎不应该被使用。由于该行,您不知道导致循环的错误发生在哪里。

另一个好主意是始终在每个模块的顶部使用 Option Explicit。这将确保您始终声明您的变量并节省比任何人想象的更多的悲伤。

于 2012-07-13T15:46:36.747 回答