Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

SelectAll and Copy through Code

I'm making a reference system for source code. By the user selecting a description in the main form undates showing the code required. I want a button that will selectAll the text in the control and automatically copy to the clipboard..
I've tried  DoCmd.RunCommand acCmdSelectAll but get an error..

Any help??

Scottsanpedro
0
scottsanpedro
Asked:
scottsanpedro
  • 2
1 Solution
 
paaskyCommented:
Hello scottsanpedro,

This function selects all text of Text1 textbox:

Private Sub B_SelectAll_Click()
    With Text1
        .SetFocus
        If Len(.Value) > 0 Then
             ' select all text
             .SelStart = 1
             .SelLength = Len(Text1)

            ....
            ....

        End If
    End With
End Sub

Regards,
Paasky
0
 
DedushkaCommented:
scottsanpedro,
to copy text automatically to clipboard you can use this function from Acc2000 Developer Code Librarian:
____________________________
     Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
         As Long
      Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
         As Long
      Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
         ByVal dwBytes As Long) As Long
      Declare Function CloseClipboard Lib "User32" () As Long
      Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
         As Long
      Declare Function EmptyClipboard Lib "User32" () As Long
      Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
         ByVal lpString2 As Any) As Long
      Declare Function SetClipboardData Lib "User32" (ByVal wFormat _
         As Long, ByVal hMem As Long) As Long

      Public Const GHND = &H42
      Public Const CF_TEXT = 1
      Public Const MAXSIZE = 4096



      Function ClipBoard_SetData(MyString As String)
         Dim hGlobalMemory As Long, lpGlobalMemory As Long
         Dim hClipMemory As Long, X As Long

         ' Allocate moveable global memory.
         '-------------------------------------------
         hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)

         ' Lock the block to get a far pointer
         ' to this memory.
         lpGlobalMemory = GlobalLock(hGlobalMemory)

         ' Copy the string to this global memory.
         lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

         ' Unlock the memory.
         If GlobalUnlock(hGlobalMemory) <> 0 Then
            MsgBox "Could not unlock memory location. Copy aborted."
            GoTo OutOfHere2
         End If

         ' Open the Clipboard to copy data to.
         If OpenClipboard(0&) = 0 Then
            MsgBox "Could not open the Clipboard. Copy aborted."
            Exit Function
         End If

         ' Clear the Clipboard.
         X = EmptyClipboard()

         ' Copy the data to the Clipboard.
         hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

      OutOfHere2:

         If CloseClipboard() = 0 Then
            MsgBox "Could not close Clipboard."
         End If

         End Function
________________________

Just call it from your button's OnClick event:

ClipBoard_SetData(Me.[txtSourceCode])

Best regards,
Dedushka
0
 
paaskyCommented:
Dedushka, cool!
0
 
scottsanpedroAuthor Commented:
Dedushka,

Didn't quite imagine it would be this big but it works perfectly..

Thanks very much

Scott
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now