'Ms access VBA copy form data
I'm currently making a ms access database and I have made a form where the user inputs data. I would like the user to be able to press a button which copies the label and the entered data so they can paste it elsewhere. I have found a project which achieves exactly what I want however I cannot seem to get it to work for my application. The code below is what I found online and this is the link to the thread. It is the one labeled copypaste.zip https://www.access-programmers.co.uk/forums/threads/copy-all-date-on-form-to-clipboard-to-user-can-past-this-into-another-system.309872/ .Thank you.
This is on the module code:
Option Compare Database
Option Explicit
Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare Function CloseClipboard Lib "user32.dll" () As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32.dll" (ByVal wFormat As Long) As Long
Private Declare Function GetClipboardData Lib "user32.dll" (ByVal wFormat As Long) As Long
Private Declare Function SetClipboardData Lib "user32.dll" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1
As Long, ByVal lpString2 As Long) As Long
Public Sub SetClipboard(sUniText As String)
Dim iStrPtr As Long
Dim iLen As Long
Dim iLock As Long
Const GMEM_MOVEABLE As Long = &H2
Const GMEM_ZEROINIT As Long = &H40
Const CF_UNICODETEXT As Long = &HD
OpenClipboard 0&
EmptyClipboard
iLen = LenB(sUniText) + 2&
iStrPtr = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, iLen)
iLock = GlobalLock(iStrPtr)
lstrcpy iLock, StrPtr(sUniText)
GlobalUnlock iStrPtr
SetClipboardData CF_UNICODETEXT, iStrPtr
CloseClipboard
End Sub
Public Function GetClipboard() As String
Dim iStrPtr As Long
Dim iLen As Long
Dim iLock As Long
Dim sUniText As String
Const CF_UNICODETEXT As Long = 13&
OpenClipboard 0&
If IsClipboardFormatAvailable(CF_UNICODETEXT) Then
iStrPtr = GetClipboardData(CF_UNICODETEXT)
If iStrPtr Then
iLock = GlobalLock(iStrPtr)
iLen = GlobalSize(iStrPtr)
sUniText = String$(iLen \ 2& - 1&, vbNullChar)
lstrcpy StrPtr(sUniText), iLock
GlobalUnlock iStrPtr
End If
GetClipboard = sUniText
End If
CloseClipboard
End Function
This is on the form code:
Option Compare Database
Option Explicit
Private Sub Command6_Click()
Dim strSql As String
Dim ctl As Variant
For Each ctl In Me.Controls
If ctl.Tag = "?" Then
strSql = strSql & ctl.Controls(0).Caption & " " & Nz(ctl, "") & vbNewLine
End If
Next
Me.Text4 = ""
Me.Text4 = strSql
Me.Text7 = ""
SetClipboard strSql
End Sub
Solution 1:[1]
That is much code for nothing. This will do:
Private Sub CommandCopy_Click()
Dim Control As Control
Dim Value As String
For Each Control In Me.Controls
If Control.Tag = "?" Then
Value = Value & Control.Caption & " " & Nz(Control.Value) & vbNewLine
End If
Next
' Renamed Text4.
Me!ValueCopy.Value = Value
Me!ValueCopy.SetFocus
DoCmd.RunCommand acCmdCopy
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gustav |
