I want to Connect different Access database to my application with the same Tables and same Fields from a textbox which i used a openfileDialog to take a access database from my files
This the piece of code that i have But keep giving me an error
Imports System.Data.OleDb
Public Class Form2
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
OpenFileDialog1.Filter = "(*.mdb, *.accdb)|*.mdb; *.accdb"
OpenFileDialog1.RestoreDirectory = True
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
OpenFileDialog1.SafeFileNames.Count()
For i As Integer = 0 To OpenFileDialog1.SafeFileNames.Count() - 1
TextBox1.Text = OpenFileDialog1.SafeFileNames(i)
Next
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim connetionString As String
Dim cnn As OleDbConnection
Dim cmd As OleDbCommand
Dim sql As String
Dim reader As OleDbDataReader
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Textbox1.text;"
sql = "Select * From Table1"
cnn = New OleDbConnection(connetionString)
Try
cnn.Open()
cmd = New OleDbCommand(sql, cnn)
reader = cmd.ExecuteReader()
While reader.Read()
MsgBox(reader.Item(0) & " - " & reader.Item(1) & " - " & reader.Item(2))
End While
reader.Close()
cmd.Dispose()
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class