Results 1 to 6 of 6
  1. #1
    Luchino is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    11

    Question acCommandCopy in Datasheet only copies first record

    Hello friends



    My setup:
    Table called "Contacts" with fields "ID" and "Name".

    I am trying to use acCmdCopy to copy the value in "ID" when I click it. This is my code:

    Code:
    Private Sub ID_Click()
    Me!ID.SetFocus
    Me.Requery
    DoCmd.RunCommand acCmdCopy
    End Sub
    However, regardless of the record I click, it copies the first value on the datasheet. For example, when the I sort "ID" by ascending order, if I click record 10, it will copy record 1 because it appears first in the datasheet. In descending order, if I click record 1, it will copy record 10, because it appears first in the datasheet.

    I want to copy the record I click to the clipboard - where am I going wrong?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    When you do this
    Me.Requery

    You are telling the form to take a trip to the data and it will navigate to the first Record in the Form's Recordset.

    You are going to need to select a record if you want to copy the entire record and not just a single field. I do not think there is a way to use the SetFocus method on a record. You can use this for controls like a form or a textbox, etc. I think you are going to need to find the ordinal position of the record. The ordinal position within the Form's Recordset and use that value in a GoToRecord statement. Actually it would probably be best to use the Bookmark method od a DAO recordset to set focus and then the acCopy ...

    GoTo Record
    Code:
    Dim intRecordPosition as integer
    'Need the ordinal postion. Here I will guess it is five
    intRecordPosition = 5
    DoCmd.GoToRecord acDataForm, Me.Name, acGoTo, intRecordPosition
    
    '   https://msdn.microsoft.com/en-us/lib.../ff194117.aspx
    Or what will probably be better is to use Bookmark to set focus on the record.
    Code:
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
        'rs.FindFirst "[ID] = '" & Me!ID & "'"    'for text field
        rs.FindFirst "[ID] = " & Me!ID             'for number field
              If rs.Bookmarkable = False Then
              MsgBox "Can't bookmark this record"
              Else
              Me.Bookmark = rs.Bookmark
              End If
    Set rs = Nothing
    
    '   https://msdn.microsoft.com/en-us/lib.../ff823084.aspx

  3. #3
    Luchino is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    11
    Thanks ItsMe!

    What I wanted to do is in fact select a field, not a record - sorry! Basically, when I click "ID", I want the code to copy the ID value to the clipboard.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Directly behind the form that has the field you want to copy ...
    Code:
    Private Sub ID_Click()
    Me.ID.SetFocus
    DoCmd.RunCommand acCmdCopy
    End Sub
    Having said that, you may need to add additional code to make sure all of the text within the control is selected.


    .

  5. #5
    Luchino is offline Novice
    Windows 10 Access 2013
    Join Date
    Dec 2015
    Posts
    11
    Thanks again!
    This was the first thing that I tried, but it gives me the error:

    Run-time error '2046':
    The command or action 'Copy' isn't available now.

    To resolve this, I came here, found a thread, and that's why I ended up putting in the "Me.Requery"

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    OK, no problem, it is not available because the control is busy or there is not anything selected to copy. Try placing focus on another control and then returning to the original control.

    Me.SomeotherControlName.SetFocus
    Me.ID.SetFocus
    DoCmd.RunCommand acCmdCopy

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

Similar Threads

  1. Delete Record from Datasheet
    By CementCarver in forum Programming
    Replies: 9
    Last Post: 09-22-2013, 04:57 PM
  2. Replies: 3
    Last Post: 08-29-2013, 02:36 PM
  3. Replies: 1
    Last Post: 03-13-2013, 10:37 PM
  4. Replies: 8
    Last Post: 08-26-2012, 11:11 PM
  5. Replies: 5
    Last Post: 11-03-2011, 08:53 PM

Tags for this Thread

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