6 Replies - 850 Views - Last Post: 10 June 2016 - 01:02 PM Rate Topic: -----

#1 SaraF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-June 16

Insert data from VB.NET to MS access

Posted 10 June 2016 - 09:46 AM

hello everyone ,

I'm trying to insert a data from vb.net to ms access , the applciation works fine until I click on this button , I get an error on this line " myConnection.ConnectionString = Connstring" saying " ArgumentException was unhandled " !
does anyone care to take a look at my code and share your ideas why it so?

Thanks in advance.

What I have tried:

Imports System
Imports System.Data
Imports System.Data.OleDb


Public Class Form18
Dim Base As String
Dim Dfichier As String
Dim Connstring As String
Dim myConnection As OleDbConnection = New OleDbConnection


Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Clear()
TextBox2.Clear()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Base= "Base = Microsoft.ACE.OLEDB.12.0;Data source="
Dfichier = "C:\Users\Sara\Desktop\Application\Liste des opérateurs.accdb"
Connstring = Base & Dfichier
myConnection.ConnectionString = Connstring
myConnection.Open()
Dim str as String
str = "Insérer votre matricule et zone ([Matricule],[Zone]) Values (?,?)"

Dim cmd As oleDbCommand= New OleDbCommand (str, Myconnection)
cmd.Parameters.Add(New OleDbParameter("Matricule", CType(TextBox2.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Zone", CType(TextBox1.Text, String)))

Try

cmd.ExecuteNonQuery()
cmd.dispose()
myConnection.Close()
TextBox1.Clear()
TextBox2.clear()
Catch ex As Exception
MsgBox(ex.Message)
End try 

End Sub
End Class
:code:

This post has been edited by modi123_1: 10 June 2016 - 09:54 AM
Reason for edit:: please use the '[code]' tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: Insert data from VB.NET to MS access

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Insert data from VB.NET to MS access

Posted 10 June 2016 - 09:54 AM

Doesn't quite look like the connection string format for Access.

https://www.connecti...ngs.com/access/
Was This Post Helpful? 1
  • +
  • -

#3 Recoil   User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Insert data from VB.NET to MS access

Posted 10 June 2016 - 09:55 AM

Hi SaraF, welcome to DiC!

First, you will get better help by posting your code in code tags (remove spaces):

[ code ]
Insert code here
[ /code ]

Second, your connection string look like it is incorrect and should resemble:
Dim con As New OledbConnection("Provider=microsoft.Jet.oledb.4.0DataSource=D:\mydata.mdb;")


Was This Post Helpful? 1
  • +
  • -

#4 SaraF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-June 16

Re: Insert data from VB.NET to MS access

Posted 10 June 2016 - 10:18 AM

Thank you for replying , I ve corrected that to :
provider = "Provider = Microsoft.ACE.OLEDB.12.0;Data source= C:\Users\Sara\Desktop\Application\Liste des opérateurs.accdb"

But now I get an error on line 23 : saying " OleDbException was unhandled"
Was This Post Helpful? 0
  • +
  • -

#5 Recoil   User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Insert data from VB.NET to MS access

Posted 10 June 2016 - 10:49 AM

Sorry, it has been a bit since I have worked with Access and VB together. Check out the link modi123_1 provided above:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;



Using that I created a dummy Access db on my desktop, I pasted your code in a blank form, added the controls, then changed this line below to reflect a correct connection string:
myConnection.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source= C:\Users\Sara\Desktop\Application\Liste des opérateurs.accdb;Persist Security Info=False;"



You can also set a breakpoint on your open connection to verify the values are correct before proceeding. Likewise, a Try/Catch statement with a messagebox can help when you are debugging :)
Was This Post Helpful? 1
  • +
  • -

#6 SaraF   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-June 16

Re: Insert data from VB.NET to MS access

Posted 10 June 2016 - 12:04 PM

Thanks all for helping , I've made another code (easier) and it worked !! so i'll let it here for anyone who will have the same issue :

 Dim myConnection As OleDbConnection
        Dim DBpath As String = "C:\Users\Sara\Desktop\Application\Liste.accdb"
        Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";Persist Security Info=True"
        myConnection = New OleDbConnection(sConnectionString)
        myConnection.Open()
        Dim SQLstr As String
        SQLstr = "INSERT INTO Table1 ([matricule], [zone]) VALUES ('" & TextBox2.Text & "','" & TextBox1.Text & "')"
        Dim cmd As New OleDbCommand(SQLstr, myConnection)
        If (cmd.ExecuteNonQuery() > 0) Then
            MsgBox("Merci!")
        End If
:code:

This post has been edited by modi123_1: 10 June 2016 - 12:05 PM
Reason for edit:: please use the '[code]' tag button in the editor

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,531
  • Joined: 12-December 12

Re: Insert data from VB.NET to MS access

Posted 10 June 2016 - 01:02 PM

You should use parameterized queries rather than concatenating text directly into your SQL statement.

Using Parameters with MS Access/OleDb
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1