Solved

DBGrid Copy to Clipboard

Posted on 1997-08-09
3
579 Views
Last Modified: 2008-03-17
I have a DBGrid that is populated by an SQL and I would like to be able to copy all of the data to the clipboard in a format that can be pasted to Excel/Access etc....
0
Comment
Question by:jcorbin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:cymbolic
ID: 1430126
Your problem is that the Grid control is really a large collection of separate controls.  In order to get all the data, you will have to interate through all the cells, collect the data, and format it for inclusion.  I recommend that if you are going to all this trouble anyway, that you dump the clipboard idea, and use the import features in Excel and Access.  You can save your data in a comma delimited format that can be read by either system.  It's simple, it's safe, and if the import doesn't get there, or your system honks in the middle, the file(s) are still there for later import.
0
 

Author Comment

by:jcorbin
ID: 1430127
I understanf all of those concerns, but I need to see the code to iterate thru the grid and copy it to the clipboard, from there I can print, export to excel etc....



0
 
LVL 5

Accepted Solution

by:
volking earned 100 total points
ID: 1430128
Here's a code snippet I use in one of my apps. It copies the entire contents of a DBgrid (bound to a data control) to the clipboard.  Excel and Word will allow paste.

Private Sub btnCopyForPaste_click()
    Dim Bulk$, Row&, Col%
    For Row& = 1 To (dcRaw.Recordset.RecordCount - 1)
        DBGrid1.Row = Row&
        For Col% = 0 To (DBGrid1.Columns.Count - 1)
            DBGrid1.Col = Col%
            Bulk$ = Bulk$ + DBGrid1.Text + vbTab
        Next
        Mid$(Bulk$, Len(Bulk$)) = Chr$(13)
    Next
    Clipboard.Clear
    Clipboard.SetText Bulk$, vbCFText
End Sub


Enjoy
===================> Frederick
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question