Results 1 to 9 of 9
  1. #1
    cbuechner is offline Advanced Beginner
    Windows 10 Access 2007
    Join Date
    Jan 2021
    Posts
    60

    Paste append

    I am adding a paste append in the form, and it works. But I need to add a part that if there is nothing to append, it doesn't create a run time error.



    I don't know how how to write the code.

    This is what I have

    Private Sub Command14_Click()
    DoCmd.RunCommand acCmdPasteAppend
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Always better to prevent errors where possible but I don't know how to test for an empty clipboard. I suspect you could trap the error easily enough and just ignore it if it's a vba generated error.

    EDIT - so there are probably more than 2 methods that I found. One is to add Forms2.0 library to your references, which can be an issue if you're distributing a front end. The other is an API call, which you don't have to write as if it were for the current version of Access because you're using 2007. However, if you upgrade or have users with different versions, code must/should be written using PtrSafe or else it will fail in newer versions. Still thinking that trapping the error is the way to go. If you're pasting records, append query or append type of code is safer and better. The downside to pasting is that user actions might interfere with the clipboard, even to the point where you end up trying to paste a picture, or as you have found, nothing.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    cbuechner is offline Advanced Beginner
    Windows 10 Access 2007
    Join Date
    Jan 2021
    Posts
    60
    Can it be ignore it if its a VBA generated error for just this error?

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Please test what error number/description occurs so we can tell you how to do a work-round
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Quote Originally Posted by isladogs View Post
    Please test what error number/description occurs so we can tell you how to do a work-round
    Probably 4605 but would sure be nice if everybody divulged these numbers.

    One way to trap
    Code:
    Private Sub Command14_Click()
    On Error GoTo errHandler
    
    DoCmd.RunCommand acCmdPasteAppend
    exitHere:
    Exit Sub
    
    errHandler:
    If Err.Number = 4605 Then 
     Msgbox "Nothing to paste append."
    Else
     Msgbox "Error " & Err.Number & ": " & Err.Description
    End If
    Resume exitHere
    
    End Sub
    The exitHere label isn't really needed in this case as the error handler could be so simple as to disregard the error using Resume Next or presenting a message for any other error and then just End Sub. However, I did it this way to show a way it should be done if you need to guarantee warnings are turned back on if they were turned off, plus a spot for destroying objects you created. Both of these things would be done in the exitHere block before the Exit Sub (or function) line.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    cbuechner is offline Advanced Beginner
    Windows 10 Access 2007
    Join Date
    Jan 2021
    Posts
    60
    run time error 2237

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Same code. Just change the number.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    cbuechner is offline Advanced Beginner
    Windows 10 Access 2007
    Join Date
    Jan 2021
    Posts
    60
    Ok running into a new problem. My form requery. This may be causing some problem with that code.

    Private Sub Combo23_AfterUpdate()
    Me.Requery
    End Sub
    Private Sub Command11_Click()
    DoCmd.Maximize
    End Sub
    Private Sub Command15_Click()
    On Error GoTo errHandler


    DoCmd.RunCommand acCmdPasteAppend
    exitHere:
    Exit Sub


    errHandler:
    If Err.Number = 2237 Then
    MsgBox "Nothing to paste append."
    Else
    MsgBox "Error " & Err.Number & ": " & Err.Description
    End If
    Resume exitHere


    End Sub

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    We're supposed to guess what the "some problem" is?
    Please use code tags (# on forum post toolbar) and indentation for code. I see nothing wrong with what you posted. The requery shouldn't matter unless you've got code somewhere else (e.g. form Current event) that is affecting something.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 02-03-2021, 08:32 AM
  2. Replies: 19
    Last Post: 10-03-2017, 09:10 AM
  3. Missing Field data on paste append
    By lacigol in forum Programming
    Replies: 4
    Last Post: 08-10-2017, 01:13 PM
  4. Access can't append all the records in the append query
    By fluffyvampirekitten in forum Access
    Replies: 2
    Last Post: 08-27-2015, 01:53 AM
  5. Paste Append not working
    By martinshort in forum Access
    Replies: 3
    Last Post: 01-20-2012, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums