Results 1 to 12 of 12
  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Crashing Code (XP/2K)

    The code is far too long to examine in detail without being able to see it in action. Try to single step through the code and find out where it crashes.

    One thing that immediately catches the eye is that you have no error handling and that the instance of Word that you start isn't being closed, so that each time you run this code, you will leave an instance of Word in memory. Also, you don't release object memory

    Use the following skeleton:

    Private Sub Command42_Click()
    ...
    Dim mobjWordApp As Word.Application
    ...

    On Error Goto ErrHandler

    Set mobjWordApp = New Word.Application

    ...

    ExitHandler:
    On Error Resume Next
    ' Quit Word
    mobjWordApp.Quit SaveChanges:=wdDoNotSaveChanges
    ' Release object memory
    Set mobjWordApp = Nothing
    Exit Sub

    ErrHandler:
    ' Inform user
    MsgBox Err.Description, vbExclamation
    ' Go to cleaning up part
    Resume ExitHandler
    End Sub

  2. #2
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Crashing Code (XP/2K)

    <P ID="edit" class=small>(Edited by HansV on 29-Jul-03 13:44. Moved long code fragment to attachment.)</P>I have the following code which is not only not working but crashing Access every time I try to run it. I have tried commenting out various parts, and it still crashes access completely. Any ideas what I am doing wrong here?
    It is meant to loop through the record on a form, put the info from each record into a word doc, and when the "intcemetary" changes put a footer and header in. At the end it goes back to the beginning and adds a header to the whole doc and saves it s a DOS text file.

    Any help would be greatly appreciated

    Thanks
    Attached Files Attached Files

  3. #3
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Crashing Code (XP/2K)

    Sorry. I stripped the error handling out, so it would be shorter. I also took out the closing of word.
    When I try to single step It gets hung up that I cannot go to the next record on the form since I am in the VB window and the form is not active, so I cannot watch the code debug. Does that make sense?

  4. #4
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Crashing Code (XP/2K)

    Is there a reason for doing this behind a form? You would find it easier to use either DAO or ADO to step through the recordset and create your text document. However if you are truly saving the document as a text document, you will loose your header and footer - those are preserved only with Word or RTF documents.
    Wendell

  5. #5
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Crashing Code (XP/2K)

    I have thought about that however this was easier at the time. Do you think switching to a recordset would solve my problem?
    Could I just bring up the form and then step through the recordset using me. recordsetclone?
    If it is off a form I can have the human sitting there fill in any missing info as it runs.
    BTW the header and footer are not actual Word headers, but a line of info for a dos based program that has to read this file and translate it for a main frame.

  6. #6
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Crashing Code (XP/2K)

    Thanks Hans. I have to leave the office now. I will continue trying later.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Crashing Code (XP/2K)

    No, you can't single-step through a DoCmd.GoToRecord, I should have seen that. But if you get that far, apparently the code can process one record.
    What happens if you set a breakpoint in the first "executable" line after Loop Until ...? Do you get to the breakpoint, or does Access crash before you get there?

  8. #8
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Crashing Code (XP/2K)

    this is where the problem is. The following bit of code works. BUT it misses the last record on the form. Changing the " If intRecCount = 1 Then Exit Do
    to "=0"" causes it to go into an infinite loop and crash Access. How can I get to the last ercord, but not crash?

    Thanks

    Dim intRecCount As Long
    intRecCount = Me.Recordset.RecordCount
    DoCmd.GoToRecord , , acFirst
    do

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Crashing Code (XP/2K)

    There must be something else that interferes with this, for in a simple test looping until intRecCount = 0 works without problems.

    Like Wendell, I recommend using a Recordset object.

    Dim rst As Object
    Set rst = Me.Recordset

    Do While Not rst.EOF
    ' do your stuff here
    ' refer to fields as rst!FieldName
    rst.MoveNext
    Loop

    Set rst = Nothing

  10. #10
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Crashing Code (XP/2K)

    You do realize you're telling it to exit when the count reaches one, not zero, right? That means that when you get to the next to the last record, you're exiting the loop instead of moving to the last record and processing it.

    Anytime you're looping through a collection and not using a For ... Each loop, it's a good idea to start from the end and work backwards. In other words, go to the *last* record in the recordset and work your way back to BOF, which will coincide with intRecCount reaching zero.
    Charlotte

  11. #11
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Jerusalem, Israel
    Posts
    708
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Crashing Code (XP/2K)

    That is eaxactly the problem. If I set it to 0 it crashes if I set it to 1 it works. I cannot figure out why.
    I cannot work backwards because the word document I am generating from this needs to be in a certain order.
    Thanks

  12. #12
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Crashing Code (XP/2K)

    OK, I had forgotten about the Word document. Well the simple answer is that you can't move to the 0th record because that would be past the EOF. If you're using the actual recordcount, then you can only loop from that count to 1, not to zero, since there isn't any current record at the zero position. What I usually do is use RecordCount -1 and then loop to zero.
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •