-1

I am trying to insert data into an MS Access DB through a VB.net Windows Forms App

when executing this code

  Dim con As New OleDbConnection(CS)
            Dim cmd As New OleDbCommand("INSERT INTO Ports ([PortNumber] , [DistributionBoardLocation] , [DistributionBoardSubLocation] , [PortLocation] , [PortSubLocation] , [PortDevice] , [POE]) VALUES('" & nudPortNumber.Value & "', '" & txtDBLocation.Text & "', '" & txtDBSubLocation.Text & "' , '" & txtPortLocation.Text & "' , '" & txtPortSubLocation.Text & "' , '" & txtPortDevice.Text & "' , '" & chPOE.Checked & "'", con)


            con.Open()
                cmd.ExecuteNonQuery()
                con.Close()

                MsgBox("New Port Has Been Created")

i get this error message -

System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement 

Please Help

Thanks, Jacob

  • We'll need the actual SQL string. [How to debug dynamic SQL in VBA](http://stackoverflow.com/questions/418960/managing-and-debugging-sql-queries-in-ms-access/1099570#1099570) – Andre Oct 20 '22 at 10:16
  • Add a close parenthesis at the end of your VALUES clause like this: `& chPOE.Checked & "')", con)` – HansUp Oct 20 '22 at 15:23
  • 1
    Consider switching to a parameter query. But you will still need a `)` at the end of the `VALUES` clause. – HansUp Oct 20 '22 at 15:32

1 Answers1

0

You insert every value as text which probably is not the case. Adjust that, for example for the port number:

.. VALUES (" & nudPortNumber.Value & ", ..
Gustav
  • 53,498
  • 7
  • 29
  • 55