在子表单中搜索值(vba Access 2013)

时间:2015-04-05 18:39:11

标签: 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(*) 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

任何建议或替代解决方案都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您将使用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