How to Create a Single Instance VB6 Program
Forcing single instance mode in a Visual Basic 6.0 (VB6) program is important when a second instance of the program will cause undesired effects, such as performing actions twice or causing errors as both instances try to access a single resource. In Visual Basic .NET (VB.NET) there is a "Make single instance application" option, however, in VB6 you have to manually check for any previously running instances when you start the program by using the "App.PrevInstance" method and then shut down the program.
Instructions
-
-
1
Open the VB6 source file in Microsoft Visual Basic 6.0.
-
2
Check if another instance of the application is already running by by adding the following code in the function the program starts with, which is usually "Sub Main":
If App.PrevInstance = True Then
-
-
3
Display a MsgBox that informs the user that an instance of the application is already running and close the program by adding the following code after the "If" statement:
MsgBox "Program already running..."
End
End If
If the function loads a form prior to the "If-else", such as your program starting in "Sub Form_Load()", then add "Unload Me" before the "End" command.
-
4
Save the VB6 file, compile and run the program to enable the single instance code.
-
1
Tips & Warnings
In VB.NET there is no "App.PrevInstance". Instead, click "Project", click "Properties" and check the "Make single instance application" box.