Copying a text box "Easy"

Jmsteph

Registered User.
Local time
Today, 02:02
Joined
Dec 2, 2008
Messages
28
Hello,

I know this is an easy one, but for some reason it is not working for me. On the form the user needs to input the contract number, which he/she will also need to input into other programs. I want access to copy what user puts into the "Contract Number" text box after he/she tabs off the text box so he/she just paste it into the other programs. I figure just writing the code into the on exit function, but for some reason it is not copying anything.
Thanks,

JS
 
What code are you currently using. If you put it in the text box's AFTER UPDATE event, then it would be

DoCmd.RunCommand acCmdCopy
 
I tried that one, but keep getting run-time error '2046': The command or action 'Copy' isn't available now. Do you have an idea on how to get around that?
Thanks,

JS
 
Yeah, here you go.

1. Create a NEW STANDARD module and name it modClip

2. Paste this code into it:
Code:
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

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

Function ClipBoard_SetData(MyString As String)
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long, X As Long

' Allocate movable global memory.
'-------------------------------------------
hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)

' Lock the block to get a far pointer
' to this memory.
lpGlobalMemory = GlobalLock(hGlobalMemory)

' Copy the string to this global memory.
lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

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

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

' Clear the Clipboard.
X = EmptyClipboard()

' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

ExitHere:

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

End Function

3. Call it like this from the After Update event of your text box on the form:
Code:
Call ClipBoard_SetData(Me.YourTextBoxNameHere)
 
That was perfect. I really appreciate all the help you have always given me!!!!!
 

Users who are viewing this thread

Back
Top Bottom