I use following code to set a connection to my database: Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\FileRename v5.4.accdb")
I wanted to extent this function by checking if the database exists and If NOT to open a FileOpenDialog
to choose another database in another folder. I cant seem to get it to work because when I place the FileOpenDialog
on my Main form it throws an Exception error.
Public Function Jokendb() As OleDbConnection
Dim FileName As String = Application.StartupPath & "\FileRename v5.4.accdb"
If IO.File.Exists(FileName) Then
Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\FileRename v5.4.accdb")
Else
'Dim result As DialogResult = OpenFileDialog1.ShowDialog()
Dim str As String = OpenFileDialog1.FileName.ToString()
'If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
' Get the file name.
Dim path As String = OpenFileDialog1.FileName
Try
' Read in text.
Dim text As String = File.ReadAllText(path)
Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & text)
Catch ex As Exception
' Report an error.
Me.Text = "Error"
End Try
'End If
End If
End Function
I don't know why those line are comented but i think your code could work with these changes:
Public Function Jokendb() As OleDbConnection
Dim FileName As String = Application.StartupPath & "\FileRename v5.4.accdb"
If IO.File.Exists(FileName) Then
Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\FileRename v5.4.accdb")
Else
OpenFileDialog1.ShowDialog()
'Dim str As String = OpenFileDialog1.FileName.ToString()
'If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
' Get the file name.
Dim path As String = ""
If OpenFileDialog1.filename <> "" Then
path = OpenFileDialog1.FileName
End If
Try
' Read in text.
Dim text As String = File.ReadAllText(path)
Return New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & text)
Catch ex As Exception
' Report an error.
Me.Text = "Error"
End Try
'End If
End If
End Function