App.previnstance and command$

Posted on 1998-10-10
Last Modified: 2012-05-04

When my program opens,

if command$ = "" and app.previnstance then

now, this is the tricky part.
I want to activate my program which is already opened
and pass the value of the command$ line.

Condition: I can not use the forms caption to activate
it as it always displays a filename.

I have VB6.
Question by:idcjavateam
  • 2

Expert Comment

ID: 1439191
put a title "mytitle" to your app and then modify app.title when it displays a filename to app.title=app.title & " " & filename.
Your second instance will have app.title and your previnstance app.title +filename.
Check app.previnstance at first line of code:
if app.previnstance then
dim firstapptitle as string

Search with findwindow(myclassname,firstapptitle)
dim hwnd as long
search again:
hwnd=(classname,firstapptitle + "*.*")
the hwnd to previous instance handle, set focus
with setfocus(hwnd)
to and exit

Accepted Solution

clifABB earned 120 total points
ID: 1439192
Ok, here's how I would do it.

In your app, every time the caption changes, write that caption to the registry using SaveSetting().

When your all starts, check for previous instance and, if true, retrieve the caption from the registry and activate the app that way.

Also, pass the command string using the same method.  When your app is started, and there is a previous instance, write command$ to the registry.  In your app's main form's activate event, check for previous instance.  If false, retrieve command$ from the registry and use it.  Also, erease it immediately after retrieving it.

The code would look something like this:
Private Sub WriteCaption(sFilename As String)
  'This doesn't have to be a sub, but the save setting line
  'must go right after setting the form caption
  frmMain.Caption = sFilename
  SaveSetting "MyApp", "Activate", "Caption", sfilename
End Sub

Sub Main()
  Dim sCaption As String

  If App.PrevInstance Then
    SaveSetting "MyApp", "Activate", "Command", Command$
    sCaption = GetSetting("MyApp", "Activate", "Caption", "")
    AppActivate sCaption
  End If
End Sub

Private Sub Form_Activate ()
  Dim sCommand As String
  sCommand = GetSetting("MyApp", "Activate", "Command", "")
  If sCommand <> "" Then
    'Process Command
  End If
  DeleteSetting "MyApp", "Activate", "Command"
End Sub

Expert Comment

ID: 1439193
Oops, the activate event should test for previous instance.
Chang the code to this:

Private Sub Form_Activate ()
  Dim sCommand As String
  If Not App.PrevInstance Then
    sCommand = GetSetting("MyApp", "Activate", "Command", "")
    If sCommand <> "" Then
      'Process Command
    End If
    DeleteSetting "MyApp", "Activate", "Command"
  End If
End Sub

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 59
Help me. 3 60
xmlhttpPost not sending form or Type=Date (ASP Classic, though this may not make a difference) 4 39
Set WorkSheet  not Working 9 42
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question