Copy Button (1 Viewer)

kitty77

Registered User.
Local time
Today, 07:12
Joined
May 27, 2019
Messages
712
I have a form and I would like to add a copy button. I would like to copy the contents of a field to the clipboard.
The field is called "Test"

Not sure how to do it...

Thanks.
 

vba_php

Forum Troll
Local time
Today, 06:12
Joined
Oct 6, 2019
Messages
2,880
why can't the user just press "Ctrl+C" on the keyboard when the cursor's inside the field and the text is selected. they can also double click the text inside the field to highlight it, which paves the way for the keyboard shortcut.
 

kitty77

Registered User.
Local time
Today, 07:12
Joined
May 27, 2019
Messages
712
I'm trying to reduce the number of steps. So, a copy button would be perfect. Plus, I'm going to close the form after the copy button is pressed.
So, to answer you question, saving steps...
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:12
Joined
Sep 21, 2011
Messages
14,262
TBH, if all you are doing is copying one control value, just to put in the clipboad to paste elsewhere, Ctrl+C is as quick as anything I would have thought. I'm all for making things more efficient, but even I would use Ctrl+C, rathers than locate a button and press it.

Now, I have used a button in the past, but that was to copy a control value and paste it into another Access DB that we had to use to keep track of the cases we worked, so I completed most of the data in that other DB and the user just had to confirm.?
 

sxschech

Registered User.
Local time
Today, 04:12
Joined
Mar 2, 2010
Messages
792
This version has a few less lines of code.

Put this in the declarations section of the standard module where you will be placing the rest of the code. (After
Option Compare Database and Option Explicit but before any Function/Sub.

Code:
Const DATAOBJECT_BINDING As String = "new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}"  'http://bytecomb.com/copy-and-paste-in-vba/

Then in the same module put the following (one is for copy and the other is for paste):
Code:
Public Sub CopyToClip(ByVal Expression As String)
'http://bytecomb.com/copy-and-paste-in-vba/
'Added doevents since sometimes it doesn't have the
'data to put in the clipboard, so hopefully a pause
'will solve that since when going into debug mode
'and clicking F5 code continues as if nothing was
'wrong
'20200106
    With CreateObject(DATAOBJECT_BINDING)
        .SetText Expression
        DoEvents
        .PutInClipboard
    End With
End Sub
 
Public Function PasteFromClip() As String
'http://bytecomb.com/copy-and-paste-in-vba/
    With CreateObject(DATAOBJECT_BINDING)
        .GetFromClipboard
        PasteFromClip = .GetText
    End With
End Function

In your form code something like this:
Code:
CopyToClip (Me.txtTest)
 

Dreamweaver

Well-known member
Local time
Today, 12:12
Joined
Nov 28, 2005
Messages
2,466
If you check out the link below I do that a lot on the style manager setup form
On my tab at moment but think its something like
Set focus to control to be copied
Docmd.runcommand accmdcopy
Check the link in my signature bottom link

Mick
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:12
Joined
Aug 30, 2003
Messages
36,125
That's what the code in the link I posted looks like.
 

sxschech

Registered User.
Local time
Today, 04:12
Joined
Mar 2, 2010
Messages
792
Something to consider, depending on the application, the code in post #6 can be run without the need to set focus to the object being copied
 

Dreamweaver

Well-known member
Local time
Today, 12:12
Joined
Nov 28, 2005
Messages
2,466
Think I would always go with the simple sollution unless what I was doing needed a more complex sollution

mick
 

Users who are viewing this thread

Top Bottom