I disagree with Dave, this may not be a whole lot of code.
First, I would create a temporary table to use to do the initial import of
your data. This table should contain the 4 fields you describe in your
original post, except that you can see I have changed [NAME] to [NameField].
Name is an Access reserved word, and should not be used as a field name.
What does your Subtotal line look like in the spreadsheet? Give us a sample
of a couple of records and the SubTotals row. Hopefully, the subtotal rows
will have a value or missing value that is in all of the other rows.
This is untested air code, and I have not filled in all the blanks, but it
should give you an idea of how I would approach this.
Public Function ImportExcelFiles
Dim strFileName as String
Dim strFileDate as String
Dim strSQL as string
'Delete records from your temp table
currentdb.execute "DELETE * FROM tbl_Temp"
strFileName = DIR(currentproiect.path & "\*.xls")
Do while len(strFileName) > 0
'Check to make sure the Excel filename is structured
'the way you describe below.
If instr(strFileName, "Report") <> 1 then
'do nothing with this file
debug.print "File " & strFileName & " was not imported " _
& "(missing 'Report' at beginning of file
name)!"
Else
'Define the date portion of the filename
strFileDate = Trim(mid(strFileName, 7))
strFileDate = Left(strFileName, len(strFileName) - 4)
if not isdate(strFileDate) then
'do nothing, wrong file type
debug.print "File " & strFileName _
& " was not imported (bad date
format)!"
else
'Do your processing here
'Import records into the temp table (fill in the
parameters as appropriate
Docmd.Transferspreadsheet ....
'Delete the subtotal rows from the temp table (I'd
write and
'save a Delete query and just execute that query
Currentdb.Querydefs("qry_DeleteSubtotals").Execute
'Insert the records into your main table
strSQL = "INSERT INTO tbl_Main (FileDate, Code, " _
&
"NameField, Balance, " _
&
"Employee) " _
& "SELECT #" & cdate(strFileDate) & "#, "
_
& "Code,
NameField, Balance, Employee " _
& "FROM tbl_Temp"
currentdb.execute strsql
'Delete records from temp table
currentdb.execute "DELETE * FROM tbl_Temp"
End if
End if
'You might want to insert some code here to either delete the
processed files, or move them to a backup folder where they could be reused
if necessary.
'Get the next filename provided by the DIR function
strFileName = DIR()
Loop
End Sub
HTH
Dale
small brother said:
Dear All
I need your help to make this import successuful.
I have arround 150 excel files that need to be imported into access.
The files are named "Report dd/mm/yyyy.xls"
The columns are Code-Name-Balance-Employee
First i will need to add a column date where i put the date included in
the
file name.
Second i will need to remove the subtotals (all the files contain a
subtotal
by employee).
Third i will need to import them to Access in one table.
Thanks for your help