Click here to Skip to main content
15,845,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello i am trying to add data to my access(2007) database using visual basic 2008.

i got no errors or anything but it wont add what can be the problem... this is my code

Imports Access = Microsoft.Office.Interop.Access
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection

Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "transport.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString

If True Then
Dim str As String
str = "insert into Details ([Name], [Number], [Destination]) values (?, ?, ?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
cmd.Parameters.Add(New OleDbParameter("Name", CType(txtName.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Number", CType(txtNumber.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Destination", CType(txtdes.Text, String)))

Catch ex As Exception
End Try
MessageBox.Show(" Data Added")

End If

End Sub
End Class
Updated 29-Sep-14 11:57am

1 solution

Where the database is located? If it's located in project folder, move it to the different location.

Check connection string[^]. The Data Source parameter must reference to existing file, such as: Data Source=C:\myFolder\myAccessFile.accdb;
Share this answer
Member 10471797 29-Sep-14 17:34pm    
thanks works now..why wont the one in the project folder work?
Maciej Los 30-Sep-14 2:12am    
I'm not sure the reason of described behaviour... I read it somewhere.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900