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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Forms

Status
Not open for further replies.

mans

Programmer
Mar 18, 2000
136
AU
Hello,

I am using VB6 with Access2000. Can someone please supply code that will close all of the forms for a particular application (App1) from within another application (App2). App2 is used to compact, repair and copy the database that is used by App1, App2 needs to shut down all of the forms in App2 as the form loads before it can perform it's function.

Thank You
 
The best would be to inform the user to close the forms so this can be done. [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Your point is made clearly to the user both in the manual and verbally but in the real world it is not always effective. I really want to avoid them having to go through ctl-alt-del each time App1 is'nt closed down properly.

My users are not highly computer literate.
 

Why isn't the application closing down properly????

You generally close all forms when the application closes:

In the MDI form QueryUndoad event you add this code:


Dim frm As VB.Form

For Each frm In VB.Forms
[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
(Sorry, somehow I clicked the Submit button:)


Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Dim frm As VB.Form
For Each frm In VB.Forms
If Not (TypeOf frm Is MDIForm) Then
Unload frm
End If
Next frm

End Sub


You could also add a menu item with that will run the above code when the user clicks it. Then all the forms except the MDI will close.

If you inform the user, then they can close all the forms with one click, or close the application which will shut down all forms prior to closing.

To just "Kick" a user out, possibly when the user is right in the middle of some serious work, with-out any notice, is not too good of an idea.

If you want to throw them out and end the application, let the application do the shutting down of itself, closing its' own forms. [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
You could make an EXE-Server from your app1 and then you could call a Method to unload your app1. If this would be a solution for you let me know.

Andreas
 
Hi,

Q: Do you want App1 to close down or just the forms (child forms)?

A: Better to close App1 if a connection was already established early at startup. I pasted sample codes that can help you. If you want, however, to close only the child forms (MDI), then ... we may have to look for the proper API for this. [sad]

From AllAPI network,
How can I close an application?

You can always use the FindWindow-function and the PostMessage-function to find the wanted application, and then send it a message that it has to close itself. One disadvantage: you'll need the exact caption of this application.


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10


Private Sub Form_Load()
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
If winHwnd <> 0 Then
PostMessage winHwnd, WM_CLOSE, 0&, 0&
Else
MsgBox &quot;The Calculator is not open.&quot;
End If
End Sub


Use the FindWindow to get the window handle of App1 by passing App1's title (what appears in the task bar) to FindWindow. Once you have the handle, use it to PostMessage passing WM_CLOSE constant (it's like Windows will &quot;click&quot; the close button of App1 for you). Where to place the codes and what follows I leave to you.

[peace]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top