VBA Recordset说有数据,但me.recordsource说它是空的

时间:2017-05-17 17:24:58

标签: vba ms-access

目前我正在处理访问应用程序的新报告。我用来填充报告的RecordSource的SQL命令在SQL / Server中提取数据,当我使用记录集运行数据时它也会提取数据;但是RecordSource说没有数据。

我的问题是,在设置为Report的RecordSource时,我正在做什么/不做会导致SQL命令不返回数据?

Private Sub Report_Open(Cancel As Integer)


gs = "SELECT dbo.tblSubGrant.intAutoRecNum "
gs = gs & ", dbo.tblSubGrantInstitutionContact.intSubGrantID "
gs = gs & ", dbo.tblSubGrant.vcEncumbranceNumber "
gs = gs & ", dbo.tblSubGrant.intFiscalYearID "
gs = gs & ", dbo.v_InstitutionAllType.exprInstitutionName "
gs = gs & ", dbo.tblGrantAwardType.vcGrantAwardTypeDescription "
gs = gs & ", dbo.tblSubGrantInstitutionContactType.intSubGrantInstitutionContactTypeID "
gs = gs & ", dbo.tblSubGrantInstitutionContactType.vcSubGrantInstitutionContactTypeDescription "
gs = gs & ", aSubGrantListAmt.ExprAmtAward "
gs = gs & ", CASE WHEN dbo.tblSubGrant.bitHoldClaim = 1 THEN 'OnHold' WHEN dbo.tblSubGrant.bitClosed = 1 THEN 'Closed' ELSE 'OPEN' END AS Status "
gs = gs & ", dbo.tblSubGrantInstitutionContact.intInstitutionEmployeeID "
gs = gs & "FROM dbo.tblSubGrant WITH(NOLOCK) "
gs = gs & "INNER JOIN dbo.tblSubGrantInstitutionContact WITH(NOLOCK) "
gs = gs & "ON dbo.tblSubGrant.intSubGrantID = dbo.tblSubGrantInstitutionContact.intSubGrantID "
gs = gs & "INNER JOIN dbo.tblGrantAwardAwardTypeAllocation WITH(NOLOCK) "
gs = gs & "ON dbo.tblSubGrant.intGrantAwardAwardTypeAllocationID = dbo.tblGrantAwardAwardTypeAllocation.intGrantAwardAwardTypeAllocationID "
gs = gs & "INNER JOIN dbo.tblGrantAwardType WITH(NOLOCK) "
gs = gs & "ON dbo.tblGrantAwardAwardTypeAllocation.intGrantAwardTypeID = dbo.tblGrantAwardType.intGrantAwardTypeID "
gs = gs & "INNER JOIN dbo.tblSubGrantInstitutionContactType WITH(NOLOCK) "
gs = gs & "ON dbo.tblSubGrantInstitutionContact.intSubGrantInstitutionContactTypeID = dbo.tblSubGrantInstitutionContactType.intSubGrantInstitutionContactTypeID "
gs = gs & "LEFT OUTER JOIN dbo.v_InstitutionAllType WITH(NOLOCK) "
gs = gs & "ON dbo.tblSubGrant.intInstitutionRecipientID = dbo.v_InstitutionAllType.ID "
gs = gs & "LEFT OUTER JOIN "
gs = gs & "(SELECT SUM (dbo.v_frmSubGrantListAmt.exprOrig) + SUM(dbo.v_frmSubGrantListAmt.exprSup) - SUM(dbo.v_frmSubGrantListAmt.exprRed) - SUM(dbo.v_frmSubGrantListAmt.exprLiq)  AS ExprAmtAward "
gs = gs & ", intSubGrantID "
gs = gs & "FROM v_frmSubGrantListAmt WITH(NOLOCK) "
gs = gs & "GROUP BY intSubGrantID) aSubGrantListAmt "
gs = gs & "ON dbo.tblSubGrant.intSubGrantID = aSubGrantListAmt.intSubGrantID "
gs = gs & "GROUP BY dbo.tblSubGrant.intAutoRecNum "
gs = gs & ", dbo.tblSubGrantInstitutionContact.intSubGrantID "
gs = gs & ", dbo.tblSubGrant.intFiscalYearID "
gs = gs & ", dbo.v_InstitutionAllType.exprInstitutionName "
gs = gs & ", dbo.tblGrantAwardType.vcGrantAwardTypeDescription "
gs = gs & ", dbo.tblSubGrantInstitutionContactType.intSubGrantInstitutionContactTypeID "
gs = gs & ", dbo.tblSubGrantInstitutionContactType.vcSubGrantInstitutionContactTypeDescription "
gs = gs & ", dbo.tblSubGrant.vcEncumbranceNumber "
gs = gs & ", dbo.tblSubGrantInstitutionContact.intInstitutionEmployeeID "
gs = gs & ", CASE WHEN bitHoldClaim = 1 THEN 'OnHold' WHEN bitClosed = 1 THEN 'Closed' ELSE 'OPEN' END "
gs = gs & ", dbo.tblSubGrant.intInstitutionRecipientID "
gs = gs & ", dbo.tblSubGrantInstitutionContact.intSubGrantInstitutionContactTypeID "
gs = gs & ", aSubGrantListAmt.ExprAmtAward "
gs = gs & "HAVING dbo.tblSubGrantInstitutionContact.intInstitutionEmployeeID = " & Forms!frmBULKContactUpdate!intInstitutionEmployeeID & " "
gs = gs & "AND (CASE WHEN dbo.tblSubGrant.bitHoldClaim = 1 THEN 'OnHold' WHEN dbo.tblSubGrant.bitClosed = 1 THEN 'Closed' ELSE 'OPEN' END = 'OPEN') "
gs = gs & "AND dbo.tblSubGrantInstitutionContactType.intSubGrantInstitutionContactTypeID = " & Forms!frmBULKContactUpdate!intSubGrantInstitutionContactTypeID & " "
gs = gs & "ORDER BY 5, 3 "


Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset

rst.Open gs, cnn, adOpenStatic, adLockReadOnly

Me.RecordSource = gs
DoEvents

rst.Close
cnn.Close

Set rst = Nothing
Set cnn = Nothing

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。由于我不再直接在报告属性中将SQL命令设置为RecordSource,因此我忘记从调用DoCmd.OpenReport gstrDocName,acViewPreview中删除Criteria部分。它试图在SQL命令中添加WHERE子句。