by VB FAQ (6 Submissions)
Category: Windows System Services
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (3 Votes)
How do I prevent multiple instances of my program? In VB 3 and above, the property App.PrevInstance is set to True if an older instance of the program already exist.
Assumes
As Robert Knienider([email protected]) informed me, this piece of code will not work for non-English versions of Mirosoft Windows where the word for "Restore" does not have "R" as the underlined word. Replace the "R" in the SendKeys line above with "{ENTER}" or "~".
Note that you shouldn't prevent multiple instances of your application unless you have a good reason to do so, since this is a very useful feature in MS Windows. Windows will only load the code and dynamic link code once, so it (normally) uses much less memory for the later instances than the first.
Sub Form_Load ()
If App.PrevInstance Then
SaveTitle$ = App.Title
App.Title = "... duplicate instance." 'Pretty, eh?
Form1.Caption = "... duplicate instance."
AppActivate SaveTitle$
SendKeys "% R", True
End
End If
End Sub