Results 1 to 9 of 9
  1. #1
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete record frmo subform (XP)

    Hi, I have searched around, but found that what I am doing should be wokring - i think ?

    I have a button on the main form, which I want to use to delete the selected record in the subform, just the same as clicking on the 'delete record' button on the toolbar.

    It works to a point, which is to say the record disappears, and the built in confirmation box comes up from access. But when I click yes, then i get the following error!

    Run time error 3021, No current record

    Hmm!

    this is the code attached to the button. The message box works fine and picks up the correct info from the selected record.

    <font color=blue>With Me.LinkTag_Subform
    If MsgBox("Remove " & ![Title] & " " & ![FirstName] & " " & ![Surname] & _
    ", are you sure?", vbYesNo + vbQuestion, "Remove...") = vbYes Then
    .Form.AllowDeletions = True
    .SetFocus
    DoCmd.RunCommand acCmdDeleteRecord
    .Form.AllowDeletions = False
    End If
    End With</font color=blue>

    Any thoughts? Am i missing something SILLY?
    Thanks,

    pmatz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete record frmo subform (XP)

    Perhaps you must also set focus to a control in the subform:
    ...
    .SetFocus
    !SomeControl.SetFocus
    RunCommand acCmdDeleteRecord
    ...

    (you do not need DoCmd before RunCommand)

    Or else put the delete button on the subform itself (won't work for a datasheet form).

    What is the objection against the toolbar button, by the way?

  3. #3
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete record frmo subform (XP)

    Hmm, strange, still get the error, even after setting focus to a filed on the subform.

    No objection to the delet record toolbar button, just wanted to add a button on the form, to compliment the 'Add Contact' button!

    If when i get the error I click END, then the record is deleted fine, strange. Its like ACCESS is trying to delete the record twice??
    Thanks,

    pmatz

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete record frmo subform (XP)

    I just tested it in my all-purpose test database, and it worked without problems. So there must be something else interfering in your database, but it's impossible for me to guess what it could be.

  5. #5
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete record frmo subform (XP)

    Ok Hans, thanks for that - I like the 'all-purpose test database' [img]/forums/images/smilies/smile.gif[/img]

    well, the only thing i can think is that some kind of relationship between tables is being affected, i will have to look into it, perhaps reconstruct the controls etc. Will let you know.
    Thanks,

    pmatz

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete record frmo subform (XP)

    If you can't solve it, you might consider posting a stripped down copy of the database. (See <post#=393395>post 393395</post#> for instructions.)

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete record frmo subform (XP)

    Here is an alternative that deletes directly from the recordset of the subform:

    Sub cmdDelete_Click()
    On Error GoTo ErrHandler

    With Me.LinkTag_Subform
    If MsgBox("Remove " & !<!t>[Title]<!/t> & " " & !<!t>[FirstName]<!/t> & " " & !<!t>[Surname]<!/t> & _
    ", are you sure?", vbYesNo + vbQuestion, "Remove...") = vbYes Then
    .Form.Recordset.Delete
    End If
    End With

    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    End Sub

  8. #8
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete record frmo subform (XP)

    Thanks Hans, this alternative deletion method works fine, I will keep this. I didn't realise you could delete the current record in this way.

    Form.Recordset refers to the selected record. Thats cool [img]/forums/images/smilies/smile.gif[/img]
    Thanks,

    pmatz

  9. #9
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete record frmo subform (XP)

    Strange, the runcommand works fine here at work. I will check the references when i get home and see if something is different. Must be something to do with my Access set up at home.
    Thanks,

    pmatz

Posting Permissions

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