Im trying to get a log system working in an application in MS Access 2016 whenever any user make any action such as login, editing etc.
So far the code I wrote is fairly simple by using open statement,
Public Sub WriteLog(ByVal strContent As String)
fileLog = FreeFile
Open "D:/log.txt" For Output As fileLog
Print #fileLog, strContent
Close #fileLog
End Sub
This is not good because I intent to write the log file in a shared network, which mean many users may simultaneously open the file to write. This will definitely throw error. I thought of doing some queuing to write to the file but havent come to any solution. Is it just impossible to do this?
Edited:
Recursively check if file open and write to file after the file is closed, a way to somehow 'queue' to write to a file. Maybe need to add some code to make sure a limit to recursively execute this function.
Function AvailableToWrite()
' Test to see if the file is open.
If IsFileOpen("D:\log.txt") Then
AvailableToWrite = IsFileOpen() ' Recursively check until file is closed
Else
AvailableToWrite = True
End If
End Function
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next
filenum = FreeFile()
' Attempt to open the file and lock it.
Open filename For Input Write As #filenum
Close filenum
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
End Select
End Function