Delete record in subform from mainform

  • Thread starter Thread starter Russ via AccessMonster.com
  • Start date Start date
R

Russ via AccessMonster.com

How can I delete a record in a subform from a button on the main form?
What do I need to do?
 
Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If
 
Thanks....

Allen said:
Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If
How can I delete a record in a subform from a button on the main form?
What do I need to do?
 
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set a
bookmark, but I didn't see where you reposition the cursor, if that record is
gone.

2) In my particular code, I want to show the user which ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to delete, so
they can confirm. However, with my cursor on Item 2, my code presents Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!" &
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " & nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf &
vbCrLf & _
"Do you want to DELETE THIS ITEM? There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


Allen Browne said:
Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Russ via AccessMonster.com said:
How can I delete a record in a subform from a button on the main form?
What do I need to do?
 
Re #1: I am not seeing this problem in A2003. There is a bug in A2002 SP3
that can cause it to trigger a spurious No Current Record record, so you may
have hit that one.

The point of the form's RecordsetClone is that it can point to a different
record to that in the form. To make it point to the same one, we set its
Bookmark to that of the form. Then when we delete from the clone set, we are
deleting the record displayed in the form. Therefore, we did not use the
Bookmark in order to reposition the record, but in order to *position* it to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would have to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the deletion to get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record to be
deleted, you can read the data from the recordset and create your message
before the delete.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set a
bookmark, but I didn't see where you reposition the cursor, if that record
is
gone.

2) In my particular code, I want to show the user which ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to delete,
so
they can confirm. However, with my cursor on Item 2, my code presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!" &
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf &
vbCrLf & _
"Do you want to DELETE THIS ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


Allen Browne said:
Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

Russ via AccessMonster.com said:
How can I delete a record in a subform from a button on the main form?
What do I need to do?
 
Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read, I -will-
be on the user's record.
--
MichaelM


Allen Browne said:
Re #1: I am not seeing this problem in A2003. There is a bug in A2002 SP3
that can cause it to trigger a spurious No Current Record record, so you may
have hit that one.

The point of the form's RecordsetClone is that it can point to a different
record to that in the form. To make it point to the same one, we set its
Bookmark to that of the form. Then when we delete from the clone set, we are
deleting the record displayed in the form. Therefore, we did not use the
Bookmark in order to reposition the record, but in order to *position* it to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would have to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the deletion to get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record to be
deleted, you can read the data from the recordset and create your message
before the delete.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set a
bookmark, but I didn't see where you reposition the cursor, if that record
is
gone.

2) In my particular code, I want to show the user which ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to delete,
so
they can confirm. However, with my cursor on Item 2, my code presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!" &
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf &
vbCrLf & _
"Do you want to DELETE THIS ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


Allen Browne said:
Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

How can I delete a record in a subform from a button on the main form?
What do I need to do?
 
Yes, Access automatically refers to the fields of the current record.

If you want to see which one that is, make sure the subform's RecordSelector
property is Yes, so that you can see the triangle at the left of the
continuous form pointing to the current record.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read, I -will-
be on the user's record.
--
MichaelM


Allen Browne said:
Re #1: I am not seeing this problem in A2003. There is a bug in A2002 SP3
that can cause it to trigger a spurious No Current Record record, so you
may
have hit that one.

The point of the form's RecordsetClone is that it can point to a
different
record to that in the form. To make it point to the same one, we set its
Bookmark to that of the form. Then when we delete from the clone set, we
are
deleting the record displayed in the form. Therefore, we did not use the
Bookmark in order to reposition the record, but in order to *position* it
to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would have to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the deletion to
get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record to be
deleted, you can read the data from the recordset and create your message
before the delete.

message
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set a
bookmark, but I didn't see where you reposition the cursor, if that
record
is
gone.

2) In my particular code, I want to show the user which
ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to
delete,
so
they can confirm. However, with my cursor on Item 2, my code presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!" &
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf &
vbCrLf & _
"Do you want to DELETE THIS ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select
the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a
New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


:

Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

message
How can I delete a record in a subform from a button on the main
form?
What do I need to do?
 
Thanks, again. I will try these suggestions and report back.
--
MichaelM


Allen Browne said:
Yes, Access automatically refers to the fields of the current record.

If you want to see which one that is, make sure the subform's RecordSelector
property is Yes, so that you can see the triangle at the left of the
continuous form pointing to the current record.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read, I -will-
be on the user's record.
--
MichaelM


Allen Browne said:
Re #1: I am not seeing this problem in A2003. There is a bug in A2002 SP3
that can cause it to trigger a spurious No Current Record record, so you
may
have hit that one.

The point of the form's RecordsetClone is that it can point to a
different
record to that in the form. To make it point to the same one, we set its
Bookmark to that of the form. Then when we delete from the clone set, we
are
deleting the record displayed in the form. Therefore, we did not use the
Bookmark in order to reposition the record, but in order to *position* it
to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would have to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the deletion to
get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record to be
deleted, you can read the data from the recordset and create your message
before the delete.

message
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set a
bookmark, but I didn't see where you reposition the cursor, if that
record
is
gone.

2) In my particular code, I want to show the user which
ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to
delete,
so
they can confirm. However, with my cursor on Item 2, my code presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!" &
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf &
vbCrLf & _
"Do you want to DELETE THIS ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select
the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a
New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


:

Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

message
How can I delete a record in a subform from a button on the main
form?
What do I need to do?
 
So far, it won't take the frm.Setfocus

==============
Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord ThenWith frm.RecordsetClone
.Bookmark = frm.Bookmark
nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item
========================
--
MichaelM


Michael Miller said:
Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read, I -will-
be on the user's record.
--
MichaelM


Allen Browne said:
Re #1: I am not seeing this problem in A2003. There is a bug in A2002 SP3
that can cause it to trigger a spurious No Current Record record, so you may
have hit that one.

The point of the form's RecordsetClone is that it can point to a different
record to that in the form. To make it point to the same one, we set its
Bookmark to that of the form. Then when we delete from the clone set, we are
deleting the record displayed in the form. Therefore, we did not use the
Bookmark in order to reposition the record, but in order to *position* it to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would have to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the deletion to get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record to be
deleted, you can read the data from the recordset and create your message
before the delete.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set a
bookmark, but I didn't see where you reposition the cursor, if that record
is
gone.

2) In my particular code, I want to show the user which ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to delete,
so
they can confirm. However, with my cursor on Item 2, my code presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!" &
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf &
vbCrLf & _
"Do you want to DELETE THIS ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


:

Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

How can I delete a record in a subform from a button on the main form?
What do I need to do?
 
Try:
a) saving any changes in the main form
b) setting focus to the subform control, and
c) setting focus to one of the controls in the subform:

If Me.Dirty Then Me.Dirty = False
Me.[Testing Results Grid].SetFocus
Me.[Testing Results Grid].Form.[SomeControlNameHere].SetFocus

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
So far, it won't take the frm.Setfocus

==============
Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord ThenWith frm.RecordsetClone
.Bookmark = frm.Bookmark
nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item
========================
--
MichaelM


Michael Miller said:
Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read,
I -will-
be on the user's record.
--
MichaelM


Allen Browne said:
Re #1: I am not seeing this problem in A2003. There is a bug in A2002
SP3
that can cause it to trigger a spurious No Current Record record, so
you may
have hit that one.

The point of the form's RecordsetClone is that it can point to a
different
record to that in the form. To make it point to the same one, we set
its
Bookmark to that of the form. Then when we delete from the clone set,
we are
deleting the record displayed in the form. Therefore, we did not use
the
Bookmark in order to reposition the record, but in order to *position*
it to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would have
to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the deletion
to get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record to
be
deleted, you can read the data from the recordset and create your
message
before the delete.

message
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set
a
bookmark, but I didn't see where you reposition the cursor, if that
record
is
gone.

2) In my particular code, I want to show the user which
ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to
delete,
so
they can confirm. However, with my cursor on Item 2, my code
presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the
CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!"
&
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf
&
vbCrLf & _
"Do you want to DELETE THIS ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited
and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select
the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a
New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


:

Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

message
How can I delete a record in a subform from a button on the main
form?
What do I need to do?
 
Thanks againe.
I assume that I am not to use the set frm, clone, and bookmark stuff, then?
Setting focus should take me to the detail form, and when I set focus to one
of the two fields I want, I should already be on the current record and see
the first of my values?

Or, no?
--
MichaelM


Allen Browne said:
Try:
a) saving any changes in the main form
b) setting focus to the subform control, and
c) setting focus to one of the controls in the subform:

If Me.Dirty Then Me.Dirty = False
Me.[Testing Results Grid].SetFocus
Me.[Testing Results Grid].Form.[SomeControlNameHere].SetFocus

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
So far, it won't take the frm.Setfocus

==============
Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
frm.SetFocus
With frm.RecordsetClone
.Bookmark = frm.Bookmark
nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item
========================
--
MichaelM


Michael Miller said:
Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read,
I -will-
be on the user's record.
--
MichaelM


:

Re #1: I am not seeing this problem in A2003. There is a bug in A2002
SP3
that can cause it to trigger a spurious No Current Record record, so
you may
have hit that one.

The point of the form's RecordsetClone is that it can point to a
different
record to that in the form. To make it point to the same one, we set
its
Bookmark to that of the form. Then when we delete from the clone set,
we are
deleting the record displayed in the form. Therefore, we did not use
the
Bookmark in order to reposition the record, but in order to *position*
it to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would have
to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the deletion
to get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record to
be
deleted, you can read the data from the recordset and create your
message
before the delete.

message
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get 'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you set
a
bookmark, but I didn't see where you reposition the cursor, if that
record
is
gone.

2) In my particular code, I want to show the user which
ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to
delete,
so
they can confirm. However, with my cursor on Item 2, my code
presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the
CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!"
&
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem & vbCrLf
&
vbCrLf & _
"Do you want to DELETE THIS ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited
and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select
the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a
New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


:

Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

message
How can I delete a record in a subform from a button on the main
form?
What do I need to do?
 
My preference would still be to delete the record from the RecordsetClone,
because that's very specific.

But if you prefer the other approach, the suggested SetFocus approach should
give you want you want.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
Thanks againe.
I assume that I am not to use the set frm, clone, and bookmark stuff,
then?
Setting focus should take me to the detail form, and when I set focus to
one
of the two fields I want, I should already be on the current record and
see
the first of my values?

Or, no?
--
MichaelM


Allen Browne said:
Try:
a) saving any changes in the main form
b) setting focus to the subform control, and
c) setting focus to one of the controls in the subform:

If Me.Dirty Then Me.Dirty = False
Me.[Testing Results Grid].SetFocus
Me.[Testing Results Grid].Form.[SomeControlNameHere].SetFocus

message
So far, it won't take the frm.Setfocus

==============
Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
frm.SetFocus
With frm.RecordsetClone
.Bookmark = frm.Bookmark
nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item
========================
--
MichaelM


:

Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail
line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read,
I -will-
be on the user's record.
--
MichaelM


:

Re #1: I am not seeing this problem in A2003. There is a bug in
A2002
SP3
that can cause it to trigger a spurious No Current Record record, so
you may
have hit that one.

The point of the form's RecordsetClone is that it can point to a
different
record to that in the form. To make it point to the same one, we set
its
Bookmark to that of the form. Then when we delete from the clone
set,
we are
deleting the record displayed in the form. Therefore, we did not use
the
Bookmark in order to reposition the record, but in order to
*position*
it to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would
have
to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the
deletion
to get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record
to
be
deleted, you can read the data from the recordset and create your
message
before the delete.

message
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get
'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you
set
a
bookmark, but I didn't see where you reposition the cursor, if
that
record
is
gone.

2) In my particular code, I want to show the user which
ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to
delete,
so
they can confirm. However, with my cursor on Item 2, my code
presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the
CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE
LINES!"
&
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem &
vbCrLf
&
vbCrLf & _
"Do you want to DELETE THIS
ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited
and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to
select
the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot
Delete a
New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


:

Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

message
How can I delete a record in a subform from a button on the
main
form?
What do I need to do?
 
Yes. I went back to the Clone setup and found that I had moved some stuff
up, on top of the bookmark, so the current record was wrong. I fixed that
and she seems to be running, and I did delete two records, one after the
other.

My final code (for all our listeners), compacted for space, is:
DoCmd.SetWarnings False
Set frm = Me.[Testing Results Grid].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
frm.[ProjectId].SetFocus
nParentProjectID = .[ParentProjectID]
nProjectID = .[ProjectId]
nItem = .[Item]
Response = MsgBox("This function will DELETE the CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE LINES!" &
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID & vbCrLf & vbCrLf & _
"Project ID: " & nProjectID & ", " &
"CURRENT ITEM: " & nItem & vbCrLf & vbCrLf & _
"Do you want to DELETE THIS ITEM? There
is *NO UNDO*!", vbCritical + vbYesNo, "Are You Sure?")
If Response = vbYes Then
.Delete
End If
End With
Else
MsgBox "The Current Issue is a New line and cannot be deleted."
& vbCrLf & vbCrLf & _
"Please click on the 'row header button' to select the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot Delete a New
Line"
End If
Set frm = Nothing
DoCmd.SetWarnings True

Thanks.
--
MichaelM


Allen Browne said:
My preference would still be to delete the record from the RecordsetClone,
because that's very specific.

But if you prefer the other approach, the suggested SetFocus approach should
give you want you want.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Michael Miller said:
Thanks againe.
I assume that I am not to use the set frm, clone, and bookmark stuff,
then?
Setting focus should take me to the detail form, and when I set focus to
one
of the two fields I want, I should already be on the current record and
see
the first of my values?

Or, no?
--
MichaelM


Allen Browne said:
Try:
a) saving any changes in the main form
b) setting focus to the subform control, and
c) setting focus to one of the controls in the subform:

If Me.Dirty Then Me.Dirty = False
Me.[Testing Results Grid].SetFocus
Me.[Testing Results Grid].Form.[SomeControlNameHere].SetFocus

message
So far, it won't take the frm.Setfocus

==============
Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
frm.SetFocus
With frm.RecordsetClone
.Bookmark = frm.Bookmark
nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item
========================
--
MichaelM


:

Thanks for all that. I will look into it.

My users will have to be on the line that they want to delete in the
subform, and I was when I was trying my first code.

I am not clear that if by using setfocus, how I do know which detail
line
the user is on? I am trying to read fields from the -current- record.

Perhaps, just by setting focus to the first field I want to read,
I -will-
be on the user's record.
--
MichaelM


:

Re #1: I am not seeing this problem in A2003. There is a bug in
A2002
SP3
that can cause it to trigger a spurious No Current Record record, so
you may
have hit that one.

The point of the form's RecordsetClone is that it can point to a
different
record to that in the form. To make it point to the same one, we set
its
Bookmark to that of the form. Then when we delete from the clone
set,
we are
deleting the record displayed in the form. Therefore, we did not use
the
Bookmark in order to reposition the record, but in order to
*position*
it to
the right record to delete.

It is also possible to use:
RunCommand acCmdDeleteRecord
However, that deletes from the form that has focus, so you would
have
to:
a) SetFocus to the Subform control in the main form;
b) SetFocus to a control within the subform;
c) Hope that nothing else takes focus before the delete occurs;
d) Position the cursor back to the main form again after the
deletion
to get
back to where you were.
To me, It seems safer and easier to delete from the explicit form's
RecordsetClone.

Re #2, *after* you set the RecordsetClone's bookmark to the record
to
be
deleted, you can read the data from the recordset and create your
message
before the delete.

message
Access 2002 (XP):
I also tried your suggestion, but encounter a couple of problems.

1) if I delete one record and then try to delete another, I get
'no
current
record'.
Is there some kinda refresh I have to do first. Also, I saw you
set
a
bookmark, but I didn't see where you reposition the cursor, if
that
record
is
gone.

2) In my particular code, I want to show the user which
ParentProjectID,
ProjectID, and Item (the unique lssue number), they are -about- to
delete,
so
they can confirm. However, with my cursor on Item 2, my code
presents
Item 1
(all the time). Code is below.

Thanks.

Private Sub cmdDeleteTRGIssue_Click()
Dim frm As Form
Dim nParentProjectID
Dim nProjectID
Dim nItem
Dim Response

Set frm = Me.[Testing Results Grid].Form
DoCmd.SetWarnings False

If Not frm.NewRecord Then
With frm.RecordsetClone

nParentProjectID = .ParentProjectID
nProjectID = .ProjectId
nItem = .Item

Response = MsgBox("This function will DELETE the
CURRENT
ISSUE LINE, " & vbCrLf & _
"AND its associated CYCLE
LINES!"
&
vbCrLf & vbCrLf & _
"CONFIRM?" & vbCrLf & _
"Parent Project ID: " &
nParentProjectID
& ", Project ID: " & nProjectID & ", CURRENT ITEM: " & nItem &
vbCrLf
&
vbCrLf & _
"Do you want to DELETE THIS
ITEM?
There
is *NO UNDO*.", vbQuestion + vbYesNo, "Are You Sure?")

If Response = vbYes Then
.Bookmark = frm.Bookmark
.Delete
End If

End With

Else
MsgBox "The Current Issue is a New line or being edited
and
cannot be deleted." & vbCrLf & vbCrLf & _
"Please click on the 'row header button' to
select
the
row for deletion, " & crlf & _
"and then press Delete again.", , "Cannot
Delete a
New
Line"

End If

Set frm = Nothing
DoCmd.SetWarnings True

End Sub

--
MichaelM


:

Add a confirmation message if you wish:

Dim frm As Form
Set frm = Me.[NameOfYourSubformControlHere].Form
If Not frm.NewRecord Then
With frm.RecordsetClone
.Bookmark = frm.Bookmark
.Delete
End With
End If

message
How can I delete a record in a subform from a button on the
main
form?
What do I need to do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top