Solved Copy all Date on form to clipboard to user can past this into another system (1 Viewer)

Number11

Member
Local time
Today, 22:05
Joined
Jan 29, 2020
Messages
607
Loop through the forms control collection and use the Tag property to identify the textboxes you want.

heres an example

edit : UG is faster than me.

added PtrSafe to code but still getting compile error on

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
 

moke123

AWF VIP
Local time
Today, 18:05
Joined
Jan 11, 2013
Messages
3,852
The code needs to be in a standard module btw, not the form module.

I dont use 64 bit so cant really help you with ptrsafe issue.
 

Number11

Member
Local time
Today, 22:05
Joined
Jan 29, 2020
Messages
607
The code needs to be in a standard module btw, not the form module.

I dont use 64 bit so cant really help you with ptrsafe issue.
Yes i have this as a stand alone module :)
 

moke123

AWF VIP
Local time
Today, 18:05
Joined
Jan 11, 2013
Messages
3,852
Glad you've got it sorted. Good luck with your project.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 22:05
Joined
Jul 9, 2003
Messages
16,245
I did notice in one of the links there was the option to "Copy From Clipboard"... I wondered if anyone wanted to explore that and post an example?


 

moke123

AWF VIP
Local time
Today, 18:05
Joined
Jan 11, 2013
Messages
3,852
Thanks Uncle. This looks like a 64 bit version that allegedly works. Cant test it as I dont have 64bit access.

from https://stackoverflow.com/questions/35416662/text-to-clipboard-in-vba-windows-10-issue

Code:
Option Explicit
#If VBA7 Then

Private Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function EmptyClipboard Lib "User32" () As LongPtr
Private Declare PtrSafe Function CloseClipboard Lib "User32" () As LongPtr
Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "User32" (ByVal wFormat As LongPtr) As LongPtr
Private Declare PtrSafe Function GetClipboardData Lib "User32" (ByVal wFormat As LongPtr) As LongPtr
Private Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As LongPtr, ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As LongPtr
Private Declare PtrSafe Function GlobalLock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As LongPtr) As Long
Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr

#Else

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

#End If

Public Sub SetClipboard(sUniText As String)

#If VBA7 Then
    Dim iStrPtr As LongPtr
    Dim iLock As LongPtr
#Else
    Dim iStrPtr As Long
    Dim iLock As Long
#End If

    Dim iLen 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 + GMEM_ZEROINIT, iLen)
    iLock = GlobalLock(iStrPtr)
    lstrcpy iLock, StrPtr(sUniText)
    GlobalUnlock iStrPtr
    SetClipboardData CF_UNICODETEXT, iStrPtr
    CloseClipboard
End Sub

Public Function GetClipboard() As String
#If VBA7 Then
    Dim iStrPtr As LongPtr
    Dim iLock As LongPtr
#Else
    Dim iStrPtr As Long
    Dim iLock As Long
#End If
    Dim iLen 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
 

Jason Lee Hayes

Active member
Local time
Today, 22:05
Joined
Jul 25, 2020
Messages
174
Here's an example I knocked up which should go through all the Text box's on your form and concatenate the label and contents of the text box together. However, there is one problem, there is no guarantee it will process them in the correct order. You could try moving them around, you could try examining the controls Tab order, but that might be complicated!

EDIT:- I've updated the sample file to work with copy to clipboard, on both 32 & 64-bit MS Access. I know it works on 64-bit, but I don't know if it works on 32-bit, if anyone could check, would be most grateful!
Can confirm works fine in Access 2016 32-bit
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:05
Joined
Feb 19, 2002
Messages
42,976
Start with this - create a spreadsheet with the data laid out in the order you want. Copy a row and paste it into the other app. Does that work?

If it does, you can paste it into notepad and then examine how the fields are separated because it is the separator that you need to understand. Is there a tab character being used to separate the fields?
 

Users who are viewing this thread

Top Bottom