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
Comment Utility
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

Comment Utility
Thanks, that worked GREAT!

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now