Search code examples
vb.netms-access

Values won't append after INSERT INTO VB.Net


I've been looking around the internet and I still can't find an answer. I have these set of codes which, of course, inserts data to an Access Database. It works fine, however, whenever we add a new data, it doesn't append but it replaces the previous data inserted (if I am making sense). I am not sure what could be wrong or missing.

Here's the full code:

Imports System.Data
Imports System.Data.OleDb
Public Class addRecords

    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\createReacord.accdb")
    Dim i
    Private Sub addrec_Click(sender As Object, e As EventArgs) Handles addrec.Click
        Try
            Dim sql As String
            Dim cmd As New OleDb.OleDbCommand
            con.Open()
            sql = "INSERT INTO record (studno, lname, fname, mname, dept, prog, yrlvl) 
values ('" & studno.Text & "','" & lname.Text & "', '" & fname.Text & "', '" & mname.Text & "', '" & dept.Text & "', '" & prog.Text & "', " & Val(year.Text) & ");"
            cmd.Connection = con
            cmd.CommandText = sql
            i = cmd.ExecuteNonQuery
            If i > 0 Then
                MsgBox("Record saved.")
                studno.Clear()
                lname.Clear()
                fname.Clear()
                mname.Clear()
                dept.ResetText()
                prog.ResetText()
                year.ResetText()

            Else
                MsgBox("No record save.")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Close()
        End Try



    End Sub

    Private Sub dept_SelectedIndexChanged(sender As Object, e As EventArgs) Handles dept.SelectedIndexChanged
        If dept.SelectedIndex = 0 Then
            prog.Items.Clear()
            prog.Items.AddRange({"BMMA", "BSPsych", "BAComm"})

        ElseIf dept.SelectedIndex = 1 Then
            prog.Items.Clear()
            prog.Items.AddRange({"BSIT", "BSCS", "BSIS"})

        ElseIf dept.SelectedIndex = 2 Then
            prog.Items.Clear()
            prog.Items.AddRange({"BSA", "BSBA", "BSTM", "BSEnt"})

        ElseIf dept.SelectedIndex = 3 Then
            prog.Items.Clear()
            prog.Items.AddRange({"BSArch", "BSChE", "BSCE", "BSCpE", "BSEE", "BSEcE", "BSIE", "BSME"})

        ElseIf dept.SelectedIndex = 4 Then
            prog.Items.Clear()
            prog.Items.AddRange({"BSMarE", "BSMT"})


        End If
    End Sub
End Class

EDIT

Okay, so it appends data if it's within the same run. However, if I re-run the project, it will overwrite the previous data. Does that make sense?


Solution

  • What you think is happening is almost certainly not happening. Many people don't know how local data files work in VS and are confused by the apparent behaviour.

    When you add a data file to your project, it is stored in the project folder with the other source files. When you build your project, that source data file is copied to the output folder with your EXE and it is that copy that you work against while debugging. No changes are made to the original.

    By default, the original source file is copied over the top of your test file every time you build your project. That means that any changes you made during previous tests will be lost. If you insert a record, quit the app, make a code change and then run the project again, the data you inserted will be lost, so inserting another record will look like it replaced the previous one.

    The solution is generally to simply change that default behaviour of copying on every build. Select the data file in the Solution Explorer and set the Copy to output directory property to Copy if newer instead of Copy always. That way, your test file will only be overwritten if you have made a change to the source file. You can then keep your changes across multiple debugging sessions. Simply delete the copy in the output folder and rebuild if you want to refresh it without changing the source file, or temporarily change that property back to Copy always.