Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 21st, 2005, 03:22 AM
Registered User
 
Join Date: Sep 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copieng and pasting with VBA

Hi,

i have a bit of a problem, im a way to big novice,

the assignment that i got was that the users of the database, need to be able to press on a button , and adress info should be copied to the clipboard, so the users can easly copy it to word, publishing it into word isnt an option cus they use old letters and emails stuff like that, and want to work as efficient as the can, my boss has been pounding on me that it should just copy to the clipboard,

hes what i need to do in VBA, i need to selelect a query to run it, select a tabel (maybe open it first) and copy the content, without the layout, i know there is a command "copy special" with should do the trick i know it does its job done from excel to word without coping the layout,

im at it for 2 days now, and i have still no qlue what to do, so i wanted a bit of a fresh start. i hope someone can help me.

also if you need any code just ask,

(p.s i allready made the button and query)

 
Old September 21st, 2005, 07:00 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You should probably create a form with a record look up feature, and then have the user navigate to the record they want first to confirm that they will be copying the data they want.

Then have another button on the form that says copy to clipboard.

Then just capture the values from the form itself, and then paste them to the clipboard. There is no need for an intermediate query.

Other posters can give you more help. I have SQL training all day. Woo-hoo!

mmcdonal
 
Old October 13th, 2005, 11:58 AM
Registered User
 
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I have done the same the code from the button click event is:

Private Sub cmdCopyToClipboard_Click()
On Error GoTo Err_cmdCopyToClipboard_Click

    Dim strCompany As String
    Dim strAddress As String
    Dim strCounty As String
    Dim strName As String
    Dim strNameAddress As String

    strName = Me.Title & " " & Me.First_Name & " " & Me.Last_Name
    strName = LTrim(strName)

    strCounty = DLookup("[County]", "[tblCounties]", "[ID] =" & Me.cboCounty)

    If Me.cboCompanyName <> 0 Then
        strCompany = DLookup("[CompanyName]", "[tblCompanies]", "[CompanyID] =" & _
        Me.cboCompanyName)
    End If

    strAddress = Me.Address1 & vbCrLf
    strAddress = strAddress & Me.Address2 & vbCrLf
    strAddress = strAddress & Me.Address3 & vbCrLf
    strAddress = strAddress & Me.Town & vbCrLf
    strAddress = strAddress & strCounty & vbCrLf
    strAddress = strAddress & Me.Postal_Code & vbCrLf

    strNameAddress = strName & vbCrLf & strCompany & vbCrLf & strAddress

    ClipBoard_SetData (strNameAddress)

Exit_cmdCopyToClipboard_Click:
Exit Sub

Err_cmdCopyToClipboard_Click:
    MsgBox Err.Description
    Resume Exit_cmdCopyToClipboard_Click

End Sub

The code for copying to clipboard is:

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 moveable 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 OutOfHere2
   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)

OutOfHere2:

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

   End Function

Good luck

regards
Paul





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pasting into spreadsheets IainAL VB How-To 0 April 23rd, 2007 04:33 AM
Copy and Pasting jilly Beginning VB 6 1 March 8th, 2006 09:46 AM
easy pasting problem reverand Excel VBA 17 November 25th, 2005 01:11 AM
Pasting information to a second database Brendan Bartley Access 1 September 27th, 2005 09:45 AM
Pasting Data into a webform dkb General .NET 2 July 4th, 2004 04:29 AM





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