【发布时间】:2014-08-08 19:44:17
【问题描述】:
我的项目的一个方面涉及将操作员输入的零件编号与数据库列中的预定零件编号列表进行比较。现在,我的程序告诉我,电子表格中输入的每个零件号 (50+) 都与数据库中的任何零件号都不匹配,我已验证这是不正确的。我检查了电子表格部件号和数据库部件号都是字符串数据类型。我已经仔细检查了我的循环逻辑是否良好,对我来说似乎它应该可以工作。据我所知,数据库单元格或电子表格单元格中没有隐藏字符。在这一点上,我完全不知道为什么我的程序没有检测到电子表格和数据库之间的任何匹配。下面是包含用于检查零件编号是否匹配的代码的 Sub:
Sub CheckPN()
'Connect to the E2 database
Call SetPNConnection
'Open a recordset
Set PNRecordset = New ADODB.Recordset
PNRecordset.Open "EstimRpt", PNConnection, adOpenKeyset, adLockOptimistic, adCmdTable
PNSQLCmd = "SELECT DISTINCT [PartNo] FROM EstimRpt;"
'Loop through data, comparing part numbers to E2 database part number records
TotalBadPNCount = 0
With PNRecordset
For DataRowCount = 2 To TrackingLastRow
PNCount = 0
Part_Number = Tracking.Sheets("Operator Data").Range("A" & DataRowCount).Value
'MsgBox "The datatype for " & Part_Number & " is " & VarType(Part_Number) & "."
Do Until .EOF
'MsgBox "The datatype for " & .Fields("PartNo").Value & " is " & VarType(.Fields("PartNo").Value) & "."
If Part_Number = .Fields("PartNo").Value Then
'If .Fields("PartNo").Value = Part_Number Then
MsgBox Part_Number & " is a match."
PNCount = PNCount + 1
End If
.MoveNext
Loop
If PNCount < 1 Then
MsgBox "The P/N " & Part_Number & " entered in cell A" & DataRowCount & " is incorrect. Please correctly enter the P/N and re-run the program."
TotalBadPNCount = TotalBadPNCount + 1
End If
Next DataRowCount
If TotalBadPNCount >= 1 Then
Exit Sub
End If
End With
PNRecordset.Close
Set PNRecordset = Nothing
PNConnection.Close
Set PNConnection = Nothing
End Sub
附带说明,如果零件号不匹配,我希望整个程序停止执行,而不仅仅是直接子。目前,只有这个子在没有零件号匹配时退出。
感谢您对这两个问题的帮助。
约旦
【问题讨论】:
标签: sql excel ms-access-2007 vba