Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

form exit routine In CONTROLBOX exit

Posted on 2000-05-05
5
Medium Priority
?
393 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

721 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