Aww heck, I decided to write it up anyhow. You can download the code here ...
http://amazecreations.com/datafast/D...tfromexcel.zip
and the bulk of the logic is posted below. You'll need a table to map the columns
from Excel to Access. The code below is looking for one called ImportColumnSpecs.
The download doesn't have a form to launch it, so you'll have to run from the debug
window with a command like this ...
ProcessFileImport "C:\download\sales.xls","sales_import"
Good Luck
Option Compare Database
Option Explicit
Public Function ProcessFileImport(ByVal sFile As String, ByVal sTable As String) As String
On Error GoTo ProcessFileImport_Error
' Excel object variables
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
' Access object variables
Dim dbs As DAO.Database
Dim rstRead As DAO.Recordset
Dim rstWrite As DAO.Recordset
Dim fld As DAO.Field
' Declared variables
Dim bytWks As Byte
Dim bytMaxPages As Byte
Dim intStartRow As Integer
Dim strData As String
Dim intMaxRow As Integer
Dim strSQL As String
Dim strMsg As String
Dim intLastCol As Integer
Dim intRow As Integer
Dim strCurrFld As String
Dim intCol As Integer
Dim intLen As Integer
Dim varValue As Variant
Dim lngErrs As Long
Const cPassword As String = "xxx999"
DoCmd.Hourglass True
' Create the Excel Applicaiton, Workbook and Worksheet and Database object
Set appExcel = Excel.Application
Set wbk = appExcel.Workbooks.Open(sFile)
Set dbs = CurrentDb
' You could loop through sheets, but for this example, we'll just do one.
bytMaxPages = 1
' Sometimes there is header info, so the "Start Row" isn't the first one.
' Set this variable to the first row that contains actual data.
intStartRow = 2
For bytWks = 1 To bytMaxPages
' Initialize variables on each pass
Set wks = Nothing
Set rstRead = Nothing
intRow = intStartRow
' Load current worksheet. Find used range to determine row count.
Set wks = appExcel.Worksheets(bytWks)
' Optionally, you can protect / unprotect with a password
'wks.Unprotect (cPassword)
' You need to figure out how many rows this sheet contains, so to know
' how far down to read. That value is saved in intMaxRow
strData = wks.UsedRange.Address
intMaxRow = CInt(Mid(strData, InStrRev(strData, "$")))
strData = ""
' Go get the list of fields for this worksheet from the Field Map table
strSQL = "SELECT [AccessField], [OrdinalPosition] FROM ImportColumnSpecs " & _
"WHERE [ImportName]='" & sTable & "' ORDER BY [OrdinalPosition] ASC;"
Set rstRead = dbs.OpenRecordset(strSQL, dbOpenDynaset)
' If there is a mistake and no specification exists, then exit with message
If rstRead.BOF And rstRead.EOF Then
strMsg = "The import spec was not found. Cannot continue."
MsgBox strMsg, vbExclamation, "Error"
Else
rstRead.MoveLast
rstRead.MoveFirst
intLastCol = rstRead.RecordCount
' The name of the import and destination table should be the same for this
' code to function correctly.
Set rstWrite = dbs.OpenRecordset(sTable, dbOpenDynaset)
Do Until intRow > intMaxRow
' Check row to be sure it is not blank. If so, skip the row
For intCol = 1 To intLastCol
strData = strData & Trim(Nz(wks.Cells(intRow, intCol), ""))
Next
If strData = "" Then
intRow = intRow + 1
Else
rstWrite.AddNew
Do Until rstRead.EOF
' Loop through the list of fields, processing them one at a time.
' Grab the field name to simplify code and improve performance.
strCurrFld = Nz(rstRead!AccessField, "")
intCol = rstRead!OrdinalPosition
' Make sure that text fields truncate data at prescribed limits.
' Users may not enter supply more text than the fields can contain.
If dbs.TableDefs(sTable).Fields(strCurrFld).Type = dbText Then
intLen = dbs.TableDefs(sTable).Fields(strCurrFld).Size
varValue = Left(Nz(wks.Cells(intRow, intCol), ""), intLen)
Else
varValue = wks.Cells(intRow, intCol)
End If
' The database schema requires that empty fields contain NULL, not
' the empty string.
If varValue = "" Then varValue = Null
' Handle date columns. Sometimes Excel doesn't format them as dates
If InStr(1, strCurrFld, "Date") > 0 Then
If Not IsDate(varValue) Then
If IsNumeric(varValue) Then
On Error Resume Next
varValue = CDate(varValue)
If Err.Number <> 0 Then
If Left(CStr(varValue), 2) = "20" Then
varValue = CDate(Left(varValue, 4) & "-" & _
Mid(varValue, 5, 2) & "-" & _
Right(varValue, 2))
End If
Err.Clear
End If
On Error GoTo ProcessFileImport_Error
Else
lngErrs = lngErrs + 1
varValue = Null
End If
End If
rstWrite.Fields(strCurrFld) = varValue
Else
' If not a date field, then just write the value to the rst
rstWrite.Fields(strCurrFld) = varValue
End If
rstRead.MoveNext
Loop
If Not rstRead.BOF Then rstRead.MoveFirst
rstWrite.Update
' Reset the variables for processing of the next record.
strData = ""
intRow = intRow + 1
'Debug.Print intRow
End If
Loop
Set wks = Nothing
End If
Next
Exit_Here:
' Report results
strMsg = "finished"
ProcessFileImport = strMsg
' Cleanup all objects (resume next on errors)
On Error Resume Next
Set wks = Nothing
wbk.Close True
Set wbk = Nothing
appExcel.Quit
Set appExcel = Nothing
Set rstRead = Nothing
Set rstWrite = Nothing
Set dbs = Nothing
DoCmd.Hourglass False
Exit Function
ProcessFileImport_Error:
MsgBox Err.Description, vbExclamation, "Error"
Resume Exit_Here
End Function
--
Danny J. Lesandrini
dl*********@hotmail.com http://amazecreations.com/datafast/
"Danny J. Lesandrini" <dl*********@hotmail.com> wrote in ...
I've been doing a lot of importing from Excel lately, and have noticed that
it is the rare spreadsheet that imports cleanly. I've gone to using automation
to loop through rows and insert into a table myself. Are you interested in
this solution? If so, I'll post the code, but it's not a trivial solution and will
require tweaking for each spreadsheet you plan to import.
If you post back, I'll try to clean up the code and post it here.
--