Results 1 to 22 of 22

Thread: Copying from datagrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    North East (UK)
    Posts
    204

    Question Copying from datagrid

    Hi All,

    Can anyone tell me how I could copy the contents of a datagrid to the clipboard. I need a way of transferring the data from the grid to an Excel worksheet.

    Many Thanks,

    Colin

  2. #2
    New Member
    Join Date
    Jun 2001
    Location
    San Antonio, TX
    Posts
    14

    Copy/Paste

    to copy the text onto the clipboard, use:

    Clipboard.SetText Screen.ActiveForm.ActiveControl.SelText

    to paste it, use:

    Screen.ActiveForm.ActiveControl.SelText = Clipboard.GetText()

    hope that helps

    David

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why not just have Excel open the same recordset?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    North East (UK)
    Posts
    204
    Thanks to you both,

    I have decided to use both solutions in my imlementation.

    Colin

  5. #5
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10

    Cool

    Hi Colin

    I have the same question about copying from a datagrid to excel.

    I tried the code from the replies you receive, but I must be doing something wrong as I cannot get it to work. I only get it to copy one cell and not all the recordsets in the datagrid.

    Have you managed to copy it properly. If you have, would you kindly forward me the code you used to copy this.

    Highly appreciated.

    Thanks
    Jannie

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    You can simply built a comma delimited file and sent to the clipboard... here is some code that u can adjust.... ps one thing to note is that string concatenation is slow and so is a good idea to build a number of intermediary strings and add together... ie rather than building a 100 row string.. build 10 x 10.. it is much faster
    Regards
    Stuart

    VB Code:
    1. Sub CopyGrid()
    2.     Dim llngRows As Long
    3.     Dim llngCols As Long
    4.     Dim lstrClipCopy As String
    5.    
    6.     lstrClipCopy = ""
    7.     With Grid1
    8.           For llngRows = 0 To .rows
    9.                 For llngCols = 0 To .Cols
    10.                       lstrClipCopy = lstrClipCopy & Trim$(.TextMatrix(llngRows, llngCols))
    11.                       If llngCols <> .Cols Then lstrClipCopy = lstrClipCopy & vbTab
    12.                 Next
    13.           Next
    14.      End With
    15.  
    16.     'Send to clipboard
    17.     Clipboard.Clear
    18.     Clipboard.SetText lstrClipCopy, 1
    19.    
    20.     'Clear memory
    21.     lstrClipCopy = ""
    22. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  7. #7
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Originally posted by beachbum
    Hi
    You can simply built a comma delimited file and sent to the clipboard... here is some code that u can adjust.... ps one thing to note is that string concatenation is slow and so is a good idea to build a number of intermediary strings and add together... ie rather than building a 100 row string.. build 10 x 10.. it is much faster
    Regards
    Stuart

    VB Code:
    1. Sub CopyGrid()
    2.     Dim llngRows As Long
    3.     Dim llngCols As Long
    4.     Dim lstrClipCopy As String
    5.    
    6.     lstrClipCopy = ""
    7.     With Grid1
    8.           For llngRows = 0 To .rows
    9.                 For llngCols = 0 To .Cols
    10.                       lstrClipCopy = lstrClipCopy & Trim$(.TextMatrix(llngRows, llngCols))
    11.                       If llngCols <> .Cols Then lstrClipCopy = lstrClipCopy & vbTab
    12.                 Next
    13.           Next
    14.      End With
    15.  
    16.     'Send to clipboard
    17.     Clipboard.Clear
    18.     Clipboard.SetText lstrClipCopy, 1
    19.    
    20.     'Clear memory
    21.     lstrClipCopy = ""
    22. End Sub
    Hi

    Thanks for your reply.

    I copied this to the project and I get some errors.I am still doing something wrong.
    I have a command button, a datagrid and a adodc on my form using the Nwind database and the customers table.
    Here is my code:

    Dim xl As Application
    Sub CopyGrid()
    Dim llngRows As Long
    Dim llngCols As Long
    Dim lstrClipCopy As String

    lstrClipCopy = ""
    With DataGrid1
    For llngRows = 0 To .Row
    For llngCols = 0 To .Col
    lstrClipCopy = lstrClipCopy & Trim$(llngRows, llngCols)
    If llngCols <> .Col Then lstrClipCopy = lstrClipCopy & vbTab
    Next
    Next
    End With

    'Send to clipboard
    Clipboard.Clear
    Clipboard.SetText lstrClipCopy, 1

    xl.Range("A1") = Clipboard.GetText
    'Clear memory
    lstrClipCopy = ""
    End Sub

    Private Sub GetExcel()

    Set xl = New Application
    xl.Visible = True
    xl.Workbooks.Add

    End Sub

    Private Sub Command1_Click()
    GetExcel
    CopyGrid
    End Sub

    Please help!!

    Thanks
    Jannie

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Sorry i had cut and pasted this code and pulled bits out here and there to make simple.. pulled out too much!! haha

    Anyway the rows and cols in a grid are zero based and so u need to subtract 1 from the upper limit
    VB Code:
    1. For llngRows = 0 To .Rows - 1
    2.             For llngCols = 0 To .Cols - 1

    I notice in ur code that u left the 'S' off of Rows and Cols in ur loops. This is important. In regard to the Excel stuff, i cant help cos i have never done any VBA stuff or things with Office objects

    Let me know how u go
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Just noticed that u also left the 'S' off of .Cols in this line

    If llngCols <> .Cols Then lstrClipCopy = lstrClipCopy & vbTab

    Let me know what errors u get if u get any
    If errors are to do with Excel try this. Run the code with all Excel stuff commented out. Then open Excel and simply Paste the clipboard. See if that works
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  10. #10
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Originally posted by beachbum
    Just noticed that u also left the 'S' off of .Cols in this line

    If llngCols <> .Cols Then lstrClipCopy = lstrClipCopy & vbTab

    Let me know what errors u get if u get any
    If errors are to do with Excel try this. Run the code with all Excel stuff commented out. Then open Excel and simply Paste the clipboard. See if that works
    Regards
    Stuart
    Hi Stuart

    It gives me a error of "Compile error - Wrong number of arguments or invalid property assignment" and highlights the "Trim$".

    I will attach the file for you.

    Thanks for your help

    Jannie

  11. #11
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Jannie
    I have this awful feeling that you are retyping what i give you... lol save urself the trouble and just Copy and Paste the code from here to VB. It works just fine. So, ur prob is just another typing error in that line.
    Regards
    Stuart

    VB Code:
    1. 'You typed
    2. lstrClipCopy = lstrClipCopy & Trim$(llngRows, llngCols)
    3.  
    4. 'Should have been, as per original post
    5. lstrClipCopy = lstrClipCopy & Trim$(.TextMatrix(llngRows, llngCols))
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  12. #12
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Originally posted by beachbum
    Hi Jannie
    I have this awful feeling that you are retyping what i give you... lol save urself the trouble and just Copy and Paste the code from here to VB. It works just fine. So, ur prob is just another typing error in that line.
    Regards
    Stuart

    VB Code:
    1. 'You typed
    2. lstrClipCopy = lstrClipCopy & Trim$(llngRows, llngCols)
    3.  
    4. 'Should have been, as per original post
    5. lstrClipCopy = lstrClipCopy & Trim$(.TextMatrix(llngRows, llngCols))
    Hi Stuart

    I have pasted your replies but I get errors.

    If I use ".textmatrix" it gives me a error saying Method or data member not found and even just".text gives me an error.Also using (s)in cols and rows gives me an error.

    I must be doing something stupid.
    Did you manage to run it with the attachment I sent, because if it works with you I am missing the point somewhere.

    Thanks for all your replies and trying to help me fixing this thing I do wrong.


    Thanks
    Jannie

  13. #13
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Jannie
    It is prob just the type of control ... which grid are u using? The newer lighter grids are Flexgrid and Hierarchical Flex grid. The code will work with Flexgrid where it was copied from. I havent looked at properties for other grids so not sure there.

    I cant use ur file attachment as u have only sent the Binary file .FRX... u need to send the .FRM file (same named file)

    Regards
    Stuart

    PS Flexgrid and HFlex can be added by:
    1) Project menu
    2) Components
    3) Check Microsoft Flexgrid Control
    OR Microsoft Hierarchical Flexgrid Control
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  14. #14
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Originally posted by beachbum
    Hi Jannie
    It is prob just the type of control ... which grid are u using? The newer lighter grids are Flexgrid and Hierarchical Flex grid. The code will work with Flexgrid where it was copied from. I havent looked at properties for other grids so not sure there.

    I cant use ur file attachment as u have only sent the Binary file .FRX... u need to send the .FRM file (same named file)

    Regards
    Stuart

    PS Flexgrid and HFlex can be added by:
    1) Project menu
    2) Components
    3) Check Microsoft Flexgrid Control
    OR Microsoft Hierarchical Flexgrid Control

    Hi Stuart

    Yes you were right. It works with the flexigrid, but I am using the HeadClick event of the datagrid. I have read some help files and came up with some code. I now get the datagrid to copy info to excel, but I am battling to copy all the cells of the datagrid. Could you please maybe check my code to see where I am going wrong and how I can fix it.

    I am using a datagrid,command button and an adodc control with the Nwind database and the employees table.

    My code is as follows:

    Private Sub GetExcel()

    Set xl = New Application
    xl.Visible = True
    xl.Workbooks.Add

    End Sub

    Private Sub Command1_Click()
    GetExcel

    Dim ISelBook As Variant
    Dim sCellVal As Variant
    Dim x As Variant
    Dim Icols As Variant
    Dim mRows

    For Icols = 0 To DataGrid1.VisibleCols - 1
    x = Icols
    Next Icols

    For mRows = 0 To DataGrid1.VisibleRows - 1
    DataGrid1.SelBookmarks.Add DataGrid1.RowBookmark(mRows)
    Next mRows

    For ISelBook = 0 To DataGrid1.SelBookmarks.Count - 1
    sCellVal = DataGrid1.Columns(x).CellValue(DataGrid1.SelBookmarks(ISelBook))
    Clipboard.SetText sCellVal, 1
    xl.Range("A1 ") = Clipboard.GetText
    Next ISelBook

    End Sub

    It now only copies the last record's last colum into excel.It seems the loops are working ,but I am not putting the loops together the right way that it copies all the records to excel.

    Please help!!

  15. #15
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Jannie
    LOL dont sound so desperate!! OK 2 ways to go about this. I havent ever used the Datagrid control so am by no means an expert on its use!!

    1) I looked at the Headclick event and if that is the only reason u are using the datagrid control then it is easy to change to a flexgrid and just check which column was clicked in the click event.

    2) Don't know about ur use of bookmarks etc. Are u sure this is the right reading of it? I only looked at help files for a few mins but i am sure that there must be easier way to get cell values. I think bookmarks is only if you want to get whole rows of vals direct from database as against what is already in the grid but not sure. I assume it is just the .Text value using the .Col and .Row properties to set which cell is being read.

    In regard to your loops they are a bit mixed up. You need to have your processing within the loops and to loop thru rows first and then columns... the following is pseudo code becos i dont know the datagrid but maybe someone else that uses it can help

    VB Code:
    1. with Datagrid1
    2.       For llngRow = 0 to .Rows - 1
    3.             For llngCol = 0 to .Cols -1
    4.                   .Row = llngRow
    5.                   .Col = llngCol
    6.                   lstrClip = lstrClip & .Text & vbtab
    7.             next
    8.             lstrClip = lstrClip & vbcrlf
    9.       next
    10. end with

    and then use the clipboard Clear, SetText stuff i gave u before. As i said this is a guess cos i dont use that control but see how u go.
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  16. #16
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi again Jannie
    I see that datagrid doesnt have .Cols or .Rows properties and that the .Col or .Row property sets the pointer position. I just tried out a stupid sample of code on a datagrid and although it sorta worked i would think that there must be a far better way.

    VB Code:
    1. Private Sub Command1_Click()
    2.     With DataGrid1
    3.         For llngrows = 0 To 10 'How do u count rows and cols?
    4.             For llngcols = 0 To 1
    5.                 .Row = llngrows
    6.                 .Col = llngcols
    7.  
    8.                 lstrtext = lstrtext & .Text & vbTab
    9.             Next
    10.             lstrtext = lstrtext & vbCrLf
    11.         Next
    12.     End With
    13.    
    14.     Me.Print lstrtext
    15. End Sub

    Go to Flexgrid where i can help!! haha
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  17. #17
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Originally posted by beachbum
    Hi again Jannie
    I see that datagrid doesnt have .Cols or .Rows properties and that the .Col or .Row property sets the pointer position. I just tried out a stupid sample of code on a datagrid and although it sorta worked i would think that there must be a far better way.

    VB Code:
    1. Private Sub Command1_Click()
    2.     With DataGrid1
    3.         For llngrows = 0 To 10 'How do u count rows and cols?
    4.             For llngcols = 0 To 1
    5.                 .Row = llngrows
    6.                 .Col = llngcols
    7.  
    8.                 lstrtext = lstrtext & .Text & vbTab
    9.             Next
    10.             lstrtext = lstrtext & vbCrLf
    11.         Next
    12.     End With
    13.    
    14.     Me.Print lstrtext
    15. End Sub

    Go to Flexgrid where i can help!! haha
    Regards
    Stuart

    Hi Stuart

    Thanks a mil. This code did the job. I have recently completed my MCSD ,but this type of practical experience and assistance you cannot get in the classroom. You have helped me a great deal and I cannot express the joy I felt when this code ran perfectly. Just shows how much I still need to learn.

    Thanks once again.

    Jannie

  18. #18
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Originally posted by Jannie



    Hi Stuart

    Thanks a mil. This code did the job. I have recently completed my MCSD ,but this type of practical experience and assistance you cannot get in the classroom. You have helped me a great deal and I cannot express the joy I felt when this code ran perfectly. Just shows how much I still need to learn.

    Thanks once again.

    Jannie
    Hi Stuart

    I wonder if you could help me with a little bit more.

    Your code worked and copied the info from the datagrid to excel, but could you maybe help me with the code that also includes the columnheaders of the datagrid. The current code do not copy the headers to the clipboard.

    I will appreciate your help

    Thanks
    Jannie

  19. #19
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Jannie
    You keep picking on me about this datagrid control!! lol I have never ever ever used it... I just looked at help file for 2 secs and saw the columns property. So, i added this loop to the original one. This should work but as i said i have no idea about this control.
    Regards
    Stuart
    VB Code:
    1. Private Sub Command1_Click()
    2.     With DataGrid1
    3.         [b]For llngcols = 0 To 1
    4.             lstrtext = lstrtext & .Columns(llngcols).Caption & vbTab
    5.         Next
    6.         lstrtext = lstrtext & vbCrLf[/b]
    7.        
    8.         For llngrows = 0 To 10 'How do u count rows and cols?
    9.             For llngcols = 0 To 1
    10.                 .Row = llngrows
    11.                 .Col = llngcols
    12.  
    13.                 lstrtext = lstrtext & .Text & vbTab
    14.             Next
    15.             lstrtext = lstrtext & vbCrLf
    16.         Next
    17.     End With
    18.    
    19.     Me.Print lstrtext
    20. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  20. #20
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Originally posted by beachbum
    VB Code:
    1. Private Sub Command1_Click()
    2.     With DataGrid1
    3.         [b]For llngcols = 0 To 1
    4.             lstrtext = lstrtext & .Columns(llngcols).Caption & vbTab
    5.         Next
    6.         lstrtext = lstrtext & vbCrLf<b>Hi Jannie
    7. You keep picking on me about this datagrid control!! <img src="images/smilies/tongue.gif" border="0" alt="" title="Stick Out Tongue" class="inlineimg" /> lol I have never ever ever used it... I just looked at help file for 2 secs and saw the columns property. So, i added this loop to the original one. This should work but as i said i have no idea about this control. <img src="images/smilies/wink.gif" border="0" alt="" title="Wink" class="inlineimg" />
    8. Regards
    9. Stuart
    10. </b>
    11.        
    12.         For llngrows = 0 To 10 'How do u count rows and cols?
    13.             For llngcols = 0 To 1
    14.                 .Row = llngrows
    15.                 .Col = llngcols
    16.  
    17.                 lstrtext = lstrtext & .Text & vbTab
    18.             Next
    19.             lstrtext = lstrtext & vbCrLf
    20.         Next
    21.     End With
    22.    
    23.     Me.Print lstrtext
    24. End Sub
    [/B]

    Thanks again!!!

    Jannie

  21. #21
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10
    Hi Stuart

    Me again.

    Compliments of the season.

    Would you know why I get a error when the datagrid shows 20 records because of the size on the form,but actually there is 35 records and as soon as the loop gets to 20 records the loop does not count properly and then it gives me "Run Time error 6148 , Invalid row number"

    This happens as soon as run the loop to copy the datagrid to excel.

    Please help

    Thanks
    Jannie









    Originally posted by beachbum
    VB Code:
    1. Private Sub Command1_Click()
    2.     With DataGrid1
    3.         [b]For llngcols = 0 To 1
    4.             lstrtext = lstrtext & .Columns(llngcols).Caption & vbTab
    5.         Next
    6.         lstrtext = lstrtext & vbCrLf<b>Hi Jannie
    7. You keep picking on me about this datagrid control!! <img src="images/smilies/tongue.gif" border="0" alt="" title="Stick Out Tongue" class="inlineimg" /> lol I have never ever ever used it... I just looked at help file for 2 secs and saw the columns property. So, i added this loop to the original one. This should work but as i said i have no idea about this control. <img src="images/smilies/wink.gif" border="0" alt="" title="Wink" class="inlineimg" />
    8. Regards
    9. Stuart
    10. </b>
    11.        
    12.         For llngrows = 0 To 10 'How do u count rows and cols?
    13.             For llngcols = 0 To 1
    14.                 .Row = llngrows
    15.                 .Col = llngcols
    16.  
    17.                 lstrtext = lstrtext & .Text & vbTab
    18.             Next
    19.             lstrtext = lstrtext & vbCrLf
    20.         Next
    21.     End With
    22.    
    23.     Me.Print lstrtext
    24. End Sub
    [/B]

  22. #22
    New Member
    Join Date
    Mar 2001
    Location
    Johannesburg
    Posts
    10

    Datagrid

    Originally posted by Jannie
    Hi Stuart

    Me again.

    Compliments of the season.

    Would you know why I get a error when the datagrid shows 20 records because of the size on the form,but actually there is 35 records and as soon as the loop gets to 20 records the loop does not count properly and then it gives me "Run Time error 6148 , Invalid row number"

    This happens as soon as run the loop to copy the datagrid to excel.

    Please help

    Thanks
    Jannie







    Hi Stuart

    Please help me with this error I am getting.
    Like I say as soon as there is more records than is shown on the form I get this error.

    Please help.
    Thanks
    Jannie



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width