Jump to content

Erro de Sintaxe no Insert into do access com o VB2012


HRS
 Share

Recommended Posts

Dim cx As New OleDb.OleDbConnection
	Dim sqlCadTelGravar As String
	Dim cmd As New OleDb.OleDbCommand
	Try
		cx.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Academia.accdb;User ID=admin;").ToString

		cx.Open()
		sqlCadTelGravar = "Insert INTO Aluno ( Aluno,DataNasci,Endereço,Número,Telefone,Celular,E-mail,Aula,Valor,Pago ) VALUES ( " & AlunoTextBox.Text & ", " & MaskedTextBox1.Text & "," & EndereçoTextBox.Text & "," & NúmeroTextBox.Text & "," & MaskedTextBox2.Text & "," & MaskedTextBox3.Text & "," & E_mailTextBox.Text & "," & cbx_Aula.Text & "," & ValorTextBox.Text & "," & PagoCheckBox.Text & ")".ToString
		cmd.CommandText = sqlCadTelGravar
		cmd.Connection = cx
		Dim vCadTelgravar As Integer = cmd.ExecuteNonQuery()
		If vCadTelgravar < 1 Then
			MsgBox("O registo não foi efetuado")
		Else
			MsgBox("Registo efetuado com sucesso")
		End If
	Catch ex As Exception
		MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
	End Try
	cx.Close()
	cmd.Connection = Nothing
	cx = Nothing
End Sub

Quando tento gravar um cadastro aparece erro de sintaxe no insert into

Link to comment
Share on other sites

Boas, duas coisas que devias experimentar, alterar os nomes dos campos que contêm caracteres especiais (ex: ç) e verificar se nos campos que preenches não introduzes nenhum caracter que vá alterar a sintaxe da instrução sql (ex: paço d'arcos, a pelica vai alterar a instrução).

Boa sorte

Férias! Estou por aqui: http://maps.google.p...001549&t=h&z=20 (a bulir claro está!)

Nunca mais é verão outra vez.. :)

Link to comment
Share on other sites

Tenta esse código

Você vai ver que vai funcionar.

Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
 Dim conexao As New OleDb.OleDbConnection
	Dim sqlCadTelGravar As String
	Dim Cm As New OleDb.OleDbCommand(sqlCadTelGravar, conexao)
	Try
		conexao.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\root\Documents\usuariosbd1.accdb;Jet OLEDB:Database Password= ")
		conexao.Open()
		sqlCadTelGravar = "INSERT INTO tbldata (Nome,Senha) VALUES ( ?,?)"
		Cm.CommandText = sqlCadTelGravar
		Cm.Parameters.Add("@nome", OleDb.OleDbType.VarChar).Value = TextBox1.Text
		Cm.Parameters.Add("@senha", OleDb.OleDbType.VarChar).Value = TextBox2.Text
		Cm.Connection = conexao
		Cm.ExecuteNonQuery()
		Dim vCadTelgravar As Integer = Cm.ExecuteNonQuery()
		If vCadTelgravar < 1 Then
			MsgBox("O registo não foi efectuado")
		Else
			MsgBox("Registo efectuado com sucesso")
		End If

	Catch ex As Exception
		MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
	End Try
	conexao.Close()
Edited by bids

Ajude-me?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.