Copy string to clipboard (1 Viewer)

wodonnell

Registered User.
Local time
Today, 03:36
Joined
Aug 10, 2003
Messages
18
Is there any way to copy a string of text to the clipboard.

I do not want the text to appear on the screen so accmdcopy is no use.

In Excel there is a function called PutToClipboard (or similar). Is there anything like this in access?

Thanks.
 

ghudson

Registered User.
Local time
Yesterday, 22:36
Joined
Jun 8, 2002
Messages
6,195
Assuming your string is the value of a text box [non visible] this will do what you want...
Code:
Me.TextBox.SetFocus
DoCmd.RunCommand acCmdCopy

HTH
 

wodonnell

Registered User.
Local time
Today, 03:36
Joined
Aug 10, 2003
Messages
18
It doesn't!

I just get an error message saying I can't set the focus - presumably because the control is not being displayed.
 

Mile-O

Back once again...
Local time
Today, 03:36
Joined
Dec 10, 2002
Messages
11,316
If you are not displaying a control, how do you expect to set the focus to it?
 

ghudson

Registered User.
Local time
Yesterday, 22:36
Joined
Jun 8, 2002
Messages
6,195
Mile-O-Phile said:
If you are not displaying a control, how do you expect to set the focus to it?
That is a valid question Mile-O. ;)

Code:
Me.TextBox1.Visible = True
Me.TextBox1.SetFocus
DoCmd.RunCommand acCmdCopy
Me.TextBox2.SetFocus 'another text box
Me.TextBox1.Visible = False
 

Mile-O

Back once again...
Local time
Today, 03:36
Joined
Dec 10, 2002
Messages
11,316
Just to be difficult...what if there's only a textbox on the form? :rolleyes:
 

ghudson

Registered User.
Local time
Yesterday, 22:36
Joined
Jun 8, 2002
Messages
6,195
Hopefully there is at least one other object that he can set the focus to so that he can hide the text box that he wants to copy from. If not, spend the extra nickel and create one. :D
 

wodonnell

Registered User.
Local time
Today, 03:36
Joined
Aug 10, 2003
Messages
18
OK, I have decided I can live with having the textbox displayed. This way I will be able to display some other "useful" information at the same time.

I have a form which contains just a textbox (text0) which already contains some text. The textbox is not hidden or disabled or locked.

I have added code to the OnOpen event of the form:

text0.setfocus
docmd.runcommand accmdcopy

My theory was that this would copy the contents of text0 to the clipboard as soon as the form was opened.

When I then try to paste the string into, for example, MS Word I get either:

a) nothing at all, or
b) whatever I had previously copied to the clipboard.

I have tried this on both Access 97 and Access 2000 and neither seemed to work.
 

ghudson

Registered User.
Local time
Yesterday, 22:36
Joined
Jun 8, 2002
Messages
6,195
I think your problem is that the text box is not yet filled with any data when the form first opens. The code is running so fast compared to the bound fields populating with your data. I had a simular problem once. My solution was to use the timer event and one second after the form was opened I "did something". You could try setting the record source of the Text0 text box in the forms OnOpen event and then the rest of your code to set the focus and copy. Or you could use the Sleep API and delay the copy command by one second.

HTH
 

wodonnell

Registered User.
Local time
Today, 03:36
Joined
Aug 10, 2003
Messages
18
It looks like my problem was because the form was a pop-up.

When I make popup=No it seems to work!
 

vicsar

https://about.me/vicsar
Local time
Yesterday, 20:36
Joined
Jul 25, 2012
Messages
35
Here is another Solution

The problem of using the, DoCmd.RunCommand acCmdCopy, statement is that my default setting for the “Behavior Entering Field” property of the database is set to “Go to start of field” and not “Select entire field”, (Options).

The solution is to select the entire field before trying to copy the data to the clipboard.

Code:
me.TheTextBox.SetFocus
me.TheTextBox.SelStart = 0
me.TheTextBox.SelLength = Len(me. TheTextBox)
DoCmd.RunCommand acCmdCopy

Simple, when you know how.

Source:
http://kingsolutions.org.uk/wordpress/tips_tricks-index/how-to-send-copy-information-to-the-clipboard-using-a-button-access-vba-tutorial/
 

sxschech

Registered User.
Local time
Yesterday, 19:36
Joined
Mar 2, 2010
Messages
793
I use this function to copy to the clipboard

Put in a standard module (works in 2010 and 2013, not sure about newer versions)

Code:
Public Sub CopyToClip(ByVal Expression As String)
'http://bytecomb.com/copy-and-paste-in-vba/
    With CreateObject(DATAOBJECT_BINDING)
        .SetText Expression
        .PutInClipboard
    End With
End Sub

Put this in the declarations section of the same module
Code:
Const DATAOBJECT_BINDING As String = "new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}"  'http://bytecomb.com/copy-and-paste-in-vba/

To call it:
Code:
CopyToClip (textstring)

I have used it in an onclick event, (user single clicks on a textbox and it copies the contents which can then be pasted in another app with the paste command.
Mouseup event where user double clicks or highlights the portion of text needed, however, I imagine it can be called from vba without needing user intervention if you want it to refer to a textbox.
 

Gasman

Enthusiastic Amateur
Local time
Today, 03:36
Joined
Sep 21, 2011
Messages
14,336
I used this a few years back.
Needs the Microsoft Forms reference I seem to recall.

Code:
Private Sub cmdCopy_Click()
Dim cb As Object

On Error GoTo Err_Handler

Set cb = New MSForms.DataObject

cb.SetText Me.contact_reference
cb.PutInClipboard

Set cb = Nothing

Exit_Handler:
    Exit Sub
    
Err_Handler:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_Handler
End Sub
 

Users who are viewing this thread

Top Bottom