Solved

App.previnstance and command$

Posted on 1998-10-10
3
444 Views
Last Modified: 2012-05-04

When my program opens,


if command$ = "" and app.previnstance then
unload
else

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.
0
Comment
Question by:idcjavateam
  • 2
3 Comments
 
LVL 1

Expert Comment

by:vignola
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
firstapptitle=app.title

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

Accepted Solution

by:
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
0
 
LVL 6

Expert Comment

by:clifABB
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

740 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