检查表中的字段中是否存在某些数据

时间:2015-03-03 21:46:30

标签: access-vba

我有一个表(tblForms),其中一个字段是对另一个表(tblClients)的查找。如何查找某个客户端是否有数据或tblForms中没有数据? DCount仅在客户端出现在tblForms中时才有效。

我有一个带有命令按钮的表单(frmDisclosure) - onClick:

Private Sub Command245_Click()
On Error GoTo Command245_Click_Err

DoCmd.OpenForm "frmClient", acNormal, "", "[ClientID]= " & Me.Client, , acNormal
DoCmd.Close acForm, "frmDisclosure"

Command245_Click_Exit:
Exit Sub

Command245_Click_Err:
MsgBox Error$
Resume Command245_Click_Exit

End Sub

当我点击这个时我得到了错误(N.B.我直接打开frmClient形成Switchboard我没有得到错误)。 frmClient有一个子表单(连续的)frmFormsList,它从以下数据派生出来:

SELECT tblForms.ClientLookup, tblForms.Issued, First(tblForms.RefNo) AS FirstOfRefNo, Last(tblForms.RefNo) AS LastOfRefNo, Count(tblForms.RefNo) AS CountOfRefNo, tblClient.KnownAs, tblClient.EMail
FROM tblForms INNER JOIN tblClient ON tblForms.ClientLookup = tblClient.ClientID
GROUP BY tblForms.ClientLookup, tblForms.Issued, tblClient.KnownAs, tblClient.EMail
HAVING (((tblForms.Issued) Is Not Null));

此函数位于frmFormsList:

Public Function NumRecs() As Integer

NumRecs = DCount("*", "tblForms", "ClientLookup = " & Me.ClientLookup)

End Function

我的查询显示我向客户发布表单的数据。因此,如果我没有向客户端发出表单,则查询不会显示该客户端,因此不会给出结果0.我得到运行时错误2427"您输入的ans表达式没有值"。 NumRecs = DCount(" *"," tblForms"," ClientLookup ="& Me.ClientLookup)在调试中突出显示。

在frm披露中,如果我退出",acNormal,""," [ClientID] =" &安培; Me.Client,acNormal"问题没有发生,但是我没有直接找到我感兴趣的客户。所以当我尝试使用没有发布客户端的Rem&#39d out位打开表单时会出现问题任何形式。当我直接操作frm客户端时,所提供的rocord没有发布表格,但问题不会发生。

1 个答案:

答案 0 :(得分:0)

这是解决方案:     公共函数NumRecs()As Integer

Dim dbs As DAO.Database
Dim rs As Object

Set dbs = CurrentDb
Set rs = dbs.OpenRecordset("qryDisclosure", dbOpenDynaset)

If Me.Recordset.RecordCount = 0 Then
    NumRecs = 0
Else
    NumRecs = Nz(DCount("*", "qryDisclosure", "ClientLookup = " &   Me.ClientLookup), 0)
End If

结束功能