One instance

Is there a way to ensure that only one instance of my VB6 application is running on a computer at one time?  Or, is there a way to lock a database so that only one application can read or write to it at one time?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

tommy_boyConnect With a Mentor Commented:
Detecting Previous Instances

Function AnotherInstance () As Integer
    Dim AppTitle$
    If App.PrevInstance Then
        ' Hold the title of the application (title bar caption)
        AppTitle$ = App.Title  
        ' Change our application title
        App.Title = "No longer want this app running..."
        ' Activate the previous instance
        AppActivate AppTitle$  
        ' Let calling procedure know another instance was detected
        AnotherInstance = True  
        ' Let calling procedure know another instance was NOT detected
        AnotherInstance = False
    End If
End Function

Sub Form_Load ()
    ' Don't want two copies of the program running at the same time
    If AnotherInstance() Then End
    ' Note: that this routine will not work if the application's title changes
    ' (showing file names in the title bar for example).
End Sub
in the Form_Load:

If App.PrevInstance then END

..and that is what is in my code mark2150
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

You do it in a function call and a dozen lines of code. I do it in one line.

Which is clearer in intent?
Which is less compiled bytes?
Which is faster to execute?
Which has fewer lines to debug?
Which took less time (read cost) to code?

Professional coding is price driven. I'm not going to pay one of my team to write like that when the one line version is simpler and clearer.

Had a guy write something like this:

TempTxt = Textbox.Text
If Len(Trim(TempTxt)) = 0 Then
    Str1 = Str1 & " This is a prompt: " & Trim(TempText) & VbCrLf
    Str1 = Str1 & " This is a prompt: " & VbCrLf
End If

Now if you look at that you'll find it'll simplify to:

Str1 = Str1 & " This is a prompt: " & Trim(TextBox.Text) & VbCrLf

Five fewer lines, one less working variable faster execution, much clearer intent. Need I say that this person is no longer working for us...


so mark2150, no validation, no activeating pervious instance, yeah, that is good.
Depends on if you want that or no. Certainly there are times that is good and wanted, but Q wasn't how to bring up alternate, but how to prevent any additional copies from running.

Your change to titlebar will be followed almost instantly by END so user will *NEVER SEE IT*. Ergo: What is point?

Activating alternate may or may not work as you pointed out yourself!

Finally, what is point of coding function call that is only going to be used *once*? Good programming practice says that "single call" functions or subs should be *inlined* for speed and clarity.

rosscAuthor Commented:
Thanks for the help.
All Courses

From novice to tech pro — start learning today.