Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Dec 2000
    Location
    Manassas, Virginia, USA
    Posts
    363
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Append text string to a text file. (Access 2002, SP 3)

    I have a batch file that opens an Access database that is linked to one of our schools and opens a form that carries out updates to a number of the fields before moving on to the next Access database linked to another school on another server. What I would like to do as part of the Error Handler code is append the school name & error message to a text file so that I could look at this file in the morning and see what problems occured at each location that prevented the updates from taking place. I can make the batch file append an error message if a school server is down and the mapped location for a school database can not be completed, but I don't know how to append an error message from code within Access. Can anyone help with the code required to do this?
    Judy Jones, Computer Training Specialist
    Manassas, Virginia

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

    Re: Append text string to a text file. (Access 2002, SP 3)

    Hi Judy,

    You can use the following as starting point:
    <code>
    Sub Append2TextFile()
    Dim f As Integer
    Dim strMessage As String
    ' Substitute correct path and filename
    Const strFile = "C:LogsErrors.log"

    ' Get available file number
    f = FreeFile
    ' Open the text file
    Open strFile For Append As #f
    ' Assemble error message here
    strMessage = "..."
    ' Append the message to the text file
    Print #f, strMessage
    ' Close the text file
    Close #f
    End Sub</code>

  3. #3
    3 Star Lounger
    Join Date
    Dec 2000
    Location
    Manassas, Virginia, USA
    Posts
    363
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Append text string to a text file. (Access 2002, SP 3)

    Thanks, although I don't understand exactly how it works, it does exactly what I needed. I tested the code so that it kicked off immediately after the queries were finished and when it worked like I wanted it to, I moved it to the ErrHandler part of the code.


    Private Sub Form_Open(Cancel As Integer)
    Dim f As Integer
    Dim strMessage As String
    ' Substitute correct path and filename
    Const strFile = "J:BatchASTU-UpdateErrors.txt"

    DoCmd.SetWarnings False
    DoCmd.OpenQuery "qryMakeUpdateTable"
    DoCmd.OpenQuery "qryUpdateAllCodes"
    DoCmd.OpenQuery "qryUpdatePstNum"
    DoCmd.SetWarnings True
    Quit


    ErrHandler:
    ' Get available file number
    f = FreeFile
    ' Open the text file
    Open strFile For Append As #f
    ' Assemble error message here
    strMessage = Date & " - " & Me.School.Column(1) & " Error " & Err & " with description: " & Err.Description & " occurred."
    ' Append the message to the text file
    Print #f, strMessage
    ' Close the text file
    Close #f
    Quit
    End Sub
    Judy Jones, Computer Training Specialist
    Manassas, Virginia

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

    Re: Append text string to a text file. (Access 2002, SP 3)

    The Open, Print and Close instructions are part of the original core of the BASIC programming language. Together with instructions such as Input, Line Input and Write, they form a set of tools for manipulating text files. Click in the word Open or Print (in the VB Editor) and press F1 to get help.

Posting Permissions

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