Cut to/paste from Windows clipboard

Posted on 1998-07-16
Last Modified: 2012-04-03
I have an MS Access form that I use for data entry.  I'd like to have two buttons on the form.  One would grab the contents of a certain textfield when pressed and put the string on the clipboard.  The other would do the opposite, copy the contents of the clibboard and paste it into a textfield on the form.
Question by:jhance

Accepted Solution

Helicopter earned 50 total points
ID: 1976807
Have a form with 2 textboxes and 2 command buttons

Private Sub Command4_Click()
Dim x As String
x = ClipBoard_SetData(Text0) 'puts contents of text0 into clipboard

End Sub

Private Sub Command5_Click()
Text2 = ClipBoard_GetData 'puts clipboard into text2
End Sub

You need a module with these declares and functions

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
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

Function ClipBoard_GetData()

         Dim hClipMemory As Long
         Dim lpClipMemory As Long
         Dim MyString As String
         Dim RetVal As Long

         If OpenClipboard(0&) = 0 Then
            MsgBox "Cannot open Clipboard. Another app. may have it open"
            Exit Function
         End If
         hClipMemory = GetClipboardData(CF_TEXT)
         If IsNull(hClipMemory) Then
            MsgBox "Could not allocate memory"
            GoTo OutOfHere
         End If

         lpClipMemory = GlobalLock(hClipMemory)

         If Not IsNull(lpClipMemory) Then
            MyString = Space$(MAXSIZE)
            RetVal = lstrcpy(MyString, lpClipMemory)
            RetVal = GlobalUnlock(hClipMemory)

            MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
            MsgBox "Could not lock memory to copy string from."
         End If


         RetVal = CloseClipboard()
         ClipBoard_GetData = MyString

      End Function

 Function ClipBoard_SetData(MyString As String)

         Dim hGlobalMemory As Long, lpGlobalMemory As Long
         Dim hClipMemory As Long, x As Long

         hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)
         lpGlobalMemory = GlobalLock(hGlobalMemory)

         lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

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

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

         x = EmptyClipboard()

         hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)


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

         End Function

(look for clipboard in MSDN if you get stuck)
LVL 32

Author Comment

ID: 1976808
Thanks, that worked GREAT!

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In the previous article, Using a Critera Form to Filter Records (, the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question