Solved

form exit routine In CONTROLBOX exit

Posted on 2000-05-05
5
321 Views
Last Modified: 2013-12-25
To All,
From my toolbar "EXIT" option.
I run the following.

' close all recordsets and databases
DBClose All
' Unload form
UnloadMe
' Compact and Repair databases
CompactDb ("c:\folder\data.mdb")
RepairAndOpen ("c:\folder\data.mdb")
MsgBox "Project Reset"
End

I also want to run this routine from the ControlBox the Main form if someone chooses the "X"
I believe it is the
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Is it just a matter of placing this same code in this event?
The code doesNOT run the compact and repair routines when I run this in this Event.
Thanks
fordraiders


0
Comment
Question by:fordraiders
5 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2781708
Yes you should place it in Form_Unload or Form_QueryUnload.

These 2 events are triggered when the form is to be closed.

If you use the QueryUnload event, you have a parameter (UnloadMode) that says what is asking your application closing (your user or Windows).
0
 
LVL 3

Author Comment

by:fordraiders
ID: 2781782
Emoreau,
This routine is from the(Compact and repair) msjet 3.5 programmers guide.

When I have 0 records in the database and I close the form.
This event will not fire from anywhere.

The error message says"
This database ( data.mdb) cannot be opened exclusively. The database may have been opened by another user.

I thought I taking care of this closing all recordsets and database's in DBCloseAll.


Now if I have records in the database it works fine.
It does not work with O records.
Thanks
fordraiders



0
 
LVL 14

Expert Comment

by:wsh2
ID: 2781981
As to the zero count problem, why not just do a SQL COUNT test prior to closing?

-------------------------------------
The unloadmode argument returns the following values:

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.
0
 

Accepted Solution

by:
jwilson earned 50 total points
ID: 2782003
Part of the problem is the global references to the database objects...


Where are these statements?
  Dim DB as Database
  Set DB = OpenDatabase()

If Dim DB is in a module, but Set DB is in a form, each instance of the form will open a new connection to the database (possibly without closing the original)

Your database object should be instantiated localled each time it is needed.  But for the sake of getting you past your problem, just do a record count for the RecordSet, and if it's equal to 0 then skip the compact and repair functions...

Example:

Dim cnt as Integer

cnt = 0
If Not rs.EOF Then
  rs.MoveLast
  rs.MoveFirst
  cnt = rs.RecordCount
End If

DBClose All
UnloadMe 'This bit is redundant
If cnt > 0 Then
  CompactDb ("c:\folder\data.mdb")
  RepairAndOpen ("c:\folder\data.mdb")
End If

MsgBox "Project Reset"
End 'If this is the last loaded form,
    'then this is also redundant

Comments:
If you remove the End statement and the program does not end, then the global object references are causing the problem....



0
 
LVL 3

Author Comment

by:fordraiders
ID: 2782248
Thanks
fordraiders
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to the message 13 63
Excel Formula to Pull Data from Text String 7 68
MsgBox 2 38
Recommendation vb6 to vb.net or others 14 44
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now