Microsoft KB Archive/888634

= You receive the &quot;The database is in an unexpected state&quot; error message when you try to open a database in Access 2000 or a later version of Access =

Article ID: 888634

Article Last Modified on 9/28/2007

-

APPLIES TO


 * Microsoft Office Access 2007
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition
 * Microsoft Access 2000 Standard Edition

-





This article applies to a Microsoft Access database (.mdb) file or to a Microsoft Access database (.accdb) file.

Moderate: Requires basic macro, coding, and interoperability skills.



SYMPTOMS
When you try to open a database in Microsoft Access 2000 or a later version, you receive the following error message:

The database is in an unexpected state



CAUSE
This problem may occur when you use the Data Access Object (DAO) library to convert a database that you created in Microsoft Access 97 or an earlier version by using the CompactDatabase method. The CompactDatabase method may leave the new database in a partially converted state.



RESOLUTION
To resolve this problem, use one of the following methods.

Method 1: Use the Convert Database command when you have the original database
If you still have a copy of the original database in its original format, use the Convert Database command. To do this, follow these steps:

Access 2000, Access 2002, or Access 2003

 * 1) Make a backup copy of the original database.
 * 2) Start Access 2000 or a later version.
 * 3) On the Tools menu, click Database Utilities, click Convert Database, and then click To Access 2000 File Format.

Note If you are using Access 2000, only To Current Access Database Version appears on the Convert Database menu.
 * 1) In the Database to Convert From dialog box, click the database file name that you want to convert, and then click Convert.
 * 2) In the Convert Database Into dialog box, type the new name of the database file, and then click Save.

Access 2007

 * 1) Make a backup copy of the original database.
 * 2) Try to open that database.
 * 3) When you open an Access 97 or Access 95 file format .mdb database, Access displays the Database Enhancement dialog box. You are prompted to upgrade the database.
 * 4) Click Yes to upgrade the database to whichever file format you have selected as the default file format in Access 2007. After you convert the database, you can make design changes to the file in Access 2007. However, you can no longer open the database by using a version of Access earlier than the version to which you converted the database.

Method 2: Recover the database data and the database queries when you do not have the original nonsecured database
If you do not have a copy of the original nonsecured database in its original format and you have tried standard corruption troubleshooting techniques, try to recover the database data and the database queries. To do this, follow these steps:  Make a backup copy of the original database. Start Access 2000 or a later version. Access 2000, Access 2002, or Access 2003  Click Blank Access database, type the new database name in the File name box, and then click Create.

Access 2007  Click Office button, click New, click Blank Database, and then click Create to create a new blank database.</li></ul> </li> Access 2000, Access 2002, or Access 2003  On the Insert menu, click Module. The Microsoft Visual Basic Editor starts, and a new module is created.</li></ul>

Access 2007  On the Create tab, click the down arrow below Macro, and then click Module. The Microsoft Visual Basic Editor starts, and a new module is created.</li></ul> </li> On the Tools menu, click References.</li> In the Available References list, locate Microsoft DAO 3.6 Object Library, and then click to select the Microsoft DAO 3.6 Object Library check box.

Note DAO 3.6 is also available on Windows XP Home Edition.</li> To close the References dialog box, click OK.</li>  Paste the following code into the new module that you created. Sub RecoverCorruptDB Dim dbCorrupt As DAO.Database Dim dbCurrent As DAO.Database Dim td As DAO.TableDef Dim tdNew As DAO.TableDef Dim fld As DAO.Field Dim fldNew As DAO.Field Dim ind As DAO.Index Dim indNew As DAO.Index Dim qd As DAO.QueryDef Dim qdNew As DAO.QueryDef Dim strDBPath As String Dim strQry As String ' Replace the following path with the path of the ' corrupted database. strDBPath = &quot;C:\My Documents\yourDatabase.mdb&quot; On Error Resume Next Set dbCurrent = CurrentDb Set dbCorrupt = OpenDatabase(strDBPath) For Each td In dbCorrupt.TableDefs If Left(td.Name, 4) <> &quot;MSys&quot; Then strQry = &quot;SELECT * INTO [&quot; & td.Name & &quot;] FROM [&quot; & td.Name & &quot;] IN '&quot; & dbCorrupt.Name & &quot;'&quot; dbCurrent.Execute strQry, dbFailOnError dbCurrent.TableDefs.Refresh Set tdNew = dbCurrent.TableDefs(td.Name) ' Re-create the indexes on the table. For Each ind In td.Indexes Set indNew = tdNew.CreateIndex(ind.Name) For Each fld In ind.Fields Set fldNew = indNew.CreateField(fld.Name) indNew.Fields.Append fldNew Next indNew.Primary = ind.Primary indNew.Unique = ind.Unique indNew.IgnoreNulls = ind.IgnoreNulls tdNew.Indexes.Append indNew tdNew.Indexes.Refresh Next End If   Next ' Re-create the queries. For Each qd In dbCorrupt.QueryDefs If Left(qd.Name, 4) <> &quot;~sq_&quot; Then Set qdNew = dbCurrent.CreateQueryDef(qd.Name, qd.SQL) End If   Next dbCorrupt.Close Application.RefreshDatabaseWindow MsgBox &quot;Procedure Complete.&quot; End Sub Note The code will try to import all tables and all queries from the corrupted database into the current database. Replace  with the correct path and file name of your database. </li> To run the code, click Run Sub/UserForm on the Run menu.</li></ol>

Method 3: Recover the database data when you do not have the original secured database
If you do not have a copy of the original secured database in its original format and you have tried standard corruption troubleshooting techniques, try to recover the database data. To do this, follow these steps: <ol> Make a backup copy of the original database.</li> Start Access 2000 or a later version.</li> Access 2000, Access 2002, or Access 2003  Click Blank Access database, type the new database name in the File name box, and then click Create.</li></ul>

Access 2007 <ul> <li>Click the Microsoft Office Button, click New, click Blank Database, and then click Create to create a new blank database.</li></ul> </li> <li>Access 2000, Access 2002, or Access 2003 <ul> <li>On the Insert menu, click Module. The Microsoft Visual Basic Editor starts, and a new module is created.</li></ul>

Access 2007 <ul> <li>On the Create tab, click the down arrow below Macro, and then click Module. The Microsoft Visual Basic Editor starts, and a new module is created.</li></ul> </li> <li>On the Tools menu, click References.</li> <li>In the Available References list, locate Microsoft DAO 3.6 Object Library, and then click to select the Microsoft DAO 3.6 Object Library check box.</li> <li>To close the References dialog box, click OK.</li> <li> Paste the following code into the new module that you created. Option Compare Database

Function BackupSecureDatabase On Error GoTo Err_BackupSecureDatabase Dim wrkDefault As DAO.Workspace Dim dbsNew As DAO.Database Dim dbeSecure As DAO.PrivDBEngine Dim wrkSecure As DAO.Workspace Dim dbsSecure As DAO.Database Dim tdfSecure As DAO.TableDef Dim strSecureUser As String Dim strSecurePwd As String Dim strSecurePathToDatabase As String Dim strSecurePathToWorkgroupFile As String Dim strTableName As String Dim strSQL As String Dim dbsTemp As DAO.Database Dim strTempPathToDatabase As String Dim strBackupPathToDatabase As String Dim strLogPath As String Dim SourceRec As DAO.Recordset Dim DestRec As DAO.Recordset

' Set the variables (change for environment). strSecurePathToDatabase = &quot;C:\MyDatabases\Northwind.mdb&quot; strSecurePathToWorkgroupFile = &quot;C:\MyDatabases\Secured.mdw&quot; strSecureUser = &quot;Administrator&quot; strSecurePwd = &quot;password&quot; strTempPathToDatabase = &quot;C:\MyDatabases\Temp.mdb&quot; strBackupPathToDatabase = &quot;C:\MyDatabases\Backup.mdb&quot; strLogPath = &quot;C:\MyDatabases\Backup.log&quot; ' Open the log file. Open strLogPath For Output As #1 Print #1, Time, &quot;Log file opened&quot; Print #1, Time, &quot;Variables set&quot; ' Delete old files. If Dir(strTempPathToDatabase) <> &quot;&quot; Then Kill strTempPathToDatabase If Dir(strBackupPathToDatabase) <> &quot;&quot; Then Kill strBackupPathToDatabase Print #1, Time, &quot;Old backup files deleted&quot; ' Create the new temp database. Set wrkDefault = DBEngine.Workspaces(0) Set dbsNew = wrkDefault.CreateDatabase(strTempPathToDatabase, dbLangGeneral) Set dbsNew = Nothing Print #1, Time, &quot;Temp database created&quot; ' Open the secured database. Set dbeSecure = New PrivDBEngine dbeSecure.SystemDB = strSecurePathToWorkgroupFile dbeSecure.DefaultUser = strSecureUser dbeSecure.DefaultPassword = strSecurePwd Set wrkSecure = dbeSecure.Workspaces(0) Set dbsSecure = wrkSecure.OpenDatabase(strSecurePathToDatabase) Print #1, Time, &quot;Secured database opened from &quot; & strSecurePathToDatabase ' Open the temp database. DBEngine(0).CreateUser Set dbsTemp = DBEngine(0).OpenDatabase(strTempPathToDatabase) Print #1, Time, &quot;Temp database opened from &quot; & strTempPathToDatabase ' Loop through the tables in the secured database. For Each tdfSecure In dbsSecure.TableDefs strTableName = tdfSecure.Name If Left(strTableName, 4) <> &quot;MSys&quot; Then Print #1, Time, &quot;Export of &quot; & strTableName ' Copy the table definition to the temp database. If CopyTableDef(tdfSecure, dbsTemp, strTableName) Then ' Then append all the data into the table. Set SourceRec = tdfSecure.OpenRecordset(dbOpenTable, dbReadOnly) Set DestRec = dbsTemp.OpenRecordset(strTableName) AppendRecordsFromOneRecordSetToAnother SourceRec, DestRec SourceRec.Close DestRec.Close

End If      End If   Next tdfSecure

' Close open objects. dbsSecure.Close Print #1, Time, &quot;Secured database closed&quot; dbsTemp.Close Print #1, Time, &quot;Temp database closed&quot; ' Compact the database into the backup database. DBEngine.CompactDatabase strTempPathToDatabase, strBackupPathToDatabase, dbLangGeneral Print #1, Time, &quot;New backup database created at &quot; & strBackupPathToDatabase ' Delete the temp database. If Dir(strTempPathToDatabase) <> &quot;&quot; Then Kill strTempPathToDatabase Print #1, Time, &quot;Temp database deleted&quot; Print #1, Time, &quot;Log file closed&quot; Close #1

Exit_BackupSecureDatabase:

Set wrkDefault = Nothing Set dbsNew = Nothing Set dbeSecure = Nothing Set wrkSecure = Nothing Set dbsSecure = Nothing Set tdfSecure = Nothing Set dbsTemp = Nothing Exit Function

Err_BackupSecureDatabase: Print #1, Time, &quot;    ***ERROR: &quot; & Err.Number, Err.Description, strTableName Resume Next

End Function

Function CopyTableDef(SourceTableDef As TableDef, TargetDB As Database, TargetName As String) As Integer Dim SI As DAO.Index, SF As DAO.Field, SP As DAO.Property Dim T As DAO.TableDef, I As DAO.Index, F As DAO.Field, P As DAO.Property Dim I1 As Integer, f1 As Integer, P1 As Integer If SourceTableDef.Attributes And dbAttachedODBC Or SourceTableDef.Attributes And dbAttachedTable Then CopyTableDef = False Exit Function End If Set T = TargetDB.CreateTableDef(TargetName) ' Copy Jet Properties. On Error Resume Next For P1 = 0 To T.Properties.Count - 1 If T.Properties(P1).Name <> &quot;Name&quot; Then T.Properties(P1).Value = SourceTableDef.Properties(P1).Value End If   Next P1    On Error GoTo 0 ' Copy Fields. For f1 = 0 To SourceTableDef.Fields.Count - 1 Set SF = SourceTableDef.Fields(f1) ' DAO 3.0 and later versions. ****     If (SF.Attributes And dbSystemField) = 0 Then Set F = T.CreateField ' Copy Jet Properties. On Error Resume Next For P1 = 0 To F.Properties.Count - 1 F.Properties(P1).Value = SF.Properties(P1).Value Next P1         On Error GoTo 0 T.Fields.Append F     End If ' Corresponding End If **** Next f1 ' Copy Indexes. For I1 = 0 To SourceTableDef.Indexes.Count - 1 Set SI = SourceTableDef.Indexes(I1) ' Foreign indexes are added by relationships. If Not SI.Foreign Then Set I = T.CreateIndex ' Copy Jet Properties. On Error Resume Next For P1 = 0 To I.Properties.Count - 1 I.Properties(P1).Value = SI.Properties(P1).Value Next P1         On Error GoTo 0 ' Copy Fields. For f1 = 0 To SI.Fields.Count - 1 Set F = T.CreateField(SI.Fields(f1).Name, T.Fields(SI.Fields(f1).Name).Type) I.Fields.Append F         Next f1        T.Indexes.Append I      End If    Next I1

' Append TableDef. TargetDB.TableDefs.Append T ' Copy Access/User Table Properties. For P1 = T.Properties.Count To SourceTableDef.Properties.Count - 1 Set SP = SourceTableDef.Properties(P1) Set P = T.CreateProperty(SP.Name, SP.Type) P.Value = SP.Value T.Properties.Append P   Next P1  ' Copy Access/User Field Properties. For f1 = 0 To T.Fields.Count - 1 Set SF = SourceTableDef.Fields(f1) Set F = T.Fields(f1) For P1 = F.Properties.Count To SF.Properties.Count - 1 Set SP = SF.Properties(P1) Set P = F.CreateProperty(SP.Name, SP.Type) P.Value = SP.Value F.Properties.Append P     Next P1    Next f1  ' Copy Access/User Index Properties. For I1 = 0 To T.Indexes.Count - 1 Set SI = SourceTableDef.Indexes(T.Indexes(I1).Name) ' Do not copy foreign indexes. They are created by relationships. If Not SI.Foreign Then Set I = T.Indexes(I1) For P1 = I.Properties.Count To SI.Properties.Count - 1 Set SP = SI.Properties(P1) Set P = I.CreateProperty(SP.Name, SP.Type) P.Value = SP.Value I.Properties.Append P       Next P1      End If    Next I1  CopyTableDef = True End Function

Function AppendRecordsFromOneRecordSetToAnother(SR As DAO.Recordset, DR As DAO.Recordset) Dim x As Integer

Do While Not SR.EOF DR.AddNew For x = 0 To SR.Fields.Count - 1 DR(x).Value = SR(x).Value Next x DR.Update SR.MoveNext Loop End Function Note The code will try to import all tables from the corrupted database into a backup database. Replace the variables in the table after step 10 with your database file locations and your user settings. </li> <li>In the list of functions, select BackupSecureDatabase.</li> <li>To run the code, click Run Sub/UserForm on the Run menu.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.

<div class="moreinformation_section">

MORE INFORMATION
For more information about how to troubleshoot corruption in a Microsoft Access database, click the following article number to view the article in the Microsoft Knowledge Base:

306204 How to troubleshoot corruption in a Microsoft Access database

Additional query words: ACC2000 ACC2002 ACC2003 ACC2007

Keywords: kbinfo kbtshoot kbimport kbconversion kbprb kbcorrupt KB888634

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.