2015-04-05 96 views
0

我有一個打開的子窗體(在窗體內)。子窗體基於查詢幷包含多個記錄。我想要搜索子表單以查找是否有任何記錄的字段值= true。在子窗體中搜索值(vba Access2013)

搜索後最好的建議是使用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 

任何建議或替代解決方案將有所幫助。

回答

0

您將使用記錄集:

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