Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 325
FerPessoa Avatar asked Aug 24 '26 05:08

FerPessoa


1 Answers

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
like image 183
T.S. Avatar answered Aug 26 '26 22:08

T.S.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!