Getting the ID from a newly entered record

I hope this describes what I am trying to accomplish, on a form I want users to enter information in fields that will be used as header information then enter information in fields that will be detail records and each of the detail records need to have the ID = to the ID of the header record. I created a simple form to try and test this but I need a little help if someone could be so kind it would be most appreciated.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StrangDataSet.Quotes' table. You can move, or remove it, as needed.
Me.QuotesTableAdapter.Fill(Me.StrangDataSet.Quotes)
'TODO: This line of code loads data into the 'StrangDataSet.Quote_Lines' table. You can move, or remove it, as needed.
Me.Quote_LinesTableAdapter.Fill(Me.StrangDataSet.Quote_Lines)
'TODO: This line of code loads data into the 'StrangDataSet.Quotes' table. You can move, or remove it, as needed.
Me.QuotesTableAdapter.Fill(Me.StrangDataSet.Quotes)
'TODO: This line of code loads data into the 'StrangDataSet.Quotes' table. You can move, or remove it, as needed.
Me.QuotesTableAdapter.Fill(Me.StrangDataSet.Quotes)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Header As StrangDataSet.QuotesRow
Header = StrangDataSet.Quotes.NewQuotesRow

Dim Detail As StrangDataSet.Quote_LinesRow
Detail = StrangDataSet.Quote_Lines.NewQuote_LinesRow

'Master Record
Dim vTitles As String
Dim vRef_No As String
Dim vQty As String
Dim vPages As String

vTitles = Title.Text
vRef_No = Ref_No.Text
vQty = QTY.Text
vPages = Pages.Text
Header.Title = vTitles
Header.Ref_No = vRef_No
Header.Qty = vQty
Header.Pages = vPages
StrangDataSet.Quotes.Rows.Add(Header)

'Detail Record
Dim vQuoteID As String
Dim vProduct_No As String
Dim vDescription As String
Dim vUnit As String
Dim vPrice As String

'vQuoteID = How can I get the this number
vProduct_No = Product_No.Text
vDescription = Description.Text
vUnit = Unit.Text
vPrice = Price.Text
Detail.Product_No = vProduct_No
Detail.Description = vDescription
Detail.Unit = vUnit
Detail.Price = vPrice

' How should I modify this to add the Quote_Lines row

Try
Me.Validate()
Me.QuotesBindingSource.EndEdit()
Me.QuotesTableAdapter.Update(Me.StrangDataSet.Quotes)
MsgBox(
"Update successful")

Catch
ex As Exception
MsgBox(
"Update failed")

End Try



Answer this question

Getting the ID from a newly entered record

  • natX

    Thank you, that is exactly what I was looking for.


  • Karin Huber

    hi,

    i don't think this is a good thing to do, the autoincremented field will get its value automaticly if its (AutoNumber in Access , or identity in SQLServer)

    so lets assume your table has 2 columns column1 is identity and column 2 is data

    insert your data in column2 and forget about column1 the database will take care of it

    also in your dataset open your dataset designer and make sure your field is autoincremented and forget about it too

    hope this helps



  • kwa

    From dataset


  • KeniBarwick

    I want to add this ID as a foreign key to another table that has a relationship with the primary one.


  • Jay Luttrell

    From the database or the dataset
  • Eric D.

    hi,

    i don't know where is the primary key and where is the foreign key in your example , so to make it simply you have 2 tables which this is a relationship between them

    table1(table1_idfiled, otherfield)

    table2(table2_idField, Table1_idField4relation, datafield)

    your creat a datarow for each table exactly as what you did , but you simply add a field to keep the master record id

    dim masterkey as integer

    when you assign values to your master field

    masterkey = Header.idField

    Header.otherfield = value

    when you assign values to your detail fields forget about the id field and start with the foreign key

    Detail.Table1_idField4relation = masterkey
    Detail.datafield= value

    but that depends on your idfield , it should be autoincremented field

    hope this helps



  • Carolann

    Is there a way to assign the autoincremented ID of a newly entered record to a variable If so how

    Thanks,

    Tbonehwd


  • Arkiliknam

    i guess you got answer for this in your other thread

    so i will merge both of your threads



  • Getting the ID from a newly entered record