Function GetXLSData(ByVal sSrcFile As String, ByVal strSheet As String)
Call OpenXLS(sSrcFile, strSheet, "Begin")
Call ExtractData(strSheet)
Call EndXLS(sSrcFile)
End Function
Sub OpenXLS(ByVal sSrcFile As String, Optional ByVal strSheet As String, _
Optional ByVal strMode As String)
Dim objSht As Worksheet
' Open Excel and the specific spreadsheet
If strMode = "Begin" Then
Set objXL = New Excel.Application ' Start New Excel
objXL.Visible = True 'visibility
On Error Resume Next
Set objWkb = objXL.Workbooks.Open(sSrcFile)
If Not Err.Number = 0 Then
' Create the Workbook
Set objWkb = objXL.Workbooks.Add
Err.Number = 0
End If
End If
On Error Resume Next
If Len(strSheet) > 0 Then
Set objSht = objWkb.Worksheets(strSheet)
If Not Err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.Name = strSheet
Err.Number = 0
End If
End If
Err.Clear
On Error GoTo 0
Set objSht = Nothing
End Sub
Function ExtractData(ByVal strSheet As String)
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim objSht As Worksheet
Dim intRng, x As Integer
Dim tdf As DAO.TableDef
Dim strSQL As String
Dim fld As Field
Set dbs = CurrentDb()
Set objSht = objWkb.Worksheets(strSheet)
With objWkb
intRng = Range("A65536").End(xlUp).Row - 1 ' This gets the Grand Total Row
intRng = Range("A" & intRng).End(xlUp).Row ' This gets the actual end row
' If it doesn't exist, create it. If it does, clear the contents
If Not ObjectExists("Table", "Greenbill") Then
Set tdf = dbs.CreateTableDef("Greenbill")
tdf.Fields.Append tdf.CreateField("ClaimNum", dbText, 10)
tdf.Fields.Append tdf.CreateField("Amount", dbDouble, 10)
dbs.TableDefs.Append tdf
Else
strSQL = "DELETE * FROM Greenbill"
DoCmd.RunSQL strSQL
End If
strSQL = "SELECT * FROM Greenbill"
Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)
' Loops through the spreadsheet and adds the data to a table ("greenbill")
With rs
For x = 5 To intRng
.AddNew
If Left(objSht.Cells(x, 2), 1) <> 0 Then
.Fields(0).Value = "0" & objSht.Cells(x, 2)
Else
.Fields(0).Value = objSht.Cells(x, 2)
End If
.Fields(1).Value = objSht.Cells(x, 5)
.Update
Next x
End With
End With
Set tdf = Nothing
Set objSht = Nothing
Set rs = Nothing
Set dbs = Nothing
End Function
Sub EndXLS(ByVal strLoc As String)
' Closes the Excel Object
On Error GoTo HandleErr
objXL.DisplayAlerts = False
objXL.UserControl = True
objWkb.Close True, strLoc
objXL.DisplayAlerts = True
objXL.Quit
Set objWkb = Nothing
Set objXL = Nothing
ExitHere:
Exit Sub
' Error handling block added by Error Handler Add-In. DO NOT EDIT this block of code.
' Automatic error handler last updated at 08-31-2004 08:43:06 'ErrorHandler:$$D=08-31-2004 'ErrorHandler:$$T=08:43:06
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "modAssemble.EndXLS" 'ErrorHandler:$$N=modAssemble.EndXLS
End Select
' End Error handling block.
End Sub