You can avoid this problem when you first create your Access
database. You must make sure that the database password for your Access
database does not end with a backslash character (\). However, if you have
already set the database password for your Access database, and the database
password ends with a backslash character (\), you cannot open your Access
database. To recover your Access database, import the database objects from
your Access database to a newly created Access database. To do this, follow
these steps.
Note The sample code in this article uses Microsoft Data Access
Objects. For this code to run correctly, you must reference the Microsoft DAO
3.6 Object Library. To do so, click
References on the
Tools menu in the Visual Basic Editor, and make sure that the
Microsoft DAO 3.6 Object Library check box is selected.
You must also make sure that no reference to
Microsoft ActiveX Data Objects is selected in the
References
dialog box.
- Start Access.
- Create a new Access database that is named
RecoveredDB.mdb.
- In the Database window, click Modules, and
then click New.
- Type or paste the following sample code in the module
window:
Option Compare Database
Option Explicit
Public Function ImportDb(strPath As String) As Boolean
On Error Resume Next
Dim db As Database 'Database to import.
Dim td As TableDef 'Table definitions in the database.
Dim strTDef As String 'Name of the table or the query to import.
Dim qd As QueryDef 'Query definitions in the database.
Dim doc As Document 'Documents in the database.
Dim strCntName As String 'Document container name.
Dim x As Integer 'For looping.
Dim cntContainer As Container 'Containers in the database.
Dim strDocName As String 'Name of the document.
Dim intConst As Integer
Dim cdb As Database 'Current database.
Dim rel As Relation 'Relation to copy.
Dim nrel As Relation 'Relation to create.
Dim strRName As String 'Copied relation's name.
Dim strTName As String 'Relation table name.
Dim strFTName As String 'Relation foreign table name.
Dim varAtt As Variant 'Attributes of the relation.
Dim fld As Field 'Field(s) in the relation to copy.
Dim strFName As String 'Name of the field to append.
Dim strFFName As String 'Foreign name of the field to append.
'Open the database that contains objects to import.
Set db = DBEngine.Workspaces(0).OpenDatabase(strPath, False, False, "MS Access;PWD=<Your database password>")
'Import tables from the specified Access database.
For Each td In db.TableDefs
strTDef = td.Name
If Left(strTDef, 4) <> "MSys" Then
DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acTable, _
strTDef, strTDef, False
End If
Next
'Import queries.
For Each qd In db.QueryDefs
strTDef = qd.Name
DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acQuery, _
strTDef, strTDef, False
Next
'Copy relationships to the current database.
Set cdb = CurrentDb
For Each rel In db.Relations
With rel
'Get the properties of the relation to copy.
strRName = .Name
strTName = .Table
strFTName = .ForeignTable
varAtt = .Attributes
'Create a relation in the current database with the same properties.
Set nrel = cdb.CreateRelation(strRName, strTName, strFTName, varAtt)
For Each fld In .Fields
strFName = fld.Name
strFFName = fld.ForeignName
nrel.Fields.Append nrel.CreateField(strFName)
nrel.Fields(strFName).ForeignName = strFFName
Next
cdb.Relations.Append nrel
End With
Next
'Loop through the containers and import all documents.
For x = 1 To 4
Select Case x
Case 1
strCntName = "Forms"
intConst = acForm
Case 2
strCntName = "Reports"
intConst = acReport
Case 3
strCntName = "Scripts"
intConst = acMacro
Case 4
strCntName = "Modules"
intConst = acModule
End Select
Set cntContainer = db.Containers(strCntName)
For Each doc In cntContainer.Documents
strDocName = doc.Name
DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, intConst, _
strDocName, strDocName
'Debug.Print strDocName
'for debugging will list document names in the Debug window.
Next doc
Next x
'Clean up variables to recover memory.
Set fld = Nothing
Set nrel = Nothing
Set rel = Nothing
Set cdb = Nothing
Set td = Nothing
Set qd = Nothing
Set cntContainer = Nothing
db.Close
Set db = Nothing
msgbox "The database has been imported successfully."
ImportDb = True
End Function
Note Before you run this sample code, replace <Your
database password> in the sample code with the password that
you have set for your Access database. - On the View menu, click Immediate
Window.
- In the Immediate window, type the following command line,
and then press ENTER:
?ImportDb("<Full path of your Access database>")
After the sample code runs successfully, you can use
the new Access database that is named RecoveredDB.mdb instead of your old
Access database.