Solved

DBGrid Copy to Clipboard

Posted on 1997-08-09
3
572 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now