473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compact on Open

How would I compact a database on Open using code? I presume I'd have to use
the Autoexec macro but what code do I put in the procedure called by
Autoexec?

Thanks,
Alistair
Nov 12 '05 #1
2 2594
It is not actually possible, since Jet's Compact functionality requires the
database to be closed (which is why even Access closes the current database
when you compact from the menu).
--
MichKa [MS]
NLS Collation/Locale/Keyboard Development
Globalization Infrastructure and Font Technologies

This posting is provided "AS IS" with
no warranties, and confers no rights.
"lestersal" <le*******@cox. net> wrote in message
news:c0KAb.3296 3$Gj2.23357@oke pread01...
How would I compact a database on Open using code? I presume I'd have to use the Autoexec macro but what code do I put in the procedure called by
Autoexec?

Thanks,
Alistair

Nov 12 '05 #2
Here is a couple of snippets of code to compact the date portion of a
database (Linked tables)
Written a long time ago for Access 97 but works OK for A2K
Think you clicked a button on a form

Table Paths contains information on the folder for the various bits of the
database
Table Users was a list of people logged on to the database (irrelevant for a
single user)
It then creates a copy of the Data.Mdb called something like Data.Bak
(Just in case)
Then rename Data.Mdb to Data.MdbOld
Then Compact Data.MdbOld calling Data.Mdb the original name
Then relink the tables

You may be able to adapt this if using a front end back end situation

Phil

Sub CompactData_Cli ck() ' Compact Database

Dim MyDb As Database
Dim PathSet As Recordset
Dim TDf As TableDef
Dim Pathname As String, FileName As String
Dim DataPath As String, OldDataPath As String, BakDataPath As String
Dim fs As Object

On Error GoTo CompactData_Err

' Check only 1 person (Current User) on System
If CheckUsers() > 1 Then Exit Sub ' Other users on system

ReturnValue = CloseForms()

Set MyDb = CurrentDb
Set PathSet = MyDb.OpenRecord set("Paths")
DataPath = PathSet!DataPat h
PathSet.Close
Set PathSet = Nothing
OldDataPath = DataPath & "Old"
BakDataPath = Left$(DataPath, (Len(DataPath) - 3)) & "Bak"

ReturnValue = SysCmd(acSysCmd SetStatus, "Copying Data Files")
If Dir(DataPath) <> "" Then ' Make sure that the data file
exists
Set fs = CreateObject("S cripting.FileSy stemObject")
fs.Copyfile DataPath, BakDataPath, True ' Copy Data to AMM
Services Data.Bak
If Dir(OldDataPath ) <> "" Then
Kill OldDataPath ' Delete Old File
End If
fs.moveFile DataPath, OldDataPath ' Rename Data to AMM
Services Data.MdbOld
Else
MsgBox "Can't find the Data", vbCritical
Exit Sub
End If

ReturnValue = SysCmd(acSysCmd SetStatus, "Compacting Database")
DBEngine.Compac tDatabase OldDataPath, DataPath

For Each TDf In MyDb.TableDefs ' Refresh the links
If Len(TDf.Connect ) > 0 Then
If TDf.Connect > "" Then 'Drive letter for client
database
TDf.RefreshLink
End If
End If
Next TDf

MsgBox "Database Compacted Succesfully", vbInformation

CompactData_Exi t:
ReturnValue = SysCmd(acSysCmd ClearStatus)
Exit Sub

CompactData_Err :
MsgBox Err.Description
MsgBox "You can restore the data from 'AMM Services Data.Bak'",
vbInformation
Resume CompactData_Exi t

End Sub

Function CloseForms()

Dim i As Integer

If Forms.Count > 1 Then ' Other Forms open
For i = 0 To Forms.Count - 1
If Forms(i).Name <> "Archive" And Forms(i).Name <> "Switchboar d"
Then
DoCmd.Close acForm, Forms(i).Name
End If
Next
End If

End Function
Function CheckUsers() As Integer

Dim MyDb As Database
Dim UserSet As Recordset
Dim SQLStg As String

SQLStg = "SELECT Users.UserName, Users.LoggedOn "
SQLStg = SQLStg & "FROM Users;"

Set MyDb = CurrentDb
Set UserSet = MyDb.OpenRecord set(SQLStg)
UserSet.MoveLas t
If UserSet.RecordC ount > 1 Then
MsgBox "There are other users on the system. The must be logged off
before proceeding", vbInformation
CheckUsers = UserSet.RecordC ount
End If

End Function
"lestersal" <le*******@cox. net> wrote in message
news:c0KAb.3296 3$Gj2.23357@oke pread01...
How would I compact a database on Open using code? I presume I'd have to use the Autoexec macro but what code do I put in the procedure called by
Autoexec?

Thanks,
Alistair

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
10402
by: Larry L | last post by:
Access is noted for bloating a database when you add and delete records frequently. I have always had mine set to compact on close, and that works great. Now after everyone's advice I split my database, so the data is in a second (back-end) database with all the tables linked. However, now when I close the database, it compacts the front end,...
3
5559
by: Paolo | last post by:
Hi, I am trying to compact and repair my database, however every time I try it comes up a message saying: Table: "TempMSysAccessObject already exists", whenever I try to look for this table I cannot find it anywhere... Any clues... Already went into the help file and nothing... Also try to copy my database and the same message comes up
2
2448
by: Greg Strong | last post by:
Hello All, I've written code in a test database with test data. Everything seems to be working except compact database in VB code per http://www.mvps.org/access/general/gen0041.htm. The reason I say this is the auto number fields are NOT being reset to zero. I delete the data from tables with action delete queries, then call the compact DB...
1
4811
by: robert demo via AccessMonster.com | last post by:
In my startup routine, I have the following code: s = CLng(FileLen(filespec) / 1000000) If s > 5 Then 'FIRST, BACKUP THE FRONT END If BackupFrontEnd = False Then Exit Function End If
6
2263
by: MLH | last post by:
I just used Tools / Database Utilities / Compact Database in Access 97 for the first time. Unlike Access 2.0, it does not ask me to furnish a filename for it to compact into. It just launched headlong into a process that, after the fact, had the result of overwriting the 44-meg previous size file into an 8-meg file of the same name. Has...
9
1787
by: Jeff | last post by:
This a little strange. The link in a FE database get corrupted when the BE get compacted, and the FE is not even open. I have checked. The data is fine in the BE. The data is fine in the link in the FE before compacting the BE. I shutdown the FE and then compact the BE. Open the FE and the data 'looks' corrupt. In data sheet view the...
43
1884
by: perryche | last post by:
I have searched the site and probably came across dozens of posts... I am somewhat confused about the replies... here is my situation, see if someone can point me to where to go: I have splitted up the DB to FE and BE, I want to autocompact BE when: 1. all users exited FE (or if not out by certain time of the day and it is idle (forgot to...
2
2775
by: Ron | last post by:
Hi All, Using WinXP pro/Access 2000. I have a database that's been used for about 5 months. Transferred lots of data from a dos based program, then the users have been using it for that 5 months. About 180000 records in one of the main files (maybe 25000 new since the dos conversion). Been having some speed issues crop up along the way...
9
3982
by: Ron | last post by:
New discovery. If I take a perfectly good database, and "compact/repair" on it with Access 2000 (seems to be at multiple sites--I've tried it with my system here, at another office on an entirely different network), it damages the file somehow. The user's machine that did the compact/repair can see the file fine. But any networked user...
0
7484
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.