It sounds as if you're importing to a temporary table, always a good
idea. One way of proceeding would be
1) Instead of using TransferSpreadsheet, create and execute an append
query that puts the records into the temporary table.
2) Run another query to check whether there are any records in the
temporary table that have null values where there should be actual
values. If so, count the file as "failed", store its name (and maybe the
primary keys of the guilty records), empty the temp table and go on to
the next file.
The query code might be something like this:
Dim myfile As String
Dim mypath As String
Dim strSQL As String
Dim dbD as DAO.Database
Dim rsR As DAO.Recordset
mypath = "C:\your folder and path\"
myfile = Dir(mypath & "*.xls")
Set dbD = CurrentDB()
Do While Len(myfile) > 0
'Delete records from temp table
strSQL = "DELETE FROM TempTable;"
dbD.Execute strSQL, dbFailOnError
'Append records from worksheet
strSQL = "INSERT INTO TempTable SELECT * FROM " _
& "[Excel 8.0;HDR=Yes;database=" & mypath & myfile _
& ";].[Sheet2$];"
dbD.Execute strSQL, dbFailOnError
'Check for "bad" records
strSQL = "SELECT PrimaryKeyField FROM TempTable " _
& "WHERE FieldThatShouldn'tBeNull IS NULL;"
Set rsR = dbD.OpenRecordset(strSQL)
If rsR.RecordCount > 0 Then
'There's at least one bad record
'Do whatever's necessary
Else
'All is well
'Transfer records to main table
End If
rsR.Close
myfile = Dir()
Loop
Hi John Nurick,
I hope you or anyone can help. let me descibe what i did what messages i got
When importing, if a file is failed, then I got the windows message. The
message like this ...bla bla bla.. ...etc. Do you want to proceed it? Yes,
No, Help. If I click yes, then the file is imported, but the fields that are
incorrect data type will be blank in MS Access Table (Temptable). If I click
"NO", then file is not imported and it brings me to the windows with message
bla lba bla... End, Debug. If I click on Debug, then it brings me to the
Coding windows for debugging.
I have too many files to import. Thus, i have not checked for data
conversion (fields)in Excel. I just want to import all files at once. If file
is failed, then display a message with file name and an OK button. When
clicking on OK, then this file will not be imported and the process will go
ahead to import the other files. I can keep track with file(s) is (are)
failed, so i can open excel file(s) to check for error data.
Somthing like this:
If (DoCmd.TransferSpreadsheet acimport, 8, "your table name here", mypath &
myfile) then
MsgBox "Transfer Failed: " & vbCrLf & "XLS: " & myfile
end if
End If