Hi,
I am back again looking for some help.
Never encountered this before but have a new user that opened my database(accde) 10 times, and it made a bit of a mess of the data entry(nothing major and fixable). I would like to prevent this happening again. I have been trialing bits of code all day and can't seem to get anything to work in a satisfactory way. Below is the nearest to working. Works ok and you get a message saying the database is already open, but once you ok the message box, you are left with the database still open twice. The initial instance (displaying the main form) and the second instance with no forms displayed. Once close the second instance the lock file is deleted, which allows the database to be opened again without the warning leaving me with 2 instances open.
Is there a better way? Or should I quit all open instances of the application after ok'ing the message box?
Thanks in advance,
Poco.
I am back again looking for some help.
Never encountered this before but have a new user that opened my database(accde) 10 times, and it made a bit of a mess of the data entry(nothing major and fixable). I would like to prevent this happening again. I have been trialing bits of code all day and can't seem to get anything to work in a satisfactory way. Below is the nearest to working. Works ok and you get a message saying the database is already open, but once you ok the message box, you are left with the database still open twice. The initial instance (displaying the main form) and the second instance with no forms displayed. Once close the second instance the lock file is deleted, which allows the database to be opened again without the warning leaving me with 2 instances open.
Is there a better way? Or should I quit all open instances of the application after ok'ing the message box?
Thanks in advance,
Poco.
Code:
Option Compare Database
Option Explicit
Function IsDatabaseAlreadyOpen() As Boolean
' Check if the lock file exists
Dim lockFilePath As String
lockFilePath = CurrentProject.Path & "\database.lock"
If Dir(lockFilePath) <> "" Then
IsDatabaseAlreadyOpen = True
Else
IsDatabaseAlreadyOpen = False
End If
End Function
Sub CreateLockFile()
' Create the lock file
Dim lockFilePath As String
lockFilePath = CurrentProject.Path & "\database.lock"
Dim lockFileNumber As Integer
lockFileNumber = FreeFile()
Open lockFilePath For Output As lockFileNumber
Close lockFileNumber
End Sub
Sub DeleteLockFile()
' Delete the lock file
Dim lockFilePath As String
lockFilePath = CurrentProject.Path & "\database.lock"
If Dir(lockFilePath) <> "" Then
Kill lockFilePath
End If
End Sub
Private Sub Form_Load()
' Check if the database is already open
If IsDatabaseAlreadyOpen() Then
MsgBox "The database is already open.", vbInformation
DoCmd.Close acForm, Me.Name
Else
' Create the lock file
CreateLockFile
Me.cboPartNumber.Locked = False
Me.cboPartNumber.BackColor = 16777215
Debug.Print "Clearing Image"
Me.Image0.Picture = ""
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
' Delete the lock file when the form is closed
DeleteLockFile
End Sub