borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Nesting If statements within a While Not loop)Next Topic (Recent Docs in File Menu) New Topic New Poll Post Reply
AndreaVB Forum : VB General : VB data grid to Excel app
Poster Message
gitchi
Level: Guest


icon VB data grid to Excel app  Archived to Disk

Does anyone have a good coding example of how to export from VB 6.0 datagrid to a excel spreadsheet from the VB form itself?

25-07-2002 at 08:54 PM
| Quote Reply
Coyote
Level: Guest

icon Re: VB data grid to Excel app  Archived to Disk

Here is one example that will write data to a .CVS excel file. ( or guess you can change to .XLS if you like-test??) Hope this helps....
' --------CODE STARTS -----

Option Explicit
Dim mstrConnectionString As String
Dim FExists As Boolean

Private Sub Form_Load()
    'Connect to Database ----->
    mstrConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "test.mdb;"
    dcexport.ConnectionString = mstrConnectionString
    dcexport.Visible = False
    dcexport.RecordSource = "SELECT * FROM streets"
    dcexport.Refresh


End Sub


Private Sub cmdExport_Click()
  Dim fieldnum As Integer
  Dim cellstring As String
  Dim headstring As String
  Dim daAnswer
  
cdexport.CancelError = True
On Error GoTo SaveErr

  With cdexport
    .DialogTitle = "Export to CSV"
    .Filter = "Excel Import File (*.csv)|*.csv"
    .FileName = "tester"
    .ShowSave
  End With
  
  FileExists (cdexport.FileName)
  If FExists = True Then
    daAnswer = MsgBox("File Exists. Overwrite?", vbYesNo + vbQuestion, "File Exists")
    If daAnswer = vbNo Then
      cmdExport_Click
    End If
  End If
    
  Open cdexport.FileName For Output As #1
  Print #1, "test Export - Coyote Say dis bad boy works" '
  dcexport.Recordset.Bookmark = dgexport.Bookmark
    
  For fieldnum = 0 To dgexport.Columns.Count - 1 'Routine for writing the header to the CSV File
    headstring = headstring & dgexport.Columns(fieldnum).Caption & ","
  Next
  Print #1, headstring

  Do While dcexport.Recordset.EOF = False
    For fieldnum = 0 To dcexport.Recordset.Fields.Count - 1
        cellstring = cellstring & dcexport.Recordset.Fields(fieldnum).Value & ","
    Next
    Print #1, cellstring
    cellstring = ""
    ' cellstring = "'"
    dcexport.Recordset.MoveNext
  Loop
  Close #1

SaveErr:
    If Err <> 32755 Then
    End If
    Exit Sub

End Sub

Function FileExists(ByVal FileName As String)

   Dim Exists As Integer
  
   On Local Error Resume Next
   Exists = Len(Dir(FileName$))
If Exists = 0 Then 'Null string?
    FileExists = False
    FExists = False
Else
    FileExists = True
    FExists = True
End If
End Function

27-07-2002 at 12:15 AM
| Quote Reply
Coyote
Level: Guest

icon Re: VB data grid to Excel app  Archived to Disk

Here is a second example: Believe you can see how this is done from the code. If not I can send you the projects in zip format via email:  This one actually opens excel and then puts in the data --- again hope this helps....
PS... those stupid winkie icon thingy macbobs in the code are actually close parentheseeee thingys (keyboard =  shift & 0)

' &&&&&&&& CODE STARTS HERE *~*~*~*~
Option Explicit

Private Sub Command1_Click()
'+++++++++++++++++++++++++++++++++++++++++++
' Reference : MicroSoft Excel 9.0 Object Library
'+++++++++++++++++++++++++++++++++++++++++++
Dim xl As Excel.Application
Dim wb As Object
Dim ws As Object
Dim excelwasnotrunnung As Boolean

' Make New Object
Set xl = CreateObject("Excel.Application")

' Display Screen
xl.Application.Visible = True
' Add Excel
xl.Workbooks.Add
xl.Worksheets("sheet1").Name = "HelloSheet" ' Change Name of Sheet

Dim i, j, k, p As Integer

k = Data1.Recordset.RecordCount - 1 ' Count Total Row in DB

p = Data1.Recordset.Fields.Count - 1   ' cOUNT Total Column in DB

With DBGrid1
  

  For i = 0 To k   ' Starting from [0] because the start point of  DBGRID
                       '  is [0]  <---- STARTING ROW
                      
     For j = 0 To p     ' Starting from [0] because the start point of  DBGRID
                       '  is [0]  <--- STARTING COLUMN
                      
          
          .Row = i
          .Col = j
          
                          
                          
         '------------------------------------------------------------
         '  IF Some of Data on your DB may have NULL. DO NOT worry !
         '  The relevant point of cells in the Excel will not be
         '  shown  [without error]
         '
         '------------------------------------------------------------
                          
        xl.Worksheets("HelloSheet").Cells(i + 1, j + 1).Value = .Text
                                                                          
                                                                          
          '----------------------- (i + 1, j + 1) --------------------
          ' This is very very important matter !
          ' If you not start + 1 . Then, You will see critical error
          ' during transfer data form DBGRID to Excel
          ' because The starting point of the Excel's cells is [Cells(1, 1)]
          ' NOT (0, 0) !
          '
          ' In addition, If you want to shift the starting point of output
          ' on the Excel, Then you can change like ..
          '
          ' .Cells(i + 5, j + 5).Value = .Text  ... Whatever you want !
          '------------------------------------------------------------------
                                                                          
      Next
      
        
  Next

End With

    Set wb = Nothing
    Set xl = Nothing


End Sub


Private Sub Form_Load()

Command1.Caption = "Start Transfer Data"

Data1.DatabaseName = App.Path & "db1.mdb"
Data1.Refresh

Call Data_Description
End Sub
Private Sub Data_Description()


If Data1.Recordset.EOF And Data1.Recordset.BOF Then
  
   MsgBox "NO Data available !", vbCritical + vbOKOnly, "Error"
  
  Exit Sub
End If


Data1.RecordSource = "SELECT * FROM Table1 WHERE [Who] ORDER BY [Who]"
Data1.Recordset.MoveLast
Data1.Recordset.MoveFirst
Data1.Refresh
Data_Grid


End Sub
Private Sub Data_Grid()

'--------- Adjust Column width of DB Grid -------------

With DBGrid1
        
         .HeadLines = 1
         .Columns(0).Width = 700
         .Columns(1).Width = 700
         .Columns(2).Width = 700
         .Columns(3).Width = 700
         .Columns(4).Width = 700
         .Columns(5).Width = 700
         .Columns(6).Width = 700
         .Columns(7).Width = 700
End With


End Sub

Private Sub Form_Unload(Cancel As Integer)
Set Form1 = Nothing
Data1.Recordset.Close
End
End Sub


27-07-2002 at 12:24 AM
| Quote Reply
AndreaVB Forum : VB General : VB data grid to Excel app
Previous Topic (Nesting If statements within a While Not loop)Next Topic (Recent Docs in File Menu) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder