Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2004, 06:43 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting to the recordset in report header


In the Access report header event, how can I get to the
report recordset? I need to trawl through the report records
before printing them - I may need to edit/delete some of them
before preview/printing. Can I get to the record set in the report
header event code ?

TIA
 
Old June 15th, 2004, 11:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

While there is no "report header event" per se, you can always modify a reports record source at run-time via its RecordSource property. Probably the best place to do that would be in the reports Open event. I've never used a report module to run action queries (Updates/Deletes) before, but its certainly possible. qryTest is a saved query:

Private Sub Report_Open(Cancel As Integer)
    Dim strSQL As String

    DoCmd.SetWarnings (False) 'turns off system messages

    strSQL = "UPDATE qryTest SET TestText = 'OldText' WHERE TestID > 2;"
    DoCmd.RunSQL strSQL

    strSQL = "DELETE * FROM qryTest WHERE TestID = 1"
    DoCmd.RunSQL strSQL

    DoCmd.SetWarnings (True) 'turns on system messages

    ' set reports recordsource
    Reports!rptTest.RecordSource = "qryTest"

End Sub

Might give you some ideas.

Bob



 
Old June 16th, 2004, 02:09 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the above.

This gets me to the record source but not the recordset.
i want to be able to loop through the records in the record set.

any ideas?
 
Old June 16th, 2004, 11:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

While I'd be inclined to do all this behind a form module, collect dynamic criteria, generate the recordset and then open the report, the ADO is the same. Here is how it might look in your report module:

Private Sub Report_Open(Cancel As Integer)
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim strSQL As String
    Dim strCriteria As String

    Set cnn = CurrentProject.Connection

    strSQL = "SELECT * FROM qryTest"

    Set rst = New ADODB.Recordset
    rst.Open strSQL, cnn, adOpenKeyset, adLockOptimistic

    'DELETE******************************************* *******

    strCriteria = "Test2"

    ' Filter records to be deleted.
    rst.Filter = "TestText = '" & strCriteria & "'"

    ' Delete the records
    If rst.RecordCount > 1 Then
        Do While Not rst.EOF
            rst.Delete
            rst.MoveNext
        Loop
        rst.UpdateBatch
    End If

    rst.Filter = adFilterNone
    rst.Requery

    'UPDATE******************************************* ******

    strCriteria = "Test3"

    ' Updaet records that meet criteria.
    If rst.RecordCount > 1 Then
        Do While Not rst.EOF
            If rst!TestText = strCriteria Then
                With rst
                    !TestText = "Test4"
                    .MoveNext
                End With
            Else
                rst.MoveNext
            End If
        Loop
        rst.UpdateBatch
    End If

     ' Set reports recordsource.
    Reports!rptTest.RecordSource = "qryTest"

    ' clean up
    rst.Close
    Set rst = Nothing
    Exit Sub

ErrorHandler:
    ' clean up
    If Not rst Is Nothing Then
        If rst.State = adStateOpen Then rst.Close
    End If
    Set rst = Nothing

    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If

End Sub

HTH,

Bob







Similar Threads
Thread Thread Starter Forum Replies Last Post
Report Header not printing lryckman Access VBA 3 November 26th, 2007 09:58 AM
Report Header JohnBoyUK BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 October 28th, 2004 07:10 AM
First and Last Record in Report Header cc16 Access VBA 7 September 10th, 2004 09:37 AM
group header in data report nebujacob2000 VB How-To 1 January 12th, 2004 04:41 PM
Report header anees81 Crystal Reports 0 September 25th, 2003 06:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.