I want to connect data from any folder in my computer.After user click browse button it appear any location for access database folder.After that user click connect button and but i have facing connection problem.
Under Browser Button
Using ofd As New OpenFileDialog
ofd.Filter = "mdb files |*.mdb"
ofd.Title = "Select File"
ofd.ShowDialog()
TextBox1.Text = ofd.FileName
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
MessageBox.Show("Hang amik " & TextBox1.Text)
End If
End Using
Under connect button
Dim connetionString As String
Dim cnn As OleDbConnection
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= & TextBox1.Text "
cnn = New OleDbConnection(connetionString)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
My main problem what code must inserted for connecting to database?
Data Source= & TextBox1.Text
My code not work.
Using Visual Studio 2010.vb.net.Thank You Very Much.
Fix:
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & TextBox1.Text
Use String.Format to best improve usability in build string with variables.
Related
I have a program that reads an excel file that is on my local disk. I need this file to be accessible to other individuals to edit the file and allow the program to execute based on its contents. The way were making this accessible is sharing it via OneDrive. The file is read by a OLEDB Script I wrote that takes the directory of the file to read it and load into temp table on the app. I have been searching online for a solution to this in visual basic but a lot of the solutions are written in c# and its hard for me to translate or understand the logic. Here is an example of the solution in pseudo code:
If (ONEDRIVE_FILE_DIRECTORY IsNot Nothing) Then
' STORE FILE FROM ONE DRIVE INTO LOCAL_FILE_DIRECTORY
End If
Dim strConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="LOCAL_FILE_DIRECTORY";Extended Properties=""Excel 8.0;HDR=YES"";"
Dim dt As New DataTable
dt.TableName = "Results"
dt.Columns.AddRange(New DataColumn() {New DataColumn("LOC_ID", GetType(String)),
New DataColumn("EMAIL", GetType(String)),
New DataColumn("INTERVAL", GetType(String)),
New DataColumn("PASSWORD", GetType(String))
})
Dim connection As OleDbConnection = New OleDbConnection(strConnectionString)
Dim strSQL As String = "SELECT [LOC_ID], [EMAIL], [INTERVAL], [PASSWORD] FROM [Report$] WHERE [LOC_ID] <> ''"
Try
connection.Open()
Using Adp As New OleDbDataAdapter(strSQL, connection)
Adp.Fill(dt)
End Using
Catch ex As Exception
LogMessage(ex.ToString, "Error message in """ & strApplicationName & """ job on " & strComputerName & " by: " & strUsername)
SendEmailErrorMessage(ex.ToString)
Finally
If connection.State = ConnectionState.Open Then
connection.Close()
End If
End Try
Hey I'm trying to use this code to save to my access database and although there are no errors and it says data saved, my access database doesn't receive the new data. What's wrong with it?
Private Sub savenew()
Dim conn As New OleDbConnection
conn = New OleDbConnection
dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbsource = "Data Source = FULL YUGIOH ACCESS DATABASE.accdb;"
conn.ConnectionString = dbprovider & dbsource
Dim reader As OleDbDataReader
Dim command As OleDbCommand
Try
conn.Open()
Dim query As String
query = "insert into item(name) values ('" & TextBox4.Text & "')"
command = New OleDbCommand(query, conn)
reader = command.ExecuteReader
MsgBox("data saved")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
(table in my database is called item, and column is called name.)
I am trying to import an Excel file into an Access DB via vb.net
the idea is the customer can export the data into excel, modify it, add, delete them importing it back.
The data exported has exactly the same format than the table to import to.
I am using the code below:
Try
Dim strFileName As String = String.Empty
Dim XLda As New OleDbDataAdapter
Dim ExcelTables As New DataTable
Dim StrSelect = "SELECT * FROM [{0}]"
OpenFileDialog1.FileName = ""
OpenFileDialog1.InitialDirectory = mdlGlobalStuff.sMasterDataPath
OpenFileDialog1.Filter = "Excel|*.xls|All files (*.*)|*.*"
If OpenFileDialog1.ShowDialog() <> Windows.Forms.DialogResult.OK Then
Exit Sub
End If
strFileName = OpenFileDialog1.FileName
Dim MyXLConnection As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strFileName & ";Extended Properties=Excel 8.0;")
Using MyXLConnection
Using cmd As New OleDbCommand
cmd.Connection = MyXLConnection
cmd.CommandText = "INSERT INTO [MS Access;Database=InvoicingToolDB.accdb].[tbl_Bases] SELECT * FROM [Sheet1$]"
If MyXLConnection.State = ConnectionState.Open Then
MyXLConnection.Close()
End If
MyXLConnection.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As Exception
MsgBox("ImportLinkLabel_LinkClicked: Importing Base data" & vbCrLf & ErrorToString())
End Try
I always have an error message saying:
Unrecognized database format 'c:\--path to db--\InvoicingToolDB.accdb'
The path is correct and I don't understand why the format wouldn't be recognized.
Ok I have found the issue. The OLEDB Provider version was not the right one (4.0 is not reading Access .accdb format but old .mdb format only)
Replacing:
Dim MyXLConnection As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strFileName & ";Extended Properties=Excel 8.0;")
With:
Dim MyXLConnection As New leDbConnection("provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strFileName & ";Extended Properties=Excel 8.0;")
Works perfectly.
[Error] empty path name is not legal
The problem is I cannot insert image to Access database. Which line is error. Thanks In advance who helping me
Regards,
Fizul
Dim OpenFileDialog1 As New OpenFileDialog
Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
Dim breader As New IO.BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
cnn.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\db1.accdb;"
cnn.Open()
Dim sql As String
sql = "insert into Table1 Values(" & TextBox1.Text & ",'" & imgbuffer.Length & "')"
Dim cmd As New OleDb.OleDbCommand(sql, cnn)
cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
The problem is in these first two lines:
Dim OpenFileDialog1 As New OpenFileDialog
Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
You created an instance of OpenFileDialog, but you never called it's ShowDialog method so the FileName property is Nothing. You need something like this:
Using OpenFileDialog1 As New OpenFileDialog
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
'remaining of code here
End If
End Using
The Using statement makes sure that the dialog is disposed correctly.
I'm trying to save a picturebox into an ole database.
Here's my code :
Dim stream As New IO.MemoryStream
PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)
Try
Dim query As String = "INSERT INTO Guestinfo ([GuestName],[Phone],[Idofguest],[Room],[Arrival],[Checkout],[Address],[IDImage]) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Text & "','" & DateTimePicker2.Text & "','" & TextBox4.Text & "',(#IDImage))"
Dim command As New OleDbCommand
With command
.CommandText = query
.Parameters.AddWithValue("#Picture", stream.GetBuffer())
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("Saved Successfully!", MsgBoxStyle.Information)
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
In "A Generic Error occurred in GDI+" this error appear:
PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)
By the way, my column type is Ole Object.
I hope someone help me ...
This application has an Open Button which will help you open any picture file to a PictureBox on the form using OpenFileDialog. You will see the path of the picture file in a disabled TextBox. When you click the update button the Picture's path is saved to an Access Database.
Follow the steps below to create a similar project for yourself:
* Create a new Visual Basic.net project. Select Windows Forms Application from New Project Dialog Box. Name this application whatever you want.
* Create the following with below mentioned properties:
- Form - (Name): sample, Text: FormPictureApplication
- PictureBox - (Name): PictureBox1, SizeMode: StretchImage
- Button - (Name): ButtonUpdate, Text: &Update
- Button - (Name): ButtonOpen, Text: &Open
- TextBox - (Name): TextBoxPictureFilePath, Enabled: False
Double Click the Form, insert the following code right above Public Class {...}:
Imports System.Data.OleDb
Imports System.IO
Imports Microsoft.Win32
Double Click ButtonOpen and insert the following code:
Dim img As String
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = Nothing
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
openFileDialog1.FileName = ""
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
TextBoxPictureFilePath.Text = ""
img = openFileDialog1.FileName
PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)
TextBoxPictureFilePath.Text = openFileDialog1.FileName
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
Create a Microsoft Access Database in your convenient location and name it as Databasemikeoe2003PictureApplication.mdb
Create a table with the name Tablemikeoe2003PictureApplication and add following Columns to it:
Id - Datatype: Autonumber
PicturePath - DataType: Memo (as file paths can be considerably long at times)
Double Click the UpdateButton and insert the following code:
Try
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim mySQLString As String
myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Databasemikeoe2003PictureApplication.mdb;")
myConnection.Open()
mySQLString = "INSERT INTO Tablemikeoe2003PictureApplication (PicturePath) VALUES('" & Replace$(TextBoxPictureFilePath.Text, "'", "''") & "')"
myCommand = New OleDbCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()
PictureBox1.Image = Nothing
TextBoxPictureFilePath.Text = ""
Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
End Try
Run the application, it should work as desired.