Avatar of SteveL13
SteveL13
Flag for United States of America asked on

Copy - Paste

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?
Microsoft Access

Avatar of undefined
Last Comment
Gustav Brock

8/22/2022 - Mon
Scott McDaniel (EE MVE )

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 Prew

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
SteveL13

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Bill Prew

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 Prew

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
Gustav Brock

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SteveL13

ASKER
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 Prew

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


»bp
Bill Prew

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SteveL13

ASKER
Windows 10.  Access 365.  

Ran it.  Got no error.  But got nothing in the clipboard.
Gustav Brock

Well, use my simple commands. That works right away.
Bill Prew

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Bill Prew

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SteveL13

ASKER
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

ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.