I'm writing my first application in VB and need some help with my Access database. I have 2 problems; the first one is the most important, the second I would just be very grateful for any help/advice.
Firstly, I need help updating a record from my database. I have some code which is what I used for inserting a record but it doesn't work. What am I missing?
Dim conStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=School1.accdb" Dim con As New OleDbConnection(conStr) Dim cmd As New OleDbCommand("select [username] from [Users] where [username]=@username", con) cmd.Parameters.AddWithValue("@username", teachersList.SelectedItem) con.Open() Dim obj As Object = cmd.ExecuteScalar() con.Close() If IsNothing(obj) Then Try cmd = New OleDbCommand("UPDATE [Users] SET [Type]=@type WHERE [Username]=@username", con) 'Add new user with following parameters Dim a As Object a = ListBox.SelectedItem cmd.Parameters.AddWithValue("@username", a) cmd.Parameters.AddWithValue("@type", "Admin") 'Open database connection con.Open() 'Execute query cmd.ExecuteScalar() 'Close database connection con.Close() MsgBox("Changed") Catch MsgBox("NOT changed") End Try
Secondly, when I insert a new record, it adds one for that session, but when I restart the application, the new row is gone. I wasn't sure if there was a way around this and would be very grateful of any help.