By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

How to extract the records from datagrid to excel

P: 2
I have developed an application for extracting records from datagrid to excel. Unfortunately, when i run this application, no records were extracted. If anybody knows what is lacking in my script then please inform me. Below is my script. Thanks a lot!!!

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmDExtract_Click()
  2.  
  3.     On Error GoTo ErrorLine
  4.     Screen.MousePointer = vbHourglass
  5.     Dim xl As Object
  6.     Dim wb As Object
  7.     Dim ws As Object
  8.     Dim i  As Long
  9.     Dim X  As Long
  10.  
  11.     Set xl = CreateObject("Excel.Application")
  12.     Set wb = xl.Workbooks.Add()
  13.     Set ws = xl.ActiveWindow.ActiveSheet
  14.     ws.Columns().ColumnWidth = 10
  15.  
  16.     For X = 0 To DataGrid1.Row - 1
  17.         DataGrid1.Row = X
  18.         DataGrid1.Col = 1
  19.             For i = 1 To DataGrid1.Col
  20.                 DataGrid1.Col = i
  21.                 If DataGrid1.Text <> "" Then
  22.                     ws.Cells(X, i) = DataGrid1.Text
  23.                     ws.Cells(X, i).HorizontalAlignment = -4131
  24.  
  25.                 End If
  26.             Next
  27.     Next
  28.     xl.Visible = True
  29. ErrorLine:
  30.     Screen.MousePointer = vbDefault
  31.  
  32. End Sub
May 7 '07 #1
Share this Question
Share on Google+
1 Reply


Dököll
Expert 100+
P: 2,364
I have developed an application for extracting records from datagrid to excel. Unfortunately, when i run this application, no records were extracted. If anybody knows what is lacking in my script then please inform me. Below is my script. Thanks a lot!!!

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmDExtract_Click()
  3.  
  4.     On Error GoTo ErrorLine
  5.     Screen.MousePointer = vbHourglass
  6.     Dim xl As Object
  7.     Dim wb As Object
  8.     Dim ws As Object
  9.     Dim i  As Long
  10.     Dim X  As Long
  11.  
  12.     Set xl = CreateObject("Excel.Application")
  13.     Set wb = xl.Workbooks.Add()
  14.     Set ws = xl.ActiveWindow.ActiveSheet
  15.     ws.Columns().ColumnWidth = 10
  16.  
  17.     For X = 0 To DataGrid1.Row - 1
  18.         DataGrid1.Row = X
  19.         DataGrid1.Col = 1
  20.             For i = 1 To DataGrid1.Col
  21.                 DataGrid1.Col = i
  22.                 If DataGrid1.Text <> "" Then
  23.                     ws.Cells(X, i) = DataGrid1.Text
  24.                     ws.Cells(X, i).HorizontalAlignment = -4131
  25.  
  26.                 End If
  27.             Next
  28.     Next
  29.     xl.Visible = True
  30. ErrorLine:
  31.     Screen.MousePointer = vbDefault
  32.  
  33.  
  34.  
  35. End Sub
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
Hey there, edgara!

Good of you to add your code in. It looks like you have not yet attempted to create a .xls file. If you have previously searched here and could not find anything, set your eyes on this:

http://www.developerfusion.co.uk/show/6072/

This should get you started. If not, please stay tuned. I cannot run anything from here just yet. Please stay tuned nonetheless, helpful members will likely see your information.

In a bit!

Dököll
May 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.