【发布时间】: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 运行之前不要显示表单。