Microsoft KB Archive/170961: Difference between revisions
(importing KB archive) |
m (Text replacement - """ to """) |
||
Line 60: | Line 60: | ||
Microsoft Access 7.0 and 97 use Jet 3.0 and Jet 3.5 MDB files, respectively. For the code below to work correctly, you must have a reference to an appropriate Microsoft DAO Object Library.<br /> | Microsoft Access 7.0 and 97 use Jet 3.0 and Jet 3.5 MDB files, respectively. For the code below to work correctly, you must have a reference to an appropriate Microsoft DAO Object Library.<br /> | ||
<br /> | <br /> | ||
The database password can be changed by using the NewPassword method. The following example changes the password of the MyDB.mdb database from | The database password can be changed by using the NewPassword method. The following example changes the password of the MyDB.mdb database from "MyPassword1" to "MyPassword2":<br /> | ||
<pre class="codesample"> Sub ChangeDBPassword () | <pre class="codesample"> Sub ChangeDBPassword () | ||
Dim Db As Database | Dim Db As Database | ||
Set Db = OpenDatabase( | Set Db = OpenDatabase("C:\My Documents\MyDB.mdb",True, _ | ||
False, | False,";pwd=MyPassword1") | ||
Db.NewPassword | Db.NewPassword "MyPassword1","MyPassword2" | ||
Db.Close | Db.Close | ||
End Sub | End Sub | ||
Line 76: | Line 76: | ||
To remove password protection from a database, use an empty string for the second parameter of the NewPassword method. For example:<br /> | To remove password protection from a database, use an empty string for the second parameter of the NewPassword method. For example:<br /> | ||
<pre class="codesample"> Db.NewPassword | <pre class="codesample"> Db.NewPassword "MyPassword2", "" | ||
</pre> | </pre> | ||
<br /> | <br /> | ||
To assign a database password to a MDB that is currently without a password, use an empty string for the first parameter of the NewPassword method. For example:<br /> | To assign a database password to a MDB that is currently without a password, use an empty string for the first parameter of the NewPassword method. For example:<br /> | ||
<pre class="codesample"> Db.NewPassword | <pre class="codesample"> Db.NewPassword "", "MyPassword1" | ||
</pre> | </pre> | ||
<br /> | <br /> | ||
Line 90: | Line 90: | ||
<pre class="codesample"> Sub CompactDB () | <pre class="codesample"> Sub CompactDB () | ||
' Compact the database, supplying the password | ' Compact the database, supplying the password | ||
DBEngine.CompactDatabase | DBEngine.CompactDatabase "C:\My Documents\MyDB.mdb", _ | ||
"C:\My Documents\Test.MDB", dbLangGeneral, dbVersion30, _ | |||
";pwd=MyPassword1" | |||
End Sub | End Sub | ||
</pre> | </pre> | ||
<br /> | <br /> | ||
The new database has the same password as the old. Here you compact a MDB with a password of | The new database has the same password as the old. Here you compact a MDB with a password of "MyPassword1" and give the new database a password of "MyNewPWD":<br /> | ||
<pre class="codesample"> DBEngine.CompactDatabase | <pre class="codesample"> DBEngine.CompactDatabase "C:\My Documents\Test.mdb", _ | ||
"C:\My Documents\TestII.MDB", dbLangGeneral & ";pwd=MyNewPWD", _ | |||
dbVersion30, | dbVersion30, ";pwd=MyPassword1" | ||
</pre> | </pre> | ||
<br /> | <br /> | ||
With this line, you can remove the database password:<br /> | With this line, you can remove the database password:<br /> | ||
<pre class="codesample"> DBEngine.CompactDatabase | <pre class="codesample"> DBEngine.CompactDatabase "C:\My Documents\TestII.mdb", _ | ||
"C:\My Documents\TestIII.MDB", dbLangGeneral & ";pwd=", _ | |||
dbVersion30, | dbVersion30, ";pwd=MyNewPWD" | ||
</pre> | </pre> | ||
<br /> | <br /> | ||
This line compacts a database without a password and assigns a password of | This line compacts a database without a password and assigns a password of "NewPassword" to the new database:<br /> | ||
<pre class="codesample"> DBEngine.CompactDatabase | <pre class="codesample"> DBEngine.CompactDatabase "C:\My Documents\TestIII.mdb", _ | ||
"C:\My Documents\TestIV.mdb", dbLangGeneral & _ | |||
";pwd=NewPassword", dbVersion30 | |||
</pre> | </pre> | ||
Revision as of 11:05, 21 July 2020
Article ID: 170961
Article Last Modified on 1/20/2007
APPLIES TO
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 4.0 Standard Edition
- Microsoft Visual Basic 4.0 Professional Edition
- Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
- Microsoft Access 95 Standard Edition
- Microsoft Access 97 Standard Edition
This article was previously published under Q170961
SUMMARY
Microsoft Access 7.0 and 97 MDB files can be password protected. This article demonstrates how to change the password associated with a Microsoft Access 7.0 or 97 MDB file.
MORE INFORMATION
Microsoft Access 7.0 and 97 use Jet 3.0 and Jet 3.5 MDB files, respectively. For the code below to work correctly, you must have a reference to an appropriate Microsoft DAO Object Library.
The database password can be changed by using the NewPassword method. The following example changes the password of the MyDB.mdb database from "MyPassword1" to "MyPassword2":
Sub ChangeDBPassword () Dim Db As Database Set Db = OpenDatabase("C:\My Documents\MyDB.mdb",True, _ False,";pwd=MyPassword1") Db.NewPassword "MyPassword1","MyPassword2" Db.Close End Sub
The code above does not handle the possibility of trying to open the database with the wrong password, which would generate an error. Every application that tries to open password protected databases should be able to handle this scenario.
To remove password protection from a database, use an empty string for the second parameter of the NewPassword method. For example:
Db.NewPassword "MyPassword2", ""
To assign a database password to a MDB that is currently without a password, use an empty string for the first parameter of the NewPassword method. For example:
Db.NewPassword "", "MyPassword1"
You can also change, assign or remove a database password using the CompactDatabase method. When using this method on a database with a password, the Optional arguments must be provided:
Sub CompactDB () ' Compact the database, supplying the password DBEngine.CompactDatabase "C:\My Documents\MyDB.mdb", _ "C:\My Documents\Test.MDB", dbLangGeneral, dbVersion30, _ ";pwd=MyPassword1" End Sub
The new database has the same password as the old. Here you compact a MDB with a password of "MyPassword1" and give the new database a password of "MyNewPWD":
DBEngine.CompactDatabase "C:\My Documents\Test.mdb", _ "C:\My Documents\TestII.MDB", dbLangGeneral & ";pwd=MyNewPWD", _ dbVersion30, ";pwd=MyPassword1"
With this line, you can remove the database password:
DBEngine.CompactDatabase "C:\My Documents\TestII.mdb", _ "C:\My Documents\TestIII.MDB", dbLangGeneral & ";pwd=", _ dbVersion30, ";pwd=MyNewPWD"
This line compacts a database without a password and assigns a password of "NewPassword" to the new database:
DBEngine.CompactDatabase "C:\My Documents\TestIII.mdb", _ "C:\My Documents\TestIV.mdb", dbLangGeneral & _ ";pwd=NewPassword", dbVersion30
Keywords: kb32bitonly kbhowto KB170961