Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 7th, 2004, 04:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Norwich, Norfolk, United Kingdom.
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copy to Clipboard

How can I copy the contents of an A97 text box to the Windows clipboard please? I tried the following but it doesn't work:

  Text0.SetFocus
  SendKeys "^C"

Clive Astley
__________________
Clive Astley
Reply With Quote
  #2 (permalink)  
Old July 7th, 2004, 07:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Clive,

In form module...

Private Sub CopyToClipboard_Click()
    Call ClipBoard_SetData(Me!fText)
End Sub


...Windows API calls in standard module...

Option Compare Database
Option Explicit

      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

HTH,

Bob

Reply With Quote
The Following User Says Thank You to Bob Bedell For This Useful Post:
boblarson (January 4th, 2009)
  #3 (permalink)  
Old July 7th, 2004, 07:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Norwich, Norfolk, United Kingdom.
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks Bob. Works perfectly.

Clive Astley
Reply With Quote
  #4 (permalink)  
Old July 7th, 2004, 07:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

You're welcome Clive. Glad it got it for you.

Bob

Reply With Quote
  #5 (permalink)  
Old July 16th, 2004, 12:01 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Bob:

I have been trying to write codes using API calls similar to the example you just gave but I have no clue where to start learning about these procedures. All the text books I have seen do not cover these topics.

Can you suggest how to get started (text books, on line tutorial etc). I am quite advanced in VBA but not in writing API calls so I will be starting form the basics.

Thanks
Reply With Quote
  #6 (permalink)  
Old July 16th, 2004, 07:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi stoneman,

As a general rule of thumb, I would never set about trying to write my own API call; I simply collect API calls that are known to work, and generally cut and paste them with few, if any, modifications. When you're calling external API functions you are no longer working in VBA's wonderfully safe environment. One simple typo can cause you to GPF (general protection fault), crash your app, and waste contless development hours trying to figure out what's up. Instead, find code that already has a good wrapper function around the API call you need, and tuck it away in a code library somewhere for future reference.

That said, there are some interesting resources out there...

The dlls that form the Windows API are not wrapped in type libraries, so you can't reference them in an Access app, and hence can't view their function definitions via the Object Browser. The obvious question then, is how do you know what the available API functions are?

Both the MS Office Developer Edition (ODE) and VB6 include a text file called WIN32API.TXT that contains VBA Declare statements for most of the functions in the Windows API (since VBA can't reference the relevant dlls [Kernel32.dll, User32.dll, Comdlg32.dll, etc.], the Declare statements are what tell VBA where to find a particular DLL function and how to call it). If you don't own either the ODE or VB6, you can download WIN32API.TXT for free from MS @:

http://www.microsoft.com/downloads/d...displaylang=en (watch word wrap)

In additioin to WIN32API.TXT, the ODE and VB6 come with a utility exe called the API Viewer. APILOAD.exe is a VB6 add-in but works fine as a stand-alone exe. See:

http://msdn.microsoft.com/library/de...pplication.asp

(look under the "Accessing the Microsoft Windows API" node in the MSDN treeview for a screen capture of the API Viewer).

The API Viewer allows you to load WIN32API.TXT into a fairly klunky GUI, write the Declares, Constants, and User-defined type definiftions you need to a Textbox, then paste the completed API call into your app. It also lets you convert WIN32API.TXT into an Access database. Frankly, I find it easier to simply open WIN32API.TXT in Notepad/Wordpad and search for a function I'm interested in.

Then once you locate the Declare you want, you need to know what it means, and what parameters it requires!! For that, you need one or all of the below:

- Somebody elses (hopefully commented) code [recommended]
- WIN32 documentation (the MSDN library CD's contain the MS Platform SDK and are the best source)
- several good books.

Some of the better Access books have good chapters on API functions:

Getz, et al - Access 2002 Developer's Handbook, Desktop Edition
F. Scott Barker - Microsoft Access 2002 Power Programming

There are also some good books on the Winows API geared toward VB developer's, but the VB Declares and wrapper functions are identical in VBA. The best in terms of an exhaustive reference is:

Dan Appleman's Visual Basic Programmer's Guide to the Win32 API

I've also worked with:

Visual Basic 6 Win32 API Tutorial - Jason Brock (Wrox)
Win32 API Programming with Visual Basic - Steven Roman (O'reilly)

HTH,

Bob

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop Changes or Copy to clipboard JezLisle Access VBA 4 November 20th, 2008 10:24 AM
Copy Metafile to the Clipboard TMA_Master BOOK: Visual Basic 2008 Programmer's Reference ISBN: 978-0-470-18262-8 1 August 26th, 2008 10:59 PM
Copy to clipboard in EMF format. watashi C# 1 April 17th, 2008 09:02 AM
how to copy text in a word file on the clipboard donrafeal Word VBA 3 February 1st, 2007 12:32 AM
Copy text to win Clipboard irresistible007 Beginning VB 6 6 October 5th, 2006 06:04 AM



All times are GMT -4. The time now is 06:35 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.