Creating Access 07 DB with VB.net always runs Repair when opening it the first time

242 views Asked by At

I have a VB.NET (Visual Studio 2010) app that creates an Access 07 database programmatically, then imports a CSV file as a new Access table. The DB gets created, and the CSV gets imported with no problems. The code uses ADOX.Catalog to create the database, and an OleDb.OleDbConnection with ACE to import the CSV. All is well, except the first time I open the Access DB. When I launch Access 07 from my desktop (Office 07), I get the green "repair" progress bar at the bottom right of the screen for about 5 seconds. It only happens the first time I open the DB. The DB and tables work fine, but Access is definitely fixing something. I can recreate this behavior every time. How can I avoid the repair when opening the DB the first time? Any ideas would be helpful.

Public Function CreateTaxDatabase(ByVal DatabaseFullPath As String) As Boolean

  Dim bAns As Boolean
  Dim cat As ADOX.Catalog

  Try
     '' check if file exists
     If System.IO.File.Exists(DatabaseFullPath) Then
        '' delete the old file
        System.IO.File.Delete(DatabaseFullPath)
     End If

     Dim sCreateString As String
     cat = New ADOX.Catalog()
     sCreateString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DatabaseFullPath
     cat.Create(sCreateString)

     '' close the connection
     Dim connection As ADODB.Connection = DirectCast(cat.ActiveConnection, ADODB.Connection)
     connection.Close()
     bAns = True
  Catch Excep As System.Runtime.InteropServices.COMException
     bAns = False
  Finally
     cat = Nothing
  End Try
  Return bAns
End Function

Sub ImportCSV(dbPath As String, CSVPath As String, CSVFile as String)

  Dim conn As OleDb.OleDbConnection = Nothing
  Dim SQL As String = ""

  Try
     conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath)
     If conn.State = ConnectionState.Closed Then
        conn.Open()
     End If
  Catch ex As Exception
     MsgBox(ex.Message, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Error Connecting to Database")
     If conn.State = ConnectionState.Open Then
        conn.Close()
     End If
 Return
  End Try

  SQL = "SELECT * INTO Table1 FROM [Text;FMT=Delimited;HDR=Yes;CharacterSet=ANSI;DATABASE=" + CSVPath + "].[" + CSVFile + "]"

  Try
     Dim SQLCmd As OleDb.OleDbCommand = conn.CreateCommand
     SQLCmd.CommandText = SQL
     SQLCmd.ExecuteNonQuery()
     Application.DoEvents()
  Catch ex As Exception
     SQL = "There was an error executing the following SQL Statement:" + vbCrLf + _
        SQL + vbCrLf + "Error - " + Trim(Str(Err.Number)) + " " + Err.Description
     MsgBox(SQL, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "SQL Error")
  Finally
     If conn.State = ConnectionState.Open Then
        conn.Close()
     End If
     If Not IsNothing(conn) Then
        conn = Nothing
     End If
  End Try
End Sub
1

There are 1 answers

0
Fred Quatro On

This seemed to do the trick

Public Sub CompactRepairDB(DBPath As String)
   '' compact and repair DB
   Dim AccessApp As Microsoft.Office.Interop.Access.Application = New Microsoft.Office.Interop.Access.Application()
   Dim TempDB = GetFilePath(DBPath) + "Compact1.ACCDB"

   DeleteFile(TempDB)
   AccessApp.OpenCurrentDatabase(DBPath)
   AccessApp.CloseCurrentDatabase()
   AccessApp.CompactRepair(DBPath, TempDB)
   AccessApp.Quit()
   DeleteFile(DBPath)
   '' rename Compact1 to Original DB Name
   My.Computer.FileSystem.RenameFile(TempDB, GetFileName(DBPath))
End Sub