I'm trying to insert data into Oradores Table in Access using Visual Basic. The code I have is: 'Public Variables novo_nome_comum = TxtNComum.Text novo_nome_completo = TxtNCompleto.Text nova_morada = TxtMorada.Text nova_localidade = TxtLocalidade.Text novo_codpostal = TxtCPostal.Text novo_tel1 = TxtTel1.Text novo_tel2 = TxtTel2.Text novo_tlm1 = TxtTlm1.Text novo_tlm2 = TxtTlm2.Text novo_email1 = TxtMail1.Text novo_email2 = TxtMail2.Text nova_cong = TxtCong.Text 'End of Public Variables
Dim connect As New OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;" & "Data Source =C:\Users\Fernando\Documents\Visual Studio 2012\Projects\Agenda_DP\Agenda_DP\AgendaDP.accdb")
connect.Open()
Dim cmd As OleDbCommand = connect.CreateCommand()
cmd.CommandText = "INSERT INTO Oradores (NomeComum, NomeCompleto, Morada, Localidade, CodPostal, Telefone, Telefone2, Telemovel, Telemovel2, email, email2, Congregacao) VALUES('novo_nome_comum', 'novo_nome_completo', 'nova_morada', 'nova_localidade', 'novo_codpostal', 'novo_tel1', 'novo_tel2','novo_tlm1', 'novo_tlm2', 'novo_email1', 'novo_email2', 'nova_cong')"
cmd.ExecuteNonQuery()
connect.Close()
However, this is not working, and I don't understand why. I searched in MSDN and I'm copying the example quoted to my code. Is there anyone who could give me an hand? Thanks in advance.
Fernando Pessoa
I will share you a piece of code:
Try
Dim con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=yourDB.accdb;")
Dim cb As String = "insert into Table1 (Date1, Sample1) VALUES (@p1, @p2)"
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = con
cmd.CommandText = cb
cmd.Parameters.AddWithValue("@p1", Me.DateTimePicker1.Value.ToShortDateString())
cmd.Parameters.AddWithValue("@p2", Me.TextBox1.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
It is working fine in my sample.
You can visit a post called MS Access - VB .net - connect and operate