Hi
Thanks for send out those links. I have written a VB code in Visual Studio for some other operation - " To export a query output to an excel spread sheet with certain rows highlighted in color". I tried to attach the same code to a button in Access form but I don't know how to do this. I would first like to understand how to attach a VB code to existing form before I actually get onto copying the tables. The code is as follows:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Pilot_Insert_intoDataSet.common_qry' table. You can move, or remove it, as needed.
Me.Common_qryTableAdapter.Fill(Me.Pilot_Insert_intoDataSet.common_qry)
End Sub
Private Sub dgGridView_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgGridView.RowPostPaint
If e.RowIndex < Me.dgGridView.RowCount - 1 Then
Dim dgvRow As DataGridViewRow = Me.dgGridView.Rows(e.RowIndex)
If dgvRow.Cells(3).Value.ToString = "Strategic" Then
dgvRow.DefaultCellStyle.BackColor = Color.Green
End If
End If
End Sub
Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
Dim style As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle")
style.Font.Bold = True
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green)
For i = 0 To dgGridView.RowCount - 2
For j = 0 To dgGridView.ColumnCount - 1
If (dgGridView(3, i).Value.ToString = "Strategic") Then
xlWorkSheet.Cells(i + 1, j + 1).Style = "NewStyle"
End If
xlWorkSheet.Cells(i + 1, j + 1) = _
dgGridView(j, i).Value.ToString()
Next
Next
xlWorkSheet.SaveAs("E:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file E:\vbexcel.xlsx")
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
Thanks!