Hi folks,
Please look at the following code. I am trying to write data from excel until the last record in the table, including any in between blanks.
This however stops at the first blank cell in the column.
I have been trying to use the EOF method, but I'm not understand the syntax. I've also tried going until record 65536, but that just slows down my database.
Any help is greatly appreciated
Code:
Option Compare Database
Function IsExcelRunning() As Boolean
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
IsExcelRunning = (Err.Number = 0)
Set xlApp = Nothing
Err.Clear
End Function
Function funExportBatch(strFilePath As String, strWorksheet As String)
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim ExcelRunning As Boolean
ExcelRunning = IsExcelRunning()
If ExcelRunning Then
Set xlApp = GetObject(, "Excel.Application")
Else
Set xlApp = CreateObject("Excel.Application")
End If
' Replace QueryOrTableName with the real name of the table or query
' that is to receive the data from the worksheet
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("TestTable", dbOpenDynaset, dbAppendOnly)
Set xlBook = xlApp.Workbooks.Open(strFilePath)
Set xlSheet = xlApp.Worksheets(strWorksheet)
Set xlstart = xlSheet.Range("A2")
r = 3 ' the start row in the worksheet
Do While Len(xlSheet.Range("A" & CStr(r)).Formula) > 0
' repeat until first empty cell in column A
With rst.AddNew ' create a new record
' add values to each field in the record
rst.Fields(1) = xlSheet.Range("A" & r).value
' add more fields if necessary...
rst.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rst.Close
Set rs = Nothing
dbs.Close
Set dbs = Nothing
' Close the EXCEL file without saving the file, and clean up the EXCEL objects
Set xlstart = Nothing
Set xlSheet = Nothing
xlBook.Close False
Set xlBook = Nothing
Set xlApp = Nothing
End Function