FerPessoa
FerPessoa

Reputation: 63

Insert into access database not working

I'm using Visual Studio 2015, Visual Basic Language. I want to INSERT INTO an Access Database a username ('Utilizador' in the code). This is the code I have:

Try
    Dim nconnect As New OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;" & "Data Source =|DataDirectory|S_Campo.accdb")
    nconnect.Open()
    Dim ncmd As OleDbCommand = nconnect.CreateCommand()
    ncmd.CommandText = "INSERT INTO Utilizador (Nome) VALUES (@p1)"
    ncmd.Parameters.AddWithValue("@1", Me.TextBox5.Text) 'Nome Do Utilizador
    ncmd.ExecuteNonQuery()
    nconnect.Close()
    MsgBox("Utilizador lançado com êxito", MsgBoxStyle.OkOnly, "Informação")

Catch ex As Exception
    MessageBox.Show(Err.Description)
End Try

It doesn't return me any error message, but the Data is not sent to the Database. However, UPDATE, and DELETE is working fine, using 'Parameters'.

What can is wrong with it?

Upvotes: 3

Views: 426

Answers (1)

T.S.
T.S.

Reputation: 19386

Seems that you have parameter issue. Here

ncmd.CommandText = "INSERT INTO Utilizador (Nome) VALUES (@p1)"

your parameter is @p1. And here

ncmd.Parameters.AddWithValue("@1", Me.TextBox5.Text) 'Nome Do Utilizador

it is @1

In any case, here how you should check for success

If ncmd.ExecuteNonQuery() > 0 Then
    MessageBox.Show("Success!!")
End If

Upvotes: 2

Related Questions