Results 1 to 2 of 2

Thread: How to add a new record to a access 2007 database with vb.net 2010 express

  1. #1
    Junior Member Bertus's Avatar
    Join Date
    Dec 11
    Posts
    25

    How to add a new record to a access 2007 database with vb.net 2010 express

    Hi,
    I wanne add a new record to a access 2007 database with vb.net 2010. I've tried various methods, but cannot get this to work correct.
    my Last code is: "Private Sub cmdAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAccept.Click
    'On Error Resume Next
    PanelHide.Visible = True
    Dim Lopw
    Dim con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & CurDir() & "\SM.accdb;")
    con.Open()
    For Lopw = 1 To (LstBarcode.Items.Count - 1)
    Dim NewJobCardRow As DataRow = SMDataSet7.Tables("JobCard").NewRow()
    NewJobCardRow("JobNumber") = txtJobNo.Text
    NewJobCardRow("JobDescription") = TextBox2.Text
    NewJobCardRow("CustomerBarCode") = TextBox3.Text
    NewJobCardRow("ItemBarCode") = LstBarcode.Items(Lopw).ToString
    NewJobCardRow("ItemDiscription") = LstDescript.Items(Lopw).ToString
    NewJobCardRow("ItemPrice") = Val(LstCost.Items(Lopw).ToString)
    NewJobCardRow("ItemVat") = (Val(LstCost.Items(Lopw).ToString) - (Val(LstCost.Items(Lopw).ToString) * 14 / 100))
    NewJobCardRow("ItemDiscount") = 0
    NewJobCardRow("item1") = "Waiting GO"
    NewJobCardRow("item2") = TextBox3.Text
    NewJobCardRow("Item3") = ""
    NewJobCardRow("Item4") = txtMoreInfo.Text
    NewJobCardRow("Item5") = ""
    NewJobCardRow("Item6") = 0
    NewJobCardRow("item7") = 0
    NewJobCardRow("item8") = Now
    NewJobCardRow("Item9") = 0
    NewJobCardRow("Item10") = 0
    SMDataSet7.Tables("JobCard").Rows.Add(NewJobCardRow)
    Try
    JobCardTableAdapter.Update(SMDataSet7.Tables("JobCard"))
    Catch x As Exception
    End Try
    If LstBarcode.Items.Count = Lopw Then Exit For
    Next
    cmdClose.Enabled = True
    cmdNew.Enabled = True
    End Sub"

    this code ONLY OVERWRITES THE 1st record in the table. and does not add any new records, and does not loop through the for next statement correctly.
    I set up the for next statement, because there might be more than 1 scanned items, the job number, JobDescription etc stays the same, LstBarcode.Items(Lopw).ToString, Val(LstCost.Items(Lopw).ToString), etc is the items that is scanned - I put these in a list control. Each list item is a new scanned item.

    Hope someone might be able to help

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,655

    Re: How to add a new record to a access 2007 database with vb.net 2010 express

    In my sig, there's a link "I swear I saved my data, where'd it run off to?" ... read it, see if it's something that applies in this case.

    That said, I wouldn't use CurrDir() either... there's no telling what it is... if could be the fodler where you app is, or it could be somewhere else... it's affected by the system. User application.StartupPath if you want the folder where the app is runnign from.

    Also, you're looping through something, but all of the rows pull from the same text boxes over and over...which is going to result in multiple rows with the same data in it. -- nevermind, I see where the different data is. Next, after adding the row to your data tabale, DON'T call the update... at least not jsut yet... loop & add your rows first... THEN call the update...

    Lastly... this isn't a good idea:
    Code:
    Try
        JobCardTableAdapter.Update(SMDataSet7.Tables("JobCard"))
    Catch x As Exception
    End Try
    It's possible you're getting errors, but you'll never know. You've decided to stuff your fingers into your ears and yell "NYA NYA NYA" completely ignoring the fact that VB might be trying to tell you there's a problem with your update. But you'll never know unless you take out the Try/Catch, or at least within the Catch display the error. If there is an error happening, it's happening for a reason, don't you think it would be a good idea to figure out why?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

Posting Permissions

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