在子窗体中搜索值(vba Access2013)
我在表单中有一个打开的子表单。该子表单基于查询并包含多条记录。我想搜索子窗体以查找是否有任何记录的字段值为true 搜索之后,最好的建议是使用sql。这就是我正在处理的问题:-在子窗体中搜索值(vba Access2013),vba,ms-access,Vba,Ms Access,我在表单中有一个打开的子表单。该子表单基于查询并包含多条记录。我想搜索子窗体以查找是否有任何记录的字段值为true 搜索之后,最好的建议是使用sql。这就是我正在处理的问题:- Dim iRecCount As Integer Dim strRecCount As String Dim vInvoiceID as Variant vInvoiceID = [Forms]![Invoices]![InvoiceID].Value strRecCount = "SELECT Count(*)
Dim iRecCount As Integer
Dim strRecCount As String
Dim vInvoiceID as Variant
vInvoiceID = [Forms]![Invoices]![InvoiceID].Value
strRecCount = "SELECT Count(*) AS CountOfSlotID FROM (Appointments INNER JOIN Students ON Appointments.StudentID = Students.StudentID) INNER JOIN Invoices ON Appointments.InvoiceID = Invoices.InvoiceID WHERE (((Appointments.InvoiceID)=" & vInvoiceID & ") AND ((Students.PAYG)=Yes));"
iRecCount = CurrentDb.OpenRecordset(strRecCount).Fields(0).Value
If iRecCount > 0 Then
[Forms]![Invoices]![Temp Termly].Value = True
Else: [Forms]![Invoices]![Temp Termly].Value = False
End If
如果我在查询中复制并粘贴SQL字符串,它会给出正确的结果,事实上,它是从一个工作查询中获取SQL字符串的。但是,无论发生什么情况,此代码都返回零。我觉得这句话有点不对劲:-
iRecCount = CurrentDb.OpenRecordset(strRecCount).Fields(0).Value
任何建议或替代解决方案都会有所帮助。您可以使用RecordsetClone:
Dim rs As DAO.Recordset
Dim Found As Boolean
Set rs = Me!SubformControlName.Form.RecordsetClone
If rs.RecordCount > 0 Then
rs.FindFirst "[Temp Termly] = True"
Found = Not rs.NoMatch
End If
Set rs = Nothing
If Found = True Then
' Success.
End If