将手动生成的RecordSet传递给SubReport

时间:2013-01-25 10:24:26

标签: ms-access

在Microsoft Access 2007中,是否可以手动生成RecordSet对象并将对象传递给Microsoft Access报告和子报告?

我希望有一个表单,用户单击一个按钮,OnClick事件动态创建两个RecordSet,打开一个Report并将第一个RecordSet附加到Report,第二个RecordSet附加到该Report上的SubReport。然后生成/显示报告。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以将表单或报表的记录源设置为SQL字符串。您还可以使用OpenForm或OpenReport的WHERE参数,根据现有查询或表筛选表单或报表。最后,您可以将表单的记录集设置为现有的Recordset,但这稍微复杂一点,只需将Record Source设置为sql。

  sSQL = "SELECT [" & Me.AField & "] FROM [" & Me.ATable & "] " _
       & "WHERE [" & MeAnotherField & "] " & Me.Operator & Me.Value

  Forms!AnotherForm.Recordsource = sSQL

已断开连接的记录集

Private Sub Form_Load()
Set rs = New ADODB.Recordset ''CreateObject("ADODB.RECORDSET")

 With rs
    'Set .ActiveConnection = cn
    .LockType = adLockOptimistic
    .CursorType = adOpenKeyset
 End With

'Create arrays from the lists
asL1 = Split("a,b,c,", ",")
asL2 = Split("b,e,c,d", ",")

'Create a disconnected recordset
rs.Fields.Append "Srt", adVarChar, 25
rs.Fields.Append "L1", adVarChar, 25
rs.Fields.Append "L2", adVarChar, 25

rs.Open

'Add list 1 to the recordset
For i = 0 To UBound(asL1)
    rs.AddNew Array("Srt", "L1"), Array(asL1(i), asL1(i))
    rs.Update
Next

rs.Sort = "Srt"

Set Me.Recordset = rs
Set rs = Nothing
Set cn = Nothing
End Sub