Solved

App.previnstance and command$

Posted on 1998-10-10
3
435 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

773 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