Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy selected text from a text field to the Clipboard

Status
Not open for further replies.

Accel45

Technical User
Jul 7, 2004
83
US
I have a form that contains an unbound text field "txtGet" on one side and several other bound text fields on the other side.
What I am trying to accomplish is this:
I paste into txtGet a paragraph from another source. I then select key words from the paragraph and paste the word(s) into the appropriate bound text fields. This takes several mouse clicks (select, right click, copy, move to field, right click, paste)
I want to be able to select word(s) from txtGet then click a command button that will place the selected word(s) into the appropriate bound text field.
I have been trying the docmd.runcommand.accmdcopy and docmd.runcommand.accmdpaste but I can't get figure out how to copy only the selected text to the clipboard.
(The docmd.runcommand.accmdpaste I have figured out but I am stuck on getting only selected text to the clipboard)

How do I accomplish this?

accel45

 
Force the focus to the appropriate textbox before running your code:
Code:
Me![txtGet].SetFocus
DoCmd.RunCommand acCmdCopy
Me![txtOther].SetFocus
DoCmd.RunCommand acCmdPaste

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Only way I can think of, at the top of my head, to make this work, is to use the forms keydown event to catch a hotkey (here using Alt+A), then issue the docmd there:

[tt]if screen.activecontrol.name = "txtGet" then
if (shift and acaltmask <> 0) then
if (keycode = vbkeya) then
docmd.runcommand accmdcopy
end if
end if
end if[/tt]

- testing also whether it is the correct control...

Remember setting the forms KeyPreview property to yes.

I think that when a command button is pressed, the selection in the first controls becomes deselected.

Then you could for instance use the doubleclick event of the text controls to drop in the values from the clipboard (accmdpaste)

Roy-Vidar
 
hmpf - it also works in the keydown event of the control too (probably more events too), but then one should perhaps cosider ctrl+c/ctrl+v;-)

Roy-Vidar
 
You can capture the selected text using the Exit event for the source textbox, then re-select it on focus:
Code:
[green]'module-level variables[/green]
Private mlngStart As Long
Private mlngLength As Long

Private Sub cmdPaste_Click()
On Error GoTo ErrHandler
  
  With txtGet
    .SetFocus
    If mlngLength > 0 Then
      
      .SelStart = mlngStart
      .SelLength = mlngLength
      
      RunCommand acCmdCopy
      
      txtPut.SetFocus

      RunCommand acCmdPaste
    End If
  End With

ExitHere:
  Exit Sub
ErrHandler:
  Debug.Print Err, Err.Description
  Resume ExitHere
End Sub

Private Sub txtGet_Exit(Cancel As Integer)
  mlngStart = txtGet.SelStart
  mlngLength = txtGet.SelLength
End Sub

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I am using 97 and I am getting an error when I try the runcommand: 2046 the command of action 'copy isn't available now' I just want it to copy the text in the text box, and also be able to check if the textbox is empty... any help is much appreciated!
 
Thank you all for your suggestions. But I have not been able to make any of them work for selecting some words from txtGet. Each of the suggestions copies all words in txtGet.

I did find that if I place Docmd.Runcommnad acCmdCopy with no other code in the On Exit of txtGet that whatever is selected in txtGet is place on the clipboard.
The problem I haven't been able to solve is that if nothing is selected in txtGet and I exit txtGet I get the this error message: Run-time error '2046': The Command or action 'Copy' isn't available now.

Any more suggestions would be appreciated.

accel45
 
The example I posted was tested on Access 2000 and worked fine. You'll notice that I checked for a selection before performing the copy:
Code:
With txtGet
    .SetFocus
    [green]'check the module-level variable for a
    'selection length before proceeding[/green]
    If mlngLength > 0 Then
      
      [green]'use the module-level variables to set the
      'SelStart and SelLength properties[/green]
      .SelStart = mlngStart
      .SelLength = mlngLength
     
      [green]'now perform the copy / paste[/green]
      RunCommand acCmdCopy      
      txtPut.SetFocus
      RunCommand acCmdPaste
    End If
  End With


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Sorry VBSlammer, but I wasn't able to get it to work for me. I too am using Access 2000.

Thank you for your time.
accel45
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top