An update, with code. I've been trying to pull snippets of code together to
make them work, and I'm stuck (again) due to my lack of .net and ADO sytax
understanding. This is for part of my original post (how to delete the last
record added to a table).
I have two tables in one database- MonkeyMain and MonkeyFlags. MonkeyMain
stores overall info about an AVI data clip (wild monkey video for research),
and MonkeyFlags stores information about content of the clip (behavior seen,
timestamp, and a foreign key back to the AVI Clip_ID). I'm trying to use
the following code to select the last added MonkeyFlags record and delete it
(in case a flag is added accidently, so the user can just delete it on the
fly). one concern I haven't addressed yet is whether the user can sort the
datatable on the form, and whether that could make this code delete the
wrong row, but I'm not even there yet.
Friend Sub DeleteCurrentFlag()
'I think this is pulling a row from MonkeyMain, where I want it to pull (I
think) from MonkeyFlags
If (Me.MonkeyMainBindingSource.Count > 0) AndAlso
(Me.MonkeyMainBindingSource.Current IsNot Nothing) Then
Dim rowView As DataRowView = CType(Me.MonkeyMainBindingSource.Current,
DataRowView)
'this is where it dies with invalidcastexception saying it can't cast a
monkeymain row to a monkeyflags row
Dim FlagRow As MonkeyMasterTableDataSet.MonkeyFlagsRow = CType(rowView.Row,
MonkeyMasterTableDataSet.MonkeyFlagsRow)
Dim msgText As String = String.Format("Are you sure you want to permanently
delete {0} from your collection? ", title)
'display Yes/No prompt to the user and store the result
Dim result As MsgBoxResult = MsgBox(msgText, MsgBoxStyle.YesNo, "OK to
delete?")
'remove the current movie if the user accepts the prompt
If result = MsgBoxResult.Yes Then
'so if I am linked to MonkeyMain and not MonkeyFlags, then this will delete
a row from the wrong table!
Me.MonkeyMainBindingSource.RemoveCurrent()
End If
' Make sure we always have a row available
If MonkeyMainBindingSource.Count = 0 Then
Me.MonkeyMainBindingSource.AddNew()
End If
End If
End Sub
[quoted text, click to view] "Keith R" <ASpamfilterAddress@NoMail.org> wrote in message
news:%23QoNRcQEHHA.3820@TK2MSFTNGP02.phx.gbl...
> I'm stuck on this in two places of my code, and after struggling and
> searching, I haven't figured it out yet.
>
> I have two linked tables; a field in my master table becomes a linked
> field in my second table (e.g. customerID, second table for customer
> orders uses customerID in each entry). What is the SQL equivalent of
> "currently selected row" of a given table, so I can add the ID to new
> entries?
>
> I worked around this in a third case by adding a data field on a form,
> then converting it back to INT to plug into my new row, but I also need to
> know the "current" row so that I can provide an "oops" button to delete
> the last added row in my child table. I'm assuming that the last added row
> is considered "current" until another record is selected (or added).
>
> Many thanks,
> Keith
>