Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper way to use app.previnstance with form_queryunload 1

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
0
0
US
Im using app.previnstance to end the program if a copy is already running.
In form_load:

Code:
If App.PrevInstance Then Unload Me: Exit Sub

But Im also using form_queryunload in the form, to detect if someone clicks the X (close) on the form.
Im using form_queryunload to detect if any settings on the form have changed and ask user if (s)he wants to save settings.
But when I run the program, and try to run it again, it pops up asking if I want to save changes. Also, even if I click No, the task manager still shows two instances of the app running.

here is the app.previnstance in my form_load:

Code:
Private Sub Form_Load()
If App.PrevInstance Then Unload Me: Exit Sub

Here is my form_queryunload code:

Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'this sub senses the closing of a form.
'next, run sub that checks for changes.
 check_for_changes
'Next, close all open forms in project
 Dim f As Form
  For Each f In Forms
   If f.Name <> Me.Name Then Unload f
  Next
'Finally close the main, or last form that this routine is in
Unload Me
End Sub

What is the proper way to do this?
Thanks.
 
[tt]Private Sub Form_QueryUnload(Cancel As Integer, [!]UnloadMode[/!] As Integer)[/tt]

Take a look at help file for the QueryUnload event.

vbFormControlMenu 0 The user chose the Close command from the Control menu on the form.

vbFormCode 1 The Unload statement is invoked from code.

vbAppWindows 2 The current Microsoft Windows operating environment session is ending.

vbAppTaskManager 3 The Microsoft Windows Task Manager is closing the application.

vbFormMDIForm 4 An MDI child form is closing because the MDI form is closing.

vbFormOwner 5 A form is closing because its owner is closing.

If the user clicks the X, UnloadMode should be vbFormControlMenu. If you unload it from code, UnloadMode should be vbFormCode.

Another way to get around this....

Create a regular module. In the module....

Code:
public sub Main()
  
  If App.PrevInstance Then Exit Sub

  Call YourForm.Show(vbModal)

End Sub

Then, change the startup properties for your project to use Sub Main instead of your form (click Project -> Properties, and look for the Startup Object). Since the form hasn't been loaded yet, you don't need to unload it.


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks George, I inserted your code in the module and it works like a charm!
How do you guys make this stuff look so simple?
Again, thanks!!!
 
How do you guys make this stuff look so simple?

Simple. Been there, done that.

I'm glad it worked out well for you.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
One more question.
Whats the advantage of using

Call YourForm.Show(vbModal)

over just simply

YourForm.Show

?

The latter seems to work fine for me.
 
No advantage. Just habit.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks George, I inserted your code in the module and it works like a charm!
How do you guys make this stuff look so simple?
At the risk of sounding like a jerk, there is a great deal to be gained by reading the documentation once front to back.

There is a ton of information in there. A careful reading one time will help you be aware of these kinds of things. When you need them later you'll have a good idea where to find them later when you need the details.

The actual Microsoft documentation is much more valuable than most of the 3rd party books that have been published. The downside is that you have to make a large effort once. But it will pay off again and again.

But it's a little late in VB6's life cycle to make the investment now.

I'll add that I don't do this myself much with newer technology. It takes more discipline and patience than I seem to have anymore, partly because reference manuals are not written to be read linearly and you have to jump all over the book to digest many topics.
 
OK, my apologies George for taking up your time..
 
We are all jerks at one time or another, but I would not say you were being a jerk.
I would be a jerk if I turned this forum into something less than what it is by arguing, but I would like to say one thing.
Sometimes the help files (for me anyway) can be very confusing and hard to understand. Sometimes I have a hard time finding what I need to know.
Being able to ask someone who already knows helps me immensely, and I am sure whoever helps gets something from it too, I know I feel good when I can help someone. (Which is rare as far as VB6.)
That being said, I do apologize to anyone who may be offended by my stupidity.
I do appreciate the knowledge that is on this forum and I have recommended the forum to several people, for I do think it is a good thing.
I will try to read the help files more thoroughly next time before posting, for I do need to work on my lazy streak. :)
 
For what it's worth....

You don't need to apologize for taking up my time. Heck, you didn't even specifically ask for my time. If I didn't want to be bothered, I wouldn't have responded. I enjoy helping others.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top