Results 1 to 5 of 5
  1. #1
    ino_mart is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    16

    Problem with EOF in custom report

    All

    I have to export an Access table to an Excel-file. The XLS would be some kind of report with a page title, subtitles, subfooters, footers.

    I am currently in debugging state as I can't solve the "No current record" error.

    A very basic example of my code is below. I do know why the error happens, but I need a proper solution how to avoid the error message.

    Code:
    Private Sub Command0_Click()
    Dim OrigPostalCode As Integer
    Dim OrigFullName As String
    Dim OrigStreet As String
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("select * from tblAdres order by postalcode, street, fullname")
    Debug.Print "Overview of adres"
    rs.MoveFirst
    OrigPostalCode = rs!postalcode
    Do While Not rs.EOF And rs!postalcode = OrigPostalCode
        Debug.Print vbTab & "The postal code is " & rs!postalcode
        OrigStreet = rs!street
        Do While Not rs.EOF And rs!postalcode = OrigPostalCode And rs!street = OrigStreet
            Debug.Print vbTab & vbTab & "A street found for this postal code is: " & rs!street
            OrigFullName = rs!FullName
     
            Do While Not rs.EOF And rs!postalcode = OrigPostalCode And rs!street = OrigStreet And rs!FullName = OrigFullName
                Debug.Print vbTab & vbTab & vbTab & "A person living in this street is: " & rs!FullName
                rs.MoveNext
                OrigFullName = rs!FullName
            Loop
            OrigStreet = rs!street
        Loop
        OrigPostalCode = rs!postalcode
    Loop
    End Sub
    I do get my report, but I do not find a proper solution for the error message.


    Code:
    Overview of adres
        The postal code is 1000
            A street found for this postal code is: Shrekstreet
                A person living in this street is: Mike Meyers
            A street found for this postal code is: Suite 750
                A person living in this street is: Lynda Blair
        The postal code is 2000
            A street found for this postal code is: Boulevard
                A person living in this street is: Joe Smith
                A person living in this street is: John X
            A street found for this postal code is: Saint Victor
                A person living in this street is: Julie Andrews

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The fly in the ointment is that you're looping the same recordset more than once. When you loop it the second time (your inner loops), it get's left at EOF, so a line like this will error:

    OrigStreet = rs!street

    Since you have an additional test along with the EOF test, I'm not sure where it's happening, but my suspicion is that one of those loops is leaving the recordset at EOF which is followed by a line like the above. You can set a breakpoint and step through the code, which should make it easier to follow what's happening.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ino_mart is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    16
    Paul

    I already found one error. Next lines needs be switched (otherwise, I do not output the last record)
    Code:
    rs.MoveNext 
    OrigFullName = rs!FullName
    However, it now fails on the While-condition
    Code:
    Do While Not rs.EOF And rs!postalcode = OrigPostalCode And rs!street = OrigStreet And rs!FullName = OrigFullName
    It is actually a same situation where I am checking a variable with a recordfield where the recordset is EOF.

    The code does print all the records in the table. It is just a problem when de recordset is EOF. I had hoped VBA would pass the While-condition as soon it detected the rs was EOF.

    I also tried to put the second part in another DO While-loop, but then my report misses the titles or got in a infinite loop.

    I can't use error handling as there will be more subtitles, footers, indication of amount of records per group, ...

    Regards
    Ino

  4. #4
    ino_mart is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    16
    I found a solution. Seems I had not to work with "Do While", but with IF-functions


    Code:
    Private Sub Knop1_Click()
    
    
    Dim reportmessage As String
    Dim street As String
    Dim postalcode As String
    Dim rs As Recordset
    
        Set rs = CurrentDb.OpenRecordset("select * from tblAdres order by postalcode, street, fullname ")
        
        rs.MoveFirst
        
        Debug.Print "Overview of adres"
        postalcode = ""
        street = ""
        FullName = ""
        Do While Not rs.EOF
            If postalcode <> rs!postalcode Then
                postalcode = rs!postalcode
                reportmessage = vbTab & _
                                "The postal code is " & postalcode
                Debug.Print reportmessage
            End If
            
            If street <> rs!street Then
            
                street = rs!street
                reportmessage = vbTab & vbTab & _
                                "A street found for this postalcode is: " & street
                Debug.Print reportmessage
            
            End If
            
            If FullName <> rs!FullName Then
                FullName = rs!FullName
            reportmessage = vbTab & vbTab & vbTab & _
                            "A person living in this street is: " & rs!FullName
                            Debug.Print reportmessage
            End If
    
            rs.MoveNext
            
        Loop
        
        Set rs = Nothing
    End Sub

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Glad you got it sorted out. I would take issue with this though:

    Quote Originally Posted by ino_mart View Post
    I can't use error handling as there will be more subtitles, footers, indication of amount of records per group, ...
    I can't think of any process that couldn't include proper error handling. Without, the app will just blow up on an mde/accde user.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 10-05-2011, 07:55 PM
  2. Custom error message problem
    By thekruser in forum Programming
    Replies: 10
    Last Post: 10-06-2010, 05:14 PM
  3. Custom Report Printing
    By robocopfl in forum Reports
    Replies: 3
    Last Post: 06-04-2010, 12:23 PM
  4. Custom Query Parameter Prompts-- Problem
    By tylerthompson in forum Forms
    Replies: 11
    Last Post: 07-07-2009, 10:28 PM
  5. Report Problem
    By tlitman09 in forum Reports
    Replies: 0
    Last Post: 02-20-2007, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums