打开报表并将 DAO 记录集附加到它

Posted

技术标签:

【中文标题】打开报表并将 DAO 记录集附加到它【英文标题】:Open a report and attach the DAO Recordset to it 【发布时间】:2022-01-20 11:59:08 【问题描述】:

我有一个用于输入库存交易的表格。我向它添加了一个按钮,当操作员按下它时,会打开一个报告并显示分配给项目的材料。当我按下按钮并且报告未打开时出现此错误。 enter image description here

我添加了一个代码来打开创建 dao 记录集的报告的事件,并且我想将记录集附加到我的报告中。打开报表的代码如下:

 Private Sub Report_Open(Cancel As Integer)
'Create the necessary recordset and connections
    Dim dbs As DAO.Database
    Dim rsInventoryControl As DAO.Recordset
    Dim rsInventoryAssigned As DAO.Recordset
    Dim rsFiltered As DAO.Recordset
    Dim strSQLInventory As String
    Dim strSQLAssigned As String
    Dim strAssignableAmount As String
    Dim lngStockID As Long
    Dim lngInventoryID As Long
    Dim strInventoryNumber As String
    
    
    'get the data from form
    
    lngStockID = Forms("frmInventoryPermission")!StockID
    lngInventoryID = Forms("frmInventoryPermission")!frmInventoryPermissionDetailSubform.Form!cboInventoryID
    
    'set the connection and recordsets
    Set dbs = CurrentDb
    
    
       
     
    
    'Create and run rsInventoryAssigned recordset
    
  strSQLAssigned = "SELECT tblInventoryPermission.Assigned,Sum(tblInventoryPermissionDetail.AssignedQty) AS SumOfAssignedQty, tblInventory.InventoryID, tblStocks.StockID " & _
    "FROM (tblInventoryPermission INNER JOIN tblStocks ON tblInventoryPermission.StockID = tblStocks.StockID) INNER JOIN (tblInventoryPermissionDetail INNER JOIN tblInventory ON tblInventoryPermissionDetail.InventoryID = tblInventory.InventoryID) ON tblInventoryPermission.TransferPermissionID = tblInventoryPermissionDetail.InventoryPermissionID " & _
    "GROUP BY tblInventoryPermission.Assigned, tblInventory.InventoryID, tblStocks.StockID " & _
    "HAVING (((tblInventoryPermission.Assigned)=False));"
    
    Set rsInventoryAssigned = dbs.OpenRecordset(strSQLAssigned)
    
    'find the record based on the information in form
    'Control that Stock and Inventory Id is specified
       rsInventoryAssigned.Filter = "[InventoryID]='" & lngInventoryID & "' AND [StockID]= '" & lngStockID & "'"
    
            
      'set the recordsource of the report to filtered recordsource
      
       
    rsFiltered = rsInventoryAssigned.OpenRecordset
    Me.RecordSource = rsFiltered
            
    
End Sub

【问题讨论】:

请显示打开报告的表单按钮后面的代码。 Access 期望报表或表单的记录源在设计时可用。如果不是这种情况,那么事情就不会顺利进行。答案将 sql 移动到可以在设计时绑定访问的查询。您还可以使用正确的变量和表名创建一个临时查询或表,然后在设计表单时绑定到它。这是一种黑客行为。使用 hack 作为记录源来设计您的表单。之后,您可以删除后面代码的临时查询,表单仍然可以工作,但在 sql 运行之前不要显示表单。 【参考方案1】:

避免任何带有 DAO 记录集的 VBA 代码,只需使用表单控件保存参数化查询,然后将其永久分配到报告的记录源中。参数化后,每次打开报表时查询都会调整。

下面的 SQL 使用 PARAMETERS 子句(在 Access 的 SQL 方言中允许)并在 WHERE 子句中使用参数。另外查询使用表别名来避免重复写出长表名。

SQL (另存为Access查询对象,然后手动将查询分配给报表的RecordSource

PARAMETERS Forms!frmInventoryPermission!frmInventoryPermissionDetailSubform.Form!cboInventoryID TEXT(255),
           Forms!frmInventoryPermission!StockID TEXT(255);
SELECT p.Assigned,
       i.InventoryID, 
       s.StockID,
       SUM(pd.AssignedQty) AS SumOfAssignedQty 
FROM (tblInventoryPermission p
INNER JOIN tblStocks s
   ON p.StockID = s.StockID) 
INNER JOIN (tblInventoryPermissionDetail pd
INNER JOIN tblInventory i
   ON pd.InventoryID = i.InventoryID) 
   ON p.TransferPermissionID = pd.InventoryPermissionID
WHERE p.Assigned = False
  AND i.[InventoryID] = Forms!frmInventoryPermission!frmInventoryPermissionDetailSubform.Form!cboInventoryID 
  AND s.[StockID]= Forms!frmInventoryPermission!StockID
GROUP BY p.Assigned, 
         i.InventoryID, 
         s.StockID;

VBA (放在表单按钮后面并删除整个Report_Open事件)

DoCmd.OpenReport "myreport", acViewPreview

【讨论】:

以上是关于打开报表并将 DAO 记录集附加到它的主要内容,如果未能解决你的问题,请参考以下文章

增加 VB 中打开的 dao 记录集的数量?

记录层次结构并将 Spark log4j 记录器附加到它

将 DAO 记录集转换为断开连接的 ADO 记录集 dbDecimal 问题

访问 DAO 将整个记录插入另一个记录集

尝试从数组创建 DAO 记录集

将 Access 报表的记录源设置为 ADODB 记录集