Copy - Paste

SteveL13
SteveL13 used Ask the Experts™
on
Is there a way to single-click a text box on a form, have the contents of that textbox copied to the clipboard (in the background), and then be able to [Right-Click]-[Paste] it into another textbox?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
The simplest way is to use keyboard shortcuts:

Ctrl+C will copy it

Ctlr + V will paste it

If you want to use code you could, but you aren't going to really save any keystrokes or mouseclicks.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
For people that are comfortable with the Windows GUI standards, it doesn't seem like it would be intuitive that if they just click on a text box the content would go to the clipboard.  You could create a custom trigger on the field to trap for mouse clicks and place something in the clipboard, but it feels less than obvious, and you would have to worry about "dirty" content in the field, meaning they typed something, but haven't left the field, so it isn't in the .Value of that control yet.  Etc...

If you really need this on just a few fields, I'd probably put a small button just to the right of the text field, with an icon of label that indicates "copy".  Then if they click that button, just take the value of the text field and place it in the clipboard.  Feels more user friendly, IMHO.


»bp

Author

Commented:
The field I want to copy would be locked so it could not be edited.  But I do want to single click it to copy the contents to the clipboard and then [Right-Click]-[Paste] it to another unlocked textbox.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
As long as the control is LOCKED=TRUE and not ENABLED=FALSE, then something like this should get you started.

I haven't figured out how to stop the normal even handler from firing after this trigger runs, working on that...

Private Sub Text0_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Button = 2 Then
        With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
            If IsNull(Text0.Value) Then
                .SetText ""
                .PutInClipboard
            Else
                .SetText Text0.Value
                .PutInClipboard
            End If
        End With
    End If

End Sub

Open in new window


»bp
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Okay, looks like we can do it this way (Up event rather than Down).

Private Sub Text0_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Button = 2 Then
        With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
            If IsNull(Text0.Value) Then
                .SetText ""
                .PutInClipboard
            Else
                .SetText Text0.Value
                .PutInClipboard
            End If
        End With
        DoCmd.CancelEvent
    End If

End Sub

Open in new window


»bp
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You may be able to use the simple commands:

' Copy selected text or object.
DoCmd.RunCommand acCmdCopy
' Paste a copied text or object.
DoCmd.RunCommand acCmdPaste

Open in new window

Author

Commented:
Bill:  This does not work:

Private Sub txtDescription_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Button = 2 Then
        With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
            If IsNull(txtDescription.Value) Then
                .SetText ""
                .PutInClipboard
            Else
                .SetText txtDescription.Value
                .PutInClipboard
            End If
        End With
        DoCmd.CancelEvent
    End If

End Sub

Open in new window

Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
What versions of Windows and Access are you running, and which are 32 bit versus 64 bit?


»bp
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
And what does "this does not work" mean?  Did you get an error?  Or did it run and nothing showed in clipboard?  Or other?


»bp

Author

Commented:
Windows 10.  Access 365.  

Ran it.  Got no error.  But got nothing in the clipboard.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Well, use my simple commands. That works right away.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
So this should handle the copy part of things, try it there.

As far as the past goes, how would that logic work?  On this first text box, right click always copies?  But on one other specific text box right click always pastes?

Private Sub Text0_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Button = 2 Then
        If Not IsNull(Text0.Value) Then
            DoCmd.RunCommand acCmdCopy
        End If
        DoCmd.CancelEvent
    End If

End Sub

Open in new window


»bp
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
If you always want to paste into a second control on right click, then try:

Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Button = 2 Then
        DoCmd.RunCommand acCmdPaste
        DoCmd.CancelEvent
    End If

End Sub

Open in new window


»bp

Author

Commented:
Gustav, I put this code in the onclick event of the textbox.  When I single click it the text does not get copied to the clipboard.

Bill.  This doesn't put anything on the clipboard either.  No error.  Nothing.

    If Button = 2 Then
        If Not IsNull(txtDescription.Value) Then
            DoCmd.RunCommand acCmdCopy
        End If
        DoCmd.CancelEvent
    End If

Open in new window

Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
Gustav, I put this code in the onclick event of the textbox.  
When I single click it the text does not get copied to the clipboard.

That's because when clicking you are resetting a select.
You must select something to copy. If that is the content of the textbox, modify your code to select the content, then copy:

Private Sub Felt1_Click()
    
    Me!Felt1.SelStart = 0
    Me!Felt1.SelLength = Len(Me!Felt1.Text)
    DoCmd.RunCommand acCmdCopy

End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial