Solved

Compacting an Access 2007 database with Excel Macro

Posted on 2010-08-25
12
580 Views
Last Modified: 2013-11-27
Hi All,

What I'm trying to do is compacting a access 2007 database with an excel macro. However my problem is that database is password protected and it asked for password as in prompt a password box and when i enter it and it compacts the database.Somehow i'm not able to figure out how to embed this password in the code itself. Can anyone help me in the same. Enclosed is the code.

Also i tried jet method but it won't work since i'm dealing with 2007 and it gives me an error message in that which is class not registered.

My Code...

Saurabh...

Sub compactmydatabse()

Dim x As New Access.Application

 x.CompactRepair "c:\COD\ABC.accdb", "C:\COD\abc1.accdb"

Kill "C:\COD\abc.accdb"

Name "C:\COD\abc1.accdb" As "C:\COD\abc.accdb"



End Sub

Open in new window

0
Comment
Question by:Saurabh Singh Teotia
  • 7
  • 4
12 Comments
 
LVL 17

Expert Comment

by:calacuccia
ID: 33526660
Does this work?

Set appAccess = New Access.Application
appAccess.OpenCurrentDatabase txtTargetDatabase.Value, True, txtPassword.Value & ""
0
 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 33526678
I dont want to open the database i want to compact it without opening..
0
 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 33526701
Also the reason why i dont want to open it because if i come into compact command after that it gives me an error message stating microsoft can't compact and repair the current database.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33526703
Or

DBEngine.CompactDatabase strSource, strDestination, ";pwd=password", , ";pwd=password"
0
 
LVL 84
ID: 33526709
You can use Shell:

Shell Chr(34) & "Full path to msaccess.exe" & Chr(34) & " " & Chr(34) & "Full path to your database" & Chr(34) & " /compact"

You __might__ be able to get by with this:

Shell "msaccess.exe " & Chr(34) & "Full path to your database" & Chr(34) & " /compact"

but that would be platform-dependent. If you're deploying this, take the time to get the correct value of the msaccess.exe executable.
0
 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 33526741
calacuccia,
What is the type for db engine as it gives me an error message that database not recognised.
LSM:-
Let me try that out will get back to you about it.
Saurabh...
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 33526759
Lsm,
One quick observation about the above code, I tried it out it worked without an error message. However this is something weird when i check the properties of the database the file path is exactly still the same as earlier. However as soon i open the access database the file size reduces by 50% any particular reason why its happening this way?
Saurabh...
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33526764
Saurabh, I just understood that :-) Excuses for my ignorance. But can't you add a reference to the Jet library? If it's as stupid as my first post, I'll leave the field and go back to Excel :-)
0
 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 33526772
calacuccia, nope this is not jet library as per reasearch done by me jetlibrary method works perfectly till access 2000 as in till the format .mdb as soon the format becomes accdb it fails. I have been trying all set of methods and i have just not able to compact by this database becuase not is working in 2007... :-(
Saurabh...
0
 
LVL 59

Author Comment

by:Saurabh Singh Teotia
ID: 33526789
LSM,
Even in the shell command it ask for database password, How will i embeded database password in the code itself?
Saurabh...
0
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 0 total points
ID: 33527000
calacuccia,
I was further testing over it and here are my findings that if i modify the code as dbengine one to this it reconises 2007 as well and works perfectly.
Here is the my code which i used..
Saurabh...

Sub compactdatabse()

Dim x As New Access.Application





x.DBENGINE.CompactDatabase "C:\COD\Mad.accdb", "C:\COD\Mad1.accdb", ";pwd=pass", , ";pwd=pass"



Kill "C:\COD\Mad.accdb"

Name "C:\COD\Mad1.accdb" As "C:\COD\Mad.accdb"

End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33527203
Glad you worked it out
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

914 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

22 Experts available now in Live!

Get 1:1 Help Now