Solved

form exit routine In CONTROLBOX exit

Posted on 2000-05-05
5
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 70

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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