I am trying to do the same as mentioned in your response to import excel
tables into access. Here is my code:
Private Sub LoadNewWorkOrders_Click()
Dim strPath As String
Dim strFileName As String
strPath = "C:\Documents and Settings\CMC\New Work Orders\" 'Path to
directory where new w/os are
strFileName = Dir(strPath & "*.xls") 'Dir
function returns only Excel files
Do While Len(strFileName) <> 0 'After Dir
function returns all .xl files, it returns a zero length string ""
If isNull((DLookup("[FileName]","Work Orders","[FileName="' &
strFileName&"'"))Then
DoCmd.TransferSpreadsheet acLink, , "TempTable", strPath & strFileName, True
CurrentDb.Execute ("AppendQuery"), dbFailOnError
DoCmd.DeleteObject acTable, "Temptable"
CurrentDb.Execute("INSERT INTO Work
Orders([Filename],[ImportDate])Values("'&strFilename&'",#"&Date&"#0;"),dbFailOnError
End If
strFileName = Dir()
Loop
End Sub
The if Is Null and the CurrentDb.Execute lines do not compile. I have
something in the syntax wrong but can't find what is wrong. Can you help me
please?
Thanks, JIM
[QUOTE="Klatuu"]
You would need to create a table that would hold the names of the files that
have been imported and check the table before you do the TransferSpreadsheet.
If you find the name in the table, don't import it. If you don't find the
name, import the file and add the file name to the table.
Sub DoImports
Dim strPath as string
Dim strFileName as string
strPath = "F:\SomeDirectory\"
strFileName = Dir(strPath & "*.xls")
Do While Len(strFileName) <> 0
If IsNull((DLookup("[FileName]", "tblImportedFiles", "[FileName = '"
& strFileName & "'")) Then
DoCmd.TransferSpreadsheet acLink, , "TempTable", strPath &
strFileName, True
CurrentDb.Execute("MyAppendQuery"), dbFailOnError
Docmd.DeleteObject acTable, "TempTable"
CurrentDb.Execute("INSERT INTO tblImportedFiles( [FileName],
[ImportDate] ) Values ('" & strFilename & "', #" & Date & "#);"),
dbFailOnError
End If
strFileName = Dir()
Loop
Kevin said:
Thanks and it works now. However, is it possible to let the program rememer
the files name so that it can automatically skip the file already been
inputed? FYI, some people might just input the new table into the same
folder, so as I ran the same program again, it read and input the old table
to the production again.
[/QUOTE]