2017-08-10 69 views
0

我工作的一些代碼,檢查特定值的數據庫,並返回在一定條件下的字符串。做每個記錄的東西,在一個記錄

我不能得到這個代碼以迭代到下一行我的數據庫結果。看來我的代碼只檢查記錄集的第一條記錄,然後繼續。

有時候,我的記錄可能只有一排,但有時它可能有很多行。如果這個記錄集的任何行都有一個特定的值,我想拋出一個MsgBox。不只是第一個或最後一個記錄。

這裏是我的代碼:

'Database Connection Strings. 
strServerName = "string" 
strDatabase = "string" 
strUserName = "string" 
strPassword = "string" 

'Connection string for SQL Server. 
strConn = "Driver={SQL Server};Server=" & strServerName & ";Database=" & strDatabase & ";Uid=" & strUsername & ";Pwd=" & strPassword & ";" 

'Create required objects for the session. 
Set WShell = CreateObject("WScript.Shell") 
Set db = CreateObject("ADODB.Connection") 
Set rs = CreateObject("ADODB.Recordset") 

'Create the array of reciever lines and count them. 
Set arrayLine = LINES.Value 
intNumOfLines = arrayLine.Count 
intLineNum = Cint(intNumOfLines)-1 
cleanPN2 = array() 
skipArray = array("-", " ", "PLATE", "HEAT-TREAT", "PAINT", "MACHINE", "WELD", "MPI") 

result = MsgBox ("Scan PO for cert requirements?", vbYesNo, "PO Requirement Scanner") 

Select Case result 
    Case vbYes 
    'Iterate through the reciever lines and look for part numbers. 
    For intLineNum = 0 To intNumOfLines 
     If intLineNum = intNumOfLines Then 
     Exit For 
     End If 
     Set arrayLine = LINES(intLineNum).Value 
     strPN = arrayLine("VENDOR_PART_ID") 
     cleanPN = split(strPN, " ") 
     For Each iteration in cleanPN 
     iteration = LTrim(RTrim(iteration)) 
     ReDim Preserve cleanPN2(UBound(cleanPN2) + 1) 
     cleanPN2(UBound(cleanPN2)) = iteration 
     Next 
    Next 

    'Take any part numbers that were found and search the WO Master for operations that require certs. 
    For Each cleanPN3 In cleanPN2 
     strSQL = "SELECT USER_3 FROM OPERATION WHERE WORKORDER_BASE_ID = " & "'" & cleanPN3 & "';" 
     db.Open strConn, db 
     rs.Open strSQL, db 
     If Not rs.EOF And Not rs.BOF Then 
     strUSER3 = rs("USER_3") 
     Do While rs("USER_3") = Null 
      strUSER3 = rs("USER_3").MoveNext 
     Loop 
     If (strUSER3 <> Null) Or (strUSER3 <> "") Then 
      MsgBox "Certifications are required for part number " & cleanPN3 & "!", vbOKOnly 
     End If 
     End If 
     rs.Close 
     db.Close 
    Next 

    MsgBox "PO Scan Complete!" 

    Case vbNo 
    MsgBox("PO Scan Cancelled!") 
End Select 

回答

0

假定其他一切工作,你想

rs.Open strSQL, db 
    If Not rs.BOF Then 
    rs.MoveFirst 
    Do While Not rs.EOF 
     If Len(Trim(Cstr(rs.fields("USER_3").value)) > 0 Then 
      MsgBox "Certifications are required for part number " & cleanPN3 & "!", vbOKOnly 
     End If 
     rs.MoveNext 
    Loop 
    End If 
    rs.Close 
+0

謝謝!我不得不調整你的代碼一點點,但是它會讓我在這完美的作品目前的解決方案!你也幫我理解了rs.MoveNext是如何工作的。非常感謝你。我所做的只是改變「if」語句在你的崗位上的中間......「如果(rs.fields(」 USER_3「)。價值=‘Y’),那麼」 – zelon88