Public Sub ImportXLS(ByVal FilePath As String, _
ByVal SheetName As String, _
ByVal DestTableName)
[COLOR=seagreen]'Note - This function assumes that the import table's field names and the source file's column headers are the same.[/COLOR]
[COLOR=seagreen]' If they are not, it is strongly advised that you use a permanent staging table and clear it before[/COLOR]
[COLOR=seagreen]' using this procedure.[/COLOR]
[COLOR=seagreen]'Note - This function assumes that the comment field for the orders is to be called "Order Comments".[/COLOR]
Dim Workspace As DAO.Workspace
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim rs As DAO.Recordset
Dim LastColumn As Long
Dim LastRow As Long
Dim x As Long
Dim y As Long
Dim OrderCol As Long
Dim Headers() As String
Dim TransActive As Boolean
On Error GoTo ImportXLS_Err
[COLOR=seagreen] 'Open the workbook and worksheet.[/COLOR]
Set wb = Excel.Application.Workbooks.Open(FilePath)
Set ws = wb.Sheets(SheetName)
[COLOR=seagreen]'Create the workspace object.[/COLOR]
Set Workspace = DBEngine.Workspaces(0)
[COLOR=seagreen] 'Set LastColumn and LastRow[/COLOR]
LastColumn = ws.UsedRange.Columns.Count
LastRow = ws.UsedRange.Rows.Count
[COLOR=seagreen] 'Set the size of the array Headers.[/COLOR]
ReDim Headers(1 To LastColumn + 1)
[COLOR=seagreen] 'Load the column names into Headers.[/COLOR]
For x = 1 To LastColumn
Headers(x) = ws.Cells(1, x).Value
[COLOR=seagreen] 'If the header for this row is "Order No", assign that column number to OrderCol.[/COLOR]
If Headers(x) = "Order No" Then OrderCol = x
Next x
[COLOR=seagreen] 'Add the field "Order Comments" to headers in the last spot (LastColumn + 1).[/COLOR]
Headers(LastColumn + 1) = "Order Comments"
[COLOR=seagreen] 'Open the destination table for import.[/COLOR]
Set rs = CurrentDb.OpenRecordset(DestTableName, dbOpenDynaset, dbAppendOnly)
[COLOR=seagreen] 'Create a progress bar.[/COLOR]
SysCmd acSysCmdInitMeter, "Importing data...", LastRow
[COLOR=seagreen] 'Activate the hourglass cursor.[/COLOR]
DoCmd.Hourglass True
With rs
[COLOR=seagreen] 'Cycle through the rows in the spreadsheet.[/COLOR]
For x = 1 To LastRow
[COLOR=seagreen] 'Begin the transaction.[/COLOR]
Workspace.BeginTrans
TransActive = True
[COLOR=seagreen] 'Create the new record.[/COLOR]
.AddNew
[COLOR=seagreen] 'Cycle through the columns in the spreadsheet.[/COLOR]
For y = 1 To LastColumn
[COLOR=seagreen] 'Copy the value from cell x,y into the corresponding field.[/COLOR]
.Fields(Headers(y)).Value = Nz(ws.Cells(x, y))
Next y
[COLOR=seagreen]'Copy the note text from Order No.[/COLOR]
.Fields("Order Comments").Value = Nz(ws.Cells(x, OrderCol).Comment.text)
[COLOR=seagreen]'Save the record.[/COLOR]
.Update
[COLOR=seagreen]'Commit the transaction.[/COLOR]
Workspace.CommitTrans
TransActive = False
[COLOR=seagreen]'Update the progress meter.[/COLOR]
SysCmd acSysCmdUpdateMeter, x
Next x
End With
ImportXLS_Exit:
On Error Resume Next
[COLOR=seagreen]'Remove the meter.[/COLOR]
SysCmd acSysCmdRemoveMeter
[COLOR=seagreen]'Remove the hourglass cursor.[/COLOR]
DoCmd.Hourglass False
[COLOR=seagreen]'Check to see if the workspace reference is still set.[/COLOR]
If Not Workspace Is Nothing Then
[COLOR=seagreen]'Verify the transaction status.[/COLOR]
If TransActive = True Then
[COLOR=seagreen]'Roll back the transaction.[/COLOR]
Workspace.Rollback
TransActive = False
End If
[COLOR=seagreen]'Clear the workspace reference.[/COLOR]
Set Workspace = Nothing
End If
[COLOR=seagreen]'Clear the worksheet reference if still set.[/COLOR]
If Not ws Is Nothing Then Set ws = Nothing
[COLOR=seagreen]'Check the workbook status.[/COLOR]
If Not wb Is Nothing Then
[COLOR=seagreen]'Close the workbook.[/COLOR]
wb.Close
[COLOR=seagreen]'Clear the workbook reference.[/COLOR]
Set wb = Nothing
End If
[COLOR=seagreen]'Clear the recordset reference if still set.[/COLOR]
If Not rs Is Nothing Then Set rs = Nothing
[COLOR=seagreen]'Exit the sub.[/COLOR]
Exit Sub
ImportXLS_Err:
DoCmd.Hourglass False
MsgBox "An error was encountered in procedure ImportXLS!" & vbCrLf & vbCrLf & _
"Error Number: " & vbTab & Err.Number & vbCrLf & _
"Error Desc: " & vbTab & Err.Description, vbCritical
Resume ImportXLS_Exit
End Sub