【问题标题】:Search for values in a subform (vba Access2013)在子表单中搜索值 (vba Access 2013)
【发布时间】:2015-04-05 18:39:11
【问题描述】:

我有一个打开的子表单(在表单内)。子表单基于查询并包含多条记录。我想搜索子表单以查找是否有任何记录具有 = 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

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

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      相关资源
      最近更新 更多