How to Append Text to a Text File With the Print Statement

Steve R.

Retired
Local time
Today, 14:12
Joined
Jul 5, 2006
Messages
5,610
I'm trying to figure out at what point an error message occurs while a database is closing. This problem only occurs if someone clicks on the red close button on the top right of the screen when there are several forms open. If the user exists normally there is NO problem.

I am attempting to use the print statement to write to a text file to track what is happening as the database shuts down. Unfortunately, the open (print) statement, when issued, zaps any prior contents which precludes having a full printed record. I am hoping for a print syntax that would allow appending.

Code:
Open "ErrorReport.txt" for Output as #1
Appended text to the file to show what is happening in that event.
Close #1

The program, in closing, does stop with an error message that a certain file can't be found. But when I click on debug, the program simply closes. I have also commented out VBA references to the file that can't be found, but the error still persists .
 
Here is some code I used for writing to an ongoing logfile. In this case it was to show recent activity, regardless of which of several databases was being used.

Code:
Function fJLogIT(sActivity As String)
'write a record into an access log file called jAccessLog.Log
'to indicate what files/databases have been used recently
'written Mar 2000
'jed
'Parameter:
'sActivity      a short text about activity in this current database, can be null

Open "E:\Work_DATA_20071207\C_Drive_WORK\Jack\WorkInProgress\jAccessLog.log" For Append As #1

Print #1, Now() & vbTab & CurrentDb.Name & vbCrLf & vbTab & "--->  " & sActivity


' Close before reopening in another mode.
Close #1
End Function
It may give some ideas for syntax etc.

Have you considered not making the "red Close button" visible while forms are open.
Or adjusting the Close button click event procedure to a) Close all forms,
or b) issue a message that Forms are still Open and close is not available at this time...
 
Have you considered not making the "red Close button" visible while forms are open.
Yes, I had. While this may be considered "bad" programing, I tend to leave all the "features" available.

Of course the act of posting this question caused me to rethink the sequence of events when the forms close. Moving the following code from the "CLOSE" event to the "DEACTIVATE" event solved the issue.
Code:
Public Sub Form_Deactivate()
    On Error GoTo ERR_Form_Deactivate
    If CurrentProject.AllForms(csSearchForm).IsLoaded Then Forms(csSearchForm).SetFocus Else DoCmd.OpenForm csSearchForm
    Exit Sub
ERR_Form_Deactivate:
    Resume Next
End Sub
 

Users who are viewing this thread

Back
Top Bottom