Here is my problem. I need a form unload check for my program to prevent accidental closing of a program. I also want only one instance of the program running at a time.
This is how I'm doing such in my main to prevent multi-instances:
CODE
Private Sub Form_Load()
On Error Resume Next
If App.PrevInstance Then
MsgBox ("Only one instance of this program can be running."), vbExclamation, "The requested " & "application is already open"
Unload Form
End
End If
OtherFunctions
RequestToCloseFlag = 1
End Sub
And doing this to check and question the unload query.
CODE
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
' *** universal unload check
If RequestToCloseFlag = 1 Then
Dim strQuestion As String
Dim intAnswer As Integer
Dim aryMode As Variant
aryMode = Array("Program Close?", _
"vbFormCode", "vbAppWindows", _
"vbAppTaskManager", "vbFormMDIForm")
strQuestion = "This program should NEVER be closed!" & vbCrLf & "Are you sure you want to close?"
intAnswer = MsgBox(strQuestion, vbQuestion + vbYesNo, _
aryMode(UnloadMode))
If intAnswer = vbNo Then
Cancel = -1
Exit Sub
End If
LogStatusByte = 4
AppendLogFile
End If
End Sub
The problem is that I get stuck in a loop doing this. If I already have 1 instance running and I open another, the App.PrevInstance triggers, on 'OK' it tries to Form Unload. But this then triggers the Form_QueryUnload which tries to run its code to prevent closing. No problem. That is why I was trying to use the RequestToCloseFlag variable to skip past all of the "Are you sure you want to close" stuff on a newly opened instance.
But it appears that any code execution within the Form_QueryUnload angers the App.PrevInstance, and causes it to just loop the messagebox "Only one instance of this program can be running."
Is there any way to stop the opening of my program with the App.PrevInstance without involving the Unload Form that triggers the Form_QueryUnload?