Solved

form exit routine In CONTROLBOX exit

Posted on 2000-05-05
5
345 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA open file from excel cell 4 42
Visual Basic Excel Formatting error 4 105
VB6 Compile Compatibility Issue 4 102
VB6 - Scroll Mouse wheel on Picturebox 13 44
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

770 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