Connect to access, with password & the registry!
Just though that i'd post the result of many hours of stuffing around trying to get a neat way to connect to an .mdb, using a password for security, and getting the path out of the registry. both of which required the help of many of you on here.
thanks!
Code:
Private Function ConnectToDatabase()
Dim strDatabasePath As String
Dim FindFile As New OpenFileDialog()
Dim RegKey As Microsoft.Win32.RegistryKey
Try
RegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(gcRegistryLocation, True)
strDatabasePath = (RegKey.GetValue("DBPath", ""))
Catch
Microsoft.Win32.Registry.LocalMachine.CreateSubKey(gcRegistryLocation)
RegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(gcRegistryLocation, True)
RegKey.SetValue("DBPath", "")
End Try
Do While (strDatabasePath = "") _
Or File.Exists(strDatabasePath) = False
FindFile.Title = "Cannot Locate Data File. Please Specify"
If FindFile.ShowDialog() = DialogResult.Cancel Then
strDatabasePath = ""
Else
strDatabasePath = FindFile.FileName
End If
Loop
If Trim(strDatabasePath) = "" Then
End
Else
RegKey.SetValue("DBPath", strDatabasePath)
End If
gconDatabase.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDatabasePath & ";" & _
"Persist Security Info=False;" & _
"Jet OLEDB: Database Password=" & gcDatabasePassword & ";"
Try
gconDatabase.Open()
Catch ex As Exception
MessageBox.Show("Sorry, but the data file that you have tried to connect to is not the correct file." & vbCrLf & _
"The full error message is:" & vbCrLf & ex.Message & vbCrLf & vbCrLf & _
"Please verify that the file exists and is valid, and try again.", "Error Connecting", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
RegKey.SetValue("DBPath", "")
End
End Try
End Function
\