Saturday 29 November 2008

Change An Access Database Password

This code will show you how to change an access database password from Visual Basic using code.

Preparations
Add reference to DAO:
from the menu choose Project-> References, mark the Microsoft DAO 3.6 (or 3.51) Object Library check box, and press OK.

Add 1 Command Button to your form.
Press the button to change the database password.

If you got "Unrecognized Database format" error message:

If you using database that made in Access 2000 and you don't have Microsoft DAO 3.6 Object Library reference, click on browse, and select the file C:\Program Files\Common Files\Microsoft Shared\Dao\dao360.dll (If you have Access 2000 installed in your computer you have this file.)
This will add Microsoft DAO 3.6 Object Library reference to your project. Now mark it and press OK.

Form Code

Private Sub ChangeAccessPassword(OldPass As String, NewPass As String)
Dim Db As Database
' opens the databse, using the old password.
' replace "C:\MyDir\Mydb1.mdb" with your database file name
Set Db = OpenDatabase("C:\MyDir\Mydb1.mdb", True, False, ";pwd=" & OldPass)
' setthe new password
Db.NewPassword OldPass, NewPass
' close the database
Db.Close
End Sub

Private Sub Command1_Click()
' replace "oldPassword" with the current database password, and
' "newPassword" with the new password you want the database will have.
Call ChangeAccessPassword("oldPassword", "newPassword")
End Sub

Source : cuinl.tripod.com

Show Code ....

List All The Tables In Database (DAO)

Preparations
Add 1 List Box to your form.

Add reference to DAO:
from the menu choose Project-> References, mark the Microsoft DAO 3.6 Object Library check box, and press OK.
If you have the Microsoft DAO 3.6 Object Library reference, you can skip the text below and start copying and pasting the code to your form.

If you have only Microsoft DAO Object Library 3.51 reference or earlier, and the database you want to export made in Access 97 or earlier, You can use it instead of the 3.6 version.

Otherwise (if you using database that made in Access 2000 and you don't have Microsoft DAO 3.6 Object Library reference), click on browse, and select the file C:\Program Files\Common Files\Microsoft Shared\Dao\dao360.dll (If you have Access 2000 installed in your computer you have this file.)
This will add Microsoft DAO 3.6 Object Library reference to your project. Now mark it and press OK.


Form Code

Option Explicit
Private Sub Form_Load()
Dim db As Database
Dim qdef As QueryDef
Dim td As TableDef
Dim dbname As String
' Open the database. replace "c:\DBfile.mdb" with your
' database file name

Set db = OpenDatabase("c:\DBfile.mdb")
' List the table names.
For Each td In db.TableDefs
' if you want to display also the system tables, replace the line
' below with: List1.AddItem td.Name
If td.Attributes = 0 Then List1.AddItem td.Name
Next td
db.Close
End Sub

Source : cuinl.tripod.com

Show Code ....

Compact An Access Database From VB

This code will show you how to compact an access database (As you can do from Microsoft Access Application) from Visual Basic using code.

Preparations
Add reference to DAO:
from the menu choose Project-> References, mark the Microsoft DAO 3.6 (or 3.51) Object Library check box, and press OK.

Add 1 Command Button to your form.
Press the button to compact the database.

If you got "Unrecognized Database format" error message:

If you using database that made in Access 2000 and you don't have Microsoft DAO 3.6 Object Library reference, click on browse, and select the file C:\Program Files\Common Files\Microsoft Shared\Dao\dao360.dll (If you have Access 2000 installed in your computer you have this file.)
This will add Microsoft DAO 3.6 Object Library reference to your project. Now mark it and press OK.


Form Code:

Private Sub Command1_Click()
' this line will compact the "c:\myDir\db1.mdb" database to "c:\myDir\db2.mdb".
' after this line had been called you will have the original
' uncompacted database in "c:\myDir\db1.mdb"
' and the new compacted database in "c:\myDir\db2.mdb".
DBEngine.CompactDatabase "c:\myDir\db1.mdb", "c:\myDir\db2.mdb"
End Sub

Source : cuinl.tripod.com

Show Code ....

How To Print MS Access Report

Preparations
Add 1 Command Button to your form.
Add 1 reference to Microsoft Access X.0 Object Library (From
VB Menu choose Project -> References..., mark the Microsoft Access X.0 Object Library check box and press OK).

Form Code :
 
Private Sub Command1_Click()
Dim ac As Access.Application
Set ac = New Access.Application
' open the database.
' replace the "c:\myDir\myDBFileName.mdb" below with your
' database file name
ac.OpenCurrentDatabase ("c:\myDir\myDBFileName.mdb")
' uncomment the line below if you want to see Print Preview
' ac.Visible = True
' replace the acViewNormal below with acViewPreview
' if you want to see Print Preview
ac.DoCmd.OpenReport "Catalog", acViewNormal
' delete the line below if you want to see Print Preview
ac.CloseCurrentDatabase
End Sub

Source : cuinl.tripod.com

Show Code ....

"Unrecognized Database Format" Error Message

If you got "Uncrecognized Database" Format" error message,
it's probably because the access database version that you work with
isn't compatible with your DAO reference.

To add the compatible reference to DAO:
from VB menu choose Project-> References, mark the Microsoft DAO 3.6 Object Library
check box, and press OK.

If you Don't have Microsoft DAO 3.6 Object Library reference, click on browse,
and select the file C:\Program Files\Common Files\Microsoft Shared\Dao\dao360.dll
(If you have Access 2000 installed in your computer you have this file.)
This will add Microsoft DAO 3.6 Object Library reference to your project.
Now mark it and press OK.

Source : cuinl.tripod.com Show Code ....