Results 1 to 4 of 4
  1. #1
    Devgo is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    2

    Copy Data from TextBox to ClipBoard

    I want to design a form on access with data that would be displayed in a textbox, i want to create a copy button which when clicked need to copy data from the textbox automatically to the clipboard on the computer. I wrote this code i found online, but it is not working, i cant figure what is wrong, please help me.

    Private Sub btnCopyClip_Click()



    Dim strClipBoard As String
    Set objData = New DataObject

    'put text from a textbox into the clipboard
    strClipBoard = TxtBox.Text
    objData.SetText strClipBoard
    objData.PutInClipboard

    'get the text on the clipboard into a string variable

    objData.GetFromClipboard
    strClipBoard = ""
    strClipBoard = objData.GetText


    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    put this code into a module, then call: CopyToClipboard "string"

    Code:
    Public Const GHND = &H42
    Public Const CF_TEXT = 1
    Public Const MAXSIZE = 4096
        #If VBA7 Then
            Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
            Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
            Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
            Declare PtrSafe Function CloseClipboard Lib "User32" () As Long
            Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
            Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
            Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
            Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
        #Else
            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
        #End If
        
    Sub CopyToClipboard(pvMyString As String)
            #If VBA7 Then
                Dim hGlobalMemory As LongPtr
                Dim hClipMemory   As LongPtr
                Dim lpGlobalMemory    As LongPtr
            #Else
                Dim hGlobalMemory As Long
                Dim hClipMemory   As Long
                Dim lpGlobalMemory    As Long
            #End If
            Dim x                 As Long
            ' Allocate moveable global memory.
           '-------------------------------------------
           hGlobalMemory = GlobalAlloc(GHND, Len(pvMyString) + 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, pvMyString)
            ' Unlock the memory.
           If GlobalUnlock(hGlobalMemory) <> 0 Then
                MsgBox "Could not unlock memory location. Copy aborted."
                GoTo PrepareToClose
            End If
            ' Open the Clipboard to copy data to.
           If OpenClipboard(0&) = 0 Then
                MsgBox "Could not open the Clipboard. Copy aborted."
                Exit Sub
            End If
            ' Clear the Clipboard.
           x = EmptyClipboard()
            ' Copy the data to the Clipboard.
           hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
    PrepareToClose:
            If CloseClipboard() = 0 Then
                MsgBox "Could not close Clipboard."
            End If
    End Sub
    


  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    If this is a one-off (i.e. you're not going to be doing this on multiple Controls)

    Code:
    If Nz(Me.TextboxName, "") <> "" Then
      Me.TextboxName.SetFocus
      Me.TextboxName.SelLength = Len(Me.TextboxName)
      DoCmd.RunCommand acCmdCopy
    Else
      MsgBox "There's Nothing to Copy!"
    End If


    Replacing TextboxName with your actual name.

    Notice that I also idiot-proofed it in case the user hits the button when the target field is empty:

    Linq ;0)>!
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Devgo is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2020
    Posts
    2
    Quote Originally Posted by ranman256 View Post
    put this code into a module, then call: CopyToClipboard "string"

    Code:
    Public Const GHND = &H42
    Public Const CF_TEXT = 1
    Public Const MAXSIZE = 4096
        #If VBA7 Then
            Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
            Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
            Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
            Declare PtrSafe Function CloseClipboard Lib "User32" () As Long
            Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
            Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
            Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
            Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
        #Else
            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
        #End If
        
    Sub CopyToClipboard(pvMyString As String)
            #If VBA7 Then
                Dim hGlobalMemory As LongPtr
                Dim hClipMemory   As LongPtr
                Dim lpGlobalMemory    As LongPtr
            #Else
                Dim hGlobalMemory As Long
                Dim hClipMemory   As Long
                Dim lpGlobalMemory    As Long
            #End If
            Dim x                 As Long
            ' Allocate moveable global memory.
           '-------------------------------------------
           hGlobalMemory = GlobalAlloc(GHND, Len(pvMyString) + 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, pvMyString)
            ' Unlock the memory.
           If GlobalUnlock(hGlobalMemory) <> 0 Then
                MsgBox "Could not unlock memory location. Copy aborted."
                GoTo PrepareToClose
            End If
            ' Open the Clipboard to copy data to.
           If OpenClipboard(0&) = 0 Then
                MsgBox "Could not open the Clipboard. Copy aborted."
                Exit Sub
            End If
            ' Clear the Clipboard.
           x = EmptyClipboard()
            ' Copy the data to the Clipboard.
           hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
    PrepareToClose:
            If CloseClipboard() = 0 Then
                MsgBox "Could not close Clipboard."
            End If
    End Sub
    

    Thanks a lot i will try it

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Copy To Clipboard - Max Length?
    By fredz in forum Access
    Replies: 3
    Last Post: 05-31-2014, 02:03 PM
  2. Copy items in a column to the clipboard
    By kevinscomp in forum Access
    Replies: 1
    Last Post: 08-07-2013, 06:57 PM
  3. Replies: 2
    Last Post: 12-05-2012, 09:39 PM
  4. Replies: 4
    Last Post: 06-21-2012, 11:43 AM
  5. Replies: 8
    Last Post: 05-26-2011, 09:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums