5

My VB6 application is backed by a MS Access database. Allowing user to open multiple instances of the application would create conflicts & alter connected database.

Ideal solution would be to focus existing instance if user tries to open new one.

How can I attain this?

Mahendran
  • 2,719
  • 5
  • 28
  • 50
Kushal
  • 3,112
  • 10
  • 50
  • 79

1 Answers1

7

Use App.PrevInstance:

'this code would be in a bas module for start up.'
Private Sub main()
    'Check for previous instance and exit if found.'

    Dim rc As Long

    If App.PrevInstance Then
        rc = MsgBox("Application is already running", vbCritical, App.Title)
        Exit Sub
    Else
        frmMain.Show
    End If

End Sub
Mike Spross
  • 7,999
  • 6
  • 49
  • 75
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • Thanks, that worked perfectly, now is there anyway to set focus to current instance instead of showing error message..? – Kushal Aug 12 '10 at 15:21
  • Yeah, use the `AppActivate` method - pass in whatever's in the title bar of your application. – MusiGenesis Aug 12 '10 at 15:32