AOB
Registered User.
- Local time
- Today, 18:21
- Joined
- Sep 26, 2012
- Messages
- 621
Hi guys,
I have a function to write some data to a text file (as an activity log, separate to the live data contained in the DB)
It uses the FileSystemObject.OpenTextFile method and works fine
However, I'm conscious that, although perhaps unlikely, there is the possibility (which is enough for me to worry about it) that multiple users could trigger the function simultaneously, which could cause a conflict when multiple FE's try to write to the same log file at the same time.
So I'm trying to add some code to prevent this from happening, by checking if write-access is available before proceeding.
Below is the piece of code I've added (as yet untested) - just wondering if anybody has any opinion on this method? Is there a better way?
Was considering, as an alternative, looping until the objFile is no longer Nothing, rather than depending on the Err.Number?
I have a function to write some data to a text file (as an activity log, separate to the live data contained in the DB)
It uses the FileSystemObject.OpenTextFile method and works fine
However, I'm conscious that, although perhaps unlikely, there is the possibility (which is enough for me to worry about it) that multiple users could trigger the function simultaneously, which could cause a conflict when multiple FE's try to write to the same log file at the same time.
So I'm trying to add some code to prevent this from happening, by checking if write-access is available before proceeding.
Below is the piece of code I've added (as yet untested) - just wondering if anybody has any opinion on this method? Is there a better way?
Was considering, as an alternative, looping until the objFile is no longer Nothing, rather than depending on the Err.Number?
Code:
Dim objFSO As Object
Dim objFile As Object
On Error GoTo ErrorHandler
...
Set objFSO = CreateObject("Scripting.FileSystemObject")
...
On Error Resume Next
Err.Clear
Set objFile = objFSO.OpenTextFile(objFolder.Path & "\" & strLogName & ".txt", IIf(blnNewLog, 2, 8), True)
If Not Err.Number = 0 Then
datTimer = Timer
Do Until Err.Number = 0 Or Timer > DateAdd("s", 60, datTimer)
Err.Clear
Pause (1) [COLOR=seagreen]' Separate function; does what it says on the tin...[/COLOR]
Set objFile = objFSO.OpenTextFile(objFolder.Path & "\" & strLogName & ".txt", IIf(blnNewLog, 2, 8), True)
Loop
End If
On Error GoTo ErrorHandler
If Not Err.Number = 0 Then
Err.Raise vbObjectError + 1000, , "Error writing to " & strLogName & "log (write-access timeout)"
End If
....