Results 1 to 8 of 8

Thread: Adding a record to MS Access database..

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Adding a record to MS Access database..

    I have a database file that when I click on the "ADD" button should allow me to add a record to the end of the database. The problem is that it DOES add the record, however it also overwrites whatever record I am currently own. So I have two identical records and one missing. It appears that I need some code to go to the end (empty) part of the database or some code that will not overwrite whatever record I am own. This is a local MS Access database by the way.

    What do I need?

    Private Sub btnAdd_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnAdd.Click
    'Begin an Add operation or cancel the current operation

    If btnAdd.Text = "&Cancel" Then 'Cancel an Add or Edit
    LockTextBoxes()
    EnableNavigation()
    btnSave.Enabled = False
    btnAdd.Text = "&Add"
    RejectChanges()
    mblnAdding = False
    Else 'Begin an Add operation

    UnlockTextBoxes()
    ClearText()
    txtAuthor.Focus()
    DisableNavigation()
    btnSave.Enabled = True
    btnAdd.Text = "&Cancel"
    lblRecordNumber.Text = ""
    mblnAdding = True
    End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSave.Click
    'Save the new record for an Add or Edit

    If mblnAdding Then 'Add in progress
    Try
    Dim newRow As DataRow = DsBooks1.Books.NewRow
    newRow("Author") = txtAuthor.Text
    newRow("ISBN") = txtISBN.Text
    newRow("Title") = txtTitle.Text
    DsBooks1.Books.Rows.Add(newRow)
    Catch exc As Exception
    MessageBox.Show("Unable to add the record." & _
    ControlChars.NewLine & exc.Message, "Books")
    End Try
    mblnAdding = False
    lblRecordNumber.Text = "Record Added at the end of the table"
    End If

    'Actions to take to complete an Add or an Edit
    LockTextBoxes()
    EnableNavigation()
    btnSave.Enabled = False
    btnAdd.Text = "&Add"
    mblnIsDirty = True
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I see no code that does the update part, at least your code is not clear enough. As you may know by now adding a new record to the dataset is not all you need. In order to add a new record you need a valid 'insert command' , to update a record you need a valid 'update command' and to delete a record you need a valid 'delete command'. All those commands are sql statments that you may write to perform needed task. However you can do all (adding insert,delete and update records) at desgin time for your ease. However you need a primary key in your data table so that desginer can create those commands for you. If you need more assistant just tell me.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok , I wrote you this sub , it saves your current data to new datarow and add it to the collection and update everything . works fine .
    VB Code:
    1. Public Sub SaveRecords(ByVal MyTable As String)
    2.         Dim conn As New OleDbConnection("connection string here")
    3.         Dim strSQL As String = "SELECT * FROM " & MyTable
    4.         Dim adp As New OleDbDataAdapter(strSQL, conn)
    5.         Dim builder As New OleDbCommandBuilder(adp)
    6.         Dim ds As New DataSet
    7.         adp.Fill(ds, MyTable)
    8.         Dim dr As DataRow = ds.Tables(MyTable).NewRow()
    9.         dr("AUTHOR") = YOURDATA1
    10.         dr("ISBN") = YOURDATA2
    11.         dr("Title") = YOURDATA3
    12.         ds.Tables(MyTable).Rows.Add(dr)
    13.         adp.Update(ds, MyTable)
    14.         MessageBox.Show("Successfully Saved")
    15.     End Sub

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    All what really need to do is just : pass the table name that's being added to , and don't forget to change the YOURDATA1 and .... 2 and ...3 . The connection string also , looks funny , change it too .

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Zip file

    I thought I had posted this earlier. This is the whole project. I copied it from a book. The problem is that when I "ADD" a record, it overwrites the current one that I am on....as well....as adds the new one to the end of the table (as it should).

    Zip file

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Don't bother yourself trying to figure out this problem , use the function I posted if you like .

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    I tried..

    I tried your code. For one thing there should be a prefix on part of it. For example,...

    OleDbDataAdapter should be oledb.OleDbDataAdapter

    I still had a little trouble with it but I am pretty much of a newbie. I am about to take an advanced VB.NET class starting Wednesday so I will figure it then.

    Thanks for the help.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: I tried..

    Put this at the very top of your form . This will shorten declaring variables , so you don't have to type oledb prefix .
    VB Code:
    1. Imports System.Data.OleDb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width