VB.NET with Acces database

Asked

Viewed 91 times

0

I’m trying to make a CRUD here using VB.NET and Access, only when I’m going to perform a INSERT returns the error:

"Sintax Error in INSERT INTO statement"

Code:

con.Open() 

Dim strCmd As String = "INSERT INTO Mapas (DataRegistro,NumReferencia,Local,Responsavel, OBS, Status)" & _ 
                                    "Values (? ,? ,? ,? ,? ,? )" 
Dim cmd As OleDbCommand = New OleDbCommand(strCmd, con) 

cmd.Parameters.AddWithValue("?", txtDataRegistro.Text) 
cmd.Parameters.AddWithValue("?", txtMapa.Text) 
cmd.Parameters.AddWithValue("?", txtLocal.Text) 
cmd.Parameters.AddWithValue("?", txtResponsavel.Text) 
cmd.Parameters.AddWithValue("?", txtObs.Text) 
cmd.Parameters.AddWithValue("?", chkStatus.Checked) 
cmd.ExecuteNonQuery() 

MsgBox("Mapa cadastrado com sucesso!" & vbCrLf & "Nome.: " & txtMapa.Text, MsgBoxStyle.Information)

1 answer

1

I particularly use defined names for each parameter. Try it that way:

con.Open()
Dim strCmd As String = "INSERT INTO Mapas (DataRegistro,NumReferencia,Local,Responsavel, OBS, Status) values (@DataRegistro,@NumReferencia,@Local,@Responsavel, @OBS, @Status)" 

Dim cmd As OleDbCommand = New OleDbCommand(strCmd, con) 

cmd.Parameters.Add("@DataRegistro", OleDb.OleDbType.Date)
cmd.Parameters.Add("@NumReferencia", OleDb.OleDbType.Integer)
cmd.Parameters.Add("@Local", OleDb.OleDbType.VarChar)
cmd.Parameters.Add("@Responsavel", OleDb.OleDbType.VarChar)
cmd.Parameters.Add("@OBS", OleDb.OleDbType.VarChar)
cmd.Parameters.Add("@Status", OleDb.OleDbType.Boolean)

cmd.Parameters("@DataRegistro").value = cdate(txtDataRegistro.Text)
cmd.Parameters("@NumReferencia").value = cint(txtMapa.Text)
cmd.Parameters("@Local").value = txtLocal.Text
cmd.Parameters("@Responsavel").value = txtResponsavel.Text
cmd.Parameters("@OBS").value = txtObs.Text
cmd.Parameters("@Status").value = chkStatus.Checked

cmd.ExecuteNonQuery() 

MsgBox("Mapa cadastrado com sucesso!" & vbCrLf & "Nome.: " & txtMapa.Text, MsgBoxStyle.Information)

Note that I defined the types as I imagine they are in your database. I hope it helps you.

Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.