我正在尝试查询链接表。在得到结果后,我需要做两件事之一。无论是说它通过还是失败了。这是我写的代码。它没有给我任何错误,也没有正确运行。当我输入SN时,我知道没有任何条目,它不会运行第二部分(失败部分)。
Private Sub SQL_Check()
Dim rs As DAO.Recordset
Dim sqlMax As String
Dim result As String
sqlMax = "Select count(1) FROM dbo_Event WHERE [AssemblyNo] = 'SYSCHATESTE' and [SerialNo] = '" & Me.txtUnitNo & "' and [ProcessTyp] = 'SF1';"
Set rs = CurrentDb.OpenRecordset(sqlMax)
If rs.Fields.Count = 1 Then
txtECCT.BackColor = vbGreen
txtECCT.ForeColor = vbBlack
txtECCT.Value = "Passed"
GoTo la
End If
If rs.Fields.Count = 0 Then
Set rs = Nothing
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from tblScannedParts")
rec.AddNew
rec("Inspector") = Me.txtUserId
rec("PO Number") = Me.txtWorkOrderNo
rec("Assembly") = Me.txtAssem
rec("SERIAL Number") = Me.txtUnitNo
rec("DateScanned") = CStr(Now())
rec("Result") = "Failed"
rec("Defect Type") = "Missing ECCT"
rec("Comments") = strFileContent
rec("Qty") = "1"
rec.Update
txtStatus.BackColor = vbRed
txtStatus.ForeColor = vbBlack
txtStatus.Value = "Failed"
MsgBox "Please take unit to NCM Cart for review.", vbCritical, "Unit Not Ready for DOF QA"
txtAssem.SetFocus
End If
la:
Set rs = Nothing
End Sub
答案 0 :(得分:1)
问题是记录集rs
基于SELECT
,它会返回一个字段,因此您的If
条件rs.Fields.Count = 1
将始终为True。
相反,If rs.Fields.Count = 0 Then
块中的代码永远不能运行,因为Fields.Count
永远不会为零。实际上,Access甚至不评估该条件,因为您在前一个GoTo la
块中包含了If
...这会快速退出该过程。警惕GoTo
如果您希望将操作基于匹配记录的计数,我认为逻辑应该更简单DCount()
。
Dim strCriteria As String
Dim lngMatchCount As Long
strCriteria = "[AssemblyNo] = 'SYSCHATESTE' AND [SerialNo] = '" & Me!txtUnitNo.Value & "' AND [ProcessTyp] = 'SF1'"
Debug.Print strCriteria '<- inspect this in Immediate window; Ctrl+g will take you there
lngMatchCount = DCount("*", "dbo_Event", strCriteria)
If lngMatchCount > 0 Then
' do the 'Passed' thing
Else
' do the 'Failed' thing
End If