VB.NET with acces database

0

Good afternoon, guys, I'm trying to make a CRUD here using VB.NET and Access, so when I perform an INSERT it returns the error "Syntax Error in INSERT INTO statement". But I can not solve it .... if you can help me right now, I would appreciate it.

con.open ()                 Dim strCmd As String="INSERT INTO Maps (DataRegistration, NumReference, Local, Respondent, OBS, Status)" & _                 "Values (?,?,?,?,?)"                 Dim cmd As OleDbCommand = New OleDbCommand (strCmd, con)                 cmd.Parameters.AddWithValue ("?", txtDataRegistration.Text)                 cmd.Parameters.AddWithValue ("?", txtMap.Text)                 cmd.Parameters.AddWithValue ("?", txtLocal.Text)                 cmd.Parameters.AddWithValue ("?", txtResponsavel.Text)                 cmd.Parameters.AddWithValue ("?", txtObs.Text)                 cmd.Parameters.AddWithValue ("?", chkStatus.Checked)                 cmd.ExecuteNonQuery ()                 MsgBox ("Map successfully registered!" & VbCrLf & "Name:" & txtMap.Text, MsgBoxStyle.Information)

    
asked by anonymous 16.04.2015 / 22:35

1 answer

1

I usually use defined names for each parameter. Try this:

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 set the types as I imagine they are in your database. I hope this helps you.

Hugs!

    
14.05.2015 / 20:34