2012-07-13 3 views
2

나는 계속 루프를 반복하고있다 (나는 믿는다). 코드를 실행하면 MsgBox 프롬프트가 나타나고 확인을 클릭하면 프로그램이 실행되고 실행되고 끝나지 않습니다. 처음에는 파일 오류에 대한 연결이라고 가정했지만 ADO가 파일에 연결하려고하면 오류가 발생합니다. 맞습니까? 파일 크기가 커서 70ish 행만입니다. 나는 MsgBox가 설정된 방식대로 루프를 통해 모든 반복을 클릭 할 때 확인 메시지를 표시해야하지만 다른 MsgBox는 수신하지 않습니다. 제안?연속 루프 ADO VBA 액세스 2010

' 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 

도움을 주셔서 감사합니다. Nathan

답변

4

오류 제거 다시 시작 다음을 클릭하십시오. 거의 사용하지 않아야합니다. 이 줄로 인해 루프를 일으키는 오류가 어디에서 발생하는지 알 수 없습니다.

또 다른 좋은 생각은 항상 각 모듈의 맨 위에 Option Explicit를 사용하는 것입니다. 이렇게하면 항상 변수를 선언하고 누군가가 생각하는 것보다 많은 슬픔을 덜 수 있습니다.

+0

위시 'On Error Resume Next'와 'Option Explicit'모두에 주목하기 때문에 +2 할 수 있습니다. –

+0

@Remou 당신은 요즘 내 영웅입니다. 감사! – nathansizemore