0

net and i'm trying to insert data into access database using sql, i have the code below, when i try to execute, it prompts me an error message and highlighting con.open() i don't understand why it's not working, can anyone guide me. Thank

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles AddBut.Click

        Dim dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= c:\Databse\Company_db.accdb"
        Dim dbSource = "Data Source= C:\Databse\Company_db.accdb"

        Dim empNum As String
        Dim empFname As String
        Dim empLname As String
        Dim empDept As String
        Dim empStat As String
        Dim empYears As String


        empNum = eNumText.Text
        empFname = empFnameText.Text
        empLname = empLnameText.Text
        empDept = Deptd.Text
        empStat = Statd.Text
        empYears = yearstext.Text




        Dim sql = "INSERT INTO tbl_empinfo (EmpID, FirstName, LastName, Department, Status, Years " & _
           ") " & _
           "Values(empNum, empFname, empLname, empDept, empStat, empYears)"

        con.ConnectionString = dbProvider & dbSource
        Using cmd = New OleDb.OleDbCommand(sql, con)
            con.Open()
            cmd.Parameters.AddWithValue("EmpID", empNum)
            cmd.Parameters.AddWithValue("FirstName", empFname)
            cmd.Parameters.AddWithValue("LastName", empLname)
            cmd.Parameters.AddWithValue("Department", empDept)
            cmd.Parameters.AddWithValue("Status", empStat)
            cmd.Parameters.AddWithValue("Years", empYears)
            cmd.ExecuteNonQuery()

        End Using


        con.Close()
    End Sub
2
  • Have you misspelled C:\Databse\? Perhaps C:\Database\ Commented Apr 1, 2014 at 9:17
  • @Remou nope that's really is exactly my folder's name :( Commented Apr 1, 2014 at 9:24

2 Answers 2

3

Standard problem, when you see a Syntax error in an otherwise fine SQL statement, look for RESERVED KEYWORDS for the underlying database.

In your case the word POSITION is a reserved keyword for MS-ACCESS.
Put it between square brackets

Dim sql = "INSERT INTO tbl_empinfo (EmpID, FirstName, LastName, Department, " & _ 
          "[Position], Status, Years) " & _ 
          "Values(empNum, empFname, empLname, empDept, empStat, empYears)"

However, you have another error in that query. You have 7 fields to insert but you pass only 6 parameters, missing just the parameter for the POSITION field.

You need to fix also the connection string. You write

  con.ConnectionString = dbProvider & dbSource

but this result in an invalid file name

  "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= 
   c:\Databse\Company_db.accdbData Source= C:\Databse\Company_db.accdb"

(line splitted for readability)

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for this. @OP please also see this link for reserved keywords. technet.microsoft.com/en-us/library/ms189822.aspx
i tried to follow your advise and have this pastebin.com/J1kFBFT9 but it's still giving me the exact error "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: Not a valid file name." message and highlighting con.Open() I still don't get it why?
Possible typo in the folder name? C:\Databse, still missing the 7th parameter
@Steve nope Sir, i have the exact Path as Databse, i have updated my Parameters from my code above sorry about that, but it's still giving me the same error :(
I see you define the connection string with con.ConnectionString = dbProvider & dbSource, but the variable dbProvider already contains the DataSource part, try fixing that leaving just one Data Source part
|
0

I consider that you have initialized the object of "OleDbConnection".

Try to get the Connection String from the Property window of your database (From Server Explorer Window right click on your database select properties option).

And when you are opening the connection use following code:

    If con.State = ConnectionState.Closed Then
        con.Open()
    End If

Hope it helps you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.