Hi:
I used the following code to export data from an Access Table ("Invoice Data") to an existing .xlsm workbook ("Control Chart Test.xlsm). The first row that will receive the data from "Invoice Data" will be A14. How do I define the range so that data will be inserted into the first empty row from A14 on everytime this subroutine is executed, rather than having it overwrite existing data. Hope I was clear enough. Thanks in advance.
Private Sub Command6_Click()
Dim Cnn As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim oExcelApp As Excel.Application
Dim lngFieldCounter As Long
Dim blnFileExists As Boolean
Dim blnExcelRunning As Boolean
Dim oTargetSheet As Excel.Worksheet
Set Cnn = CurrentProject.Connection
Set rs1 = New ADODB.Recordset
rs1.Open "Select [Run Date],[Field5] from [Invoice Data]", Cnn, adOpenDynamic, adLockOptimistic
Set oExcelApp = GetObject("", "Excel.Application")
If Err.Number <> 0 Then
blnExcelRunning = False
Set oExcelApp = CreateObject("Excel.Application")
Else
blnExcelRunning = True
End If
If Dir("C:\XL Test\Control Chart Test.xlsm") <> "" Then
blnFileExists = True
oExcelApp.Workbooks.Open FileName:="C:\XL Test\Control Chart Test.xlsm"
Else
oExcelApp.Workbooks.Add
End If
If IsEmpty("BRK2") = False Then
On Error Resume Next
Set oTargetSheet = oExcelApp.ActiveWorkbook.Sheets("BRK2")
If Err.Number <> 0 Then
Set oTargetSheet = oExcelApp.ActiveWorkbook.Sheets.Add
oTargetSheet.Name = "BRK2"
End If
Else
Set oTargetSheet = oExcelApp.ActiveWorkbook.Sheets.Add
End If
oTargetSheet.Range("A14").CopyFromRecordset rs1
rs1.Close
If blnFileExists Then
oExcelApp.ActiveWorkbook.Save
Else
oExcelApp.ActiveWorkbook.SaveAs FileName:="C:\XL Test\Control Chart Test.xlsm"
End If
oExcelApp.ActiveWorkbook.Close
If Not blnExcelRunning Then
oExcelApp.Quit
Set oExcelApp = Nothing
End If
Set rs1 = Nothing
ExportToExcel = True
End Sub
I used the following code to export data from an Access Table ("Invoice Data") to an existing .xlsm workbook ("Control Chart Test.xlsm). The first row that will receive the data from "Invoice Data" will be A14. How do I define the range so that data will be inserted into the first empty row from A14 on everytime this subroutine is executed, rather than having it overwrite existing data. Hope I was clear enough. Thanks in advance.
Private Sub Command6_Click()
Dim Cnn As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim oExcelApp As Excel.Application
Dim lngFieldCounter As Long
Dim blnFileExists As Boolean
Dim blnExcelRunning As Boolean
Dim oTargetSheet As Excel.Worksheet
Set Cnn = CurrentProject.Connection
Set rs1 = New ADODB.Recordset
rs1.Open "Select [Run Date],[Field5] from [Invoice Data]", Cnn, adOpenDynamic, adLockOptimistic
Set oExcelApp = GetObject("", "Excel.Application")
If Err.Number <> 0 Then
blnExcelRunning = False
Set oExcelApp = CreateObject("Excel.Application")
Else
blnExcelRunning = True
End If
If Dir("C:\XL Test\Control Chart Test.xlsm") <> "" Then
blnFileExists = True
oExcelApp.Workbooks.Open FileName:="C:\XL Test\Control Chart Test.xlsm"
Else
oExcelApp.Workbooks.Add
End If
If IsEmpty("BRK2") = False Then
On Error Resume Next
Set oTargetSheet = oExcelApp.ActiveWorkbook.Sheets("BRK2")
If Err.Number <> 0 Then
Set oTargetSheet = oExcelApp.ActiveWorkbook.Sheets.Add
oTargetSheet.Name = "BRK2"
End If
Else
Set oTargetSheet = oExcelApp.ActiveWorkbook.Sheets.Add
End If
oTargetSheet.Range("A14").CopyFromRecordset rs1
rs1.Close
If blnFileExists Then
oExcelApp.ActiveWorkbook.Save
Else
oExcelApp.ActiveWorkbook.SaveAs FileName:="C:\XL Test\Control Chart Test.xlsm"
End If
oExcelApp.ActiveWorkbook.Close
If Not blnExcelRunning Then
oExcelApp.Quit
Set oExcelApp = Nothing
End If
Set rs1 = Nothing
ExportToExcel = True
End Sub