We help IT Professionals succeed at work.

Copy - Paste

SteveL13
SteveL13 asked
on
56 Views
Last Modified: 2018-02-21
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

Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
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 PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
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.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
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 PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
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
CERTIFIED EXPERT
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 PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

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


»bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
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.
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Well, use my simple commands. That works right away.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
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 PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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

CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION