Copy and Paste Action in a Form

  • Thread starter Thread starter TPG
  • Start date Start date
T

TPG

Hi Everyone

What would be the code for copying (or Reading) the contents of an Access
field on a form, (or fields) into Memory, for pasting into another
Application.

Basicall the Actions are:-
Select the Entire Contents of a field
Control C (To Read Into Memory)

And I want to do that with a click of a button.

Many Thanks in Anticipation

Centaur
 
Centaur,

This code is NOT mine. I acquired it along my travels, but it may do the
trick. Full credit to whoever the author was. Use the sample code at the
bottom in the click event of your button.

Option Compare Database
Option Explicit

' Obtaining Clipboard Data using API
' (needed for VBA since no Clipboard Object) -- can also paste the data to a
control
' *************************************************************

Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As
Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes As
Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal
lpString2 As Any) As Long

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

' ************* Create the following function **************
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. It may be in use by another
application."
Exit Function
End If

' Obtain the handle to the global memory
' block that is referencing the text.
hClipMemory = GetClipboardData(CF_TEXT)
If IsNull(hClipMemory) Then
MsgBox "Could not allocate memory"
GoTo OutOfHere
End If

' Lock Clipboard memory so we can reference
' the actual data string.
lpClipMemory = GlobalLock(hClipMemory)

If Not IsNull(lpClipMemory) Then

MyString = Space$(MAXSIZE)
RetVal = lstrcpy(MyString, lpClipMemory)
RetVal = GlobalUnlock(hClipMemory)

' Peel off the null terminating character.
MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
Else
MsgBox "Could not lock memory to copy string from."
End If

OutOfHere:

RetVal = CloseClipboard()
ClipBoard_GetData = MyString

End Function

' *************** Simple procedure the Test the code *****************
Private Sub Main()
MsgBox ClipBoard_GetData()
End Sub

Jamie
 
Centaur

Like this...

Private Sub YourButton_Click()
Me.YourTextbox.SetFocus
DoCmd.RunCommand acCmdCopy
End Sub
 
Back
Top