Losing my Access DB password on compact, how to reset from VB?
First time poster here...
My VB.NET program compacts and repairs my DB, but somewhere in the process unsets the database password. No problem, just reset it through VB, right? Ummm... not sure how to go about that task.
So I either need to know how to compact it without losing the password, or how to reset the password after I lose it.
Trying the best I can to make the code look decent in my post, let me know if there is a better way.
Thanks!
Dim strAccessDatabasePath As String = System.AppDomain.CurrentDomain.BaseDirectory & "App_Data\Squirrel.accdb"
Dim LockedDbFileInfo As New System.IO.FileInfo(strAccessDatabasePath.Replace(".accdb", ".laccdb"))
Dim TempFolder As String = System.AppDomain.CurrentDomain.BaseDirectory & "App_Data\temp"
Dim TempDB As String = Te开发者_StackOverflow中文版mpFolder & "\temp.accdb"
If LockedDbFileInfo.Exists Then
MsgBox("Database is in use. Please make sure no users are in the database before performing maintenance.")
Else
MsgBox("This program has detected no current database users and will proceed with database maintenance.")
End If
Try
Directory.CreateDirectory(TempFolder)
Catch ex As Exception
MsgBox("There has been an error creating a temporary directory in your application directory. Please check credentials and try again.")
End Try
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM d yyyy HHmmss "
Dim TempDateTime As String = time.ToString(format)
'Copy DB to backup directory.
Try
File.Copy(strAccessDatabasePath, txtDBbackupFolder.Text & "\" & "DBbkup " & TempDateTime & ".accdb")
Catch ex As Exception
MsgBox("There was an issue writing to the backup folder. Please check credentials and try again.")
End Try
'Copy DB to temp directory for maintenance.
File.Copy(strAccessDatabasePath, TempFolder & "\" & "temp.accdb")
File.Delete(strAccessDatabasePath)
Dim JROEng As Object
JROEng = CreateObject("JRO.JetEngine")
JROEng.CompactDatabase("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & TempDB & ";Jet OLEDB:Database Password=woodkitten70;Jet OLEDB:Engine Type=5", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
strAccessDatabasePath & ";Jet OLEDB:Engine Type=5")
'connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\Squirrel.accdb;Jet OLEDB:Database Password=XXXXXXXXX"
Try
Kill(TempFolder & "\*.*")
Catch ex As Exception
'MsgBox(ex.Message)
End Try
Directory.Delete(TempFolder)`
Can you refer to the Access interop and use that compact repair? "Microsoft.Office.Interop.Access.ApplicationClass.CompactRepair(String, String, [Boolean]) As Boolean"
Public Overridable Function CompactRepair(ByVal SourceFile As String, ByVal DestinationFile As String, Optional ByVal LogFile As Boolean = False) As Boolean
Member of Microsoft.Office.Interop.Access.ApplicationClass
精彩评论