Click here to Skip to main content
15,885,979 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

myConnection.Open()
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)))

Try
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
txtName.Clear()
txtNumber.Clear()
txtdes.Clear()
myConnection.Close()
MessageBox.Show(" Data Added")


End If

End Sub
End Class
Posted
Updated 29-Sep-14 10:57am
v3

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
 
Comments
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