Am busy importing text files and its working OK, save for my attempt to write the filename to each record - its a bit of an after-thought, I suppose
The challenge I'm having is wrting to the field, only if it is empty. That prevents the code from overwrting every record with teh filename of its most recent importee .... Anyone got any insight on this? Am trying to do it with an SQL UPDATE ... SET ...WHERE .... and it's not working .... :banghead:
The challenge I'm having is wrting to the field, only if it is empty. That prevents the code from overwrting every record with teh filename of its most recent importee .... Anyone got any insight on this? Am trying to do it with an SQL UPDATE ... SET ...WHERE .... and it's not working .... :banghead:
Code:
Sub txtfileimport()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
Dim strImpSpec As String
Dim dbs As DAO.Database
Dim strSQL As String
Dim strFileNametoTable As String
Dim dtLastModified As Date
Dim strProcessTimeDate As String
strPath = "G:\ACT SJF files\upd\"
strTable = "UPD"
strImpSpec = "UPD2"
strFile = Dir(strPath & "*.txt")
blnHasFieldNames = True
Set dbs = CurrentDb
Do While Len(strFile) > 0
strPathFile = strPath & strFile
dtLastModified = FileDateTime(strPathFile)
strFilesize = FileLen(strPathFile)
dtSystemtime = Time
dtSystemdate = Date
strProcessTimeDate = dtSystemdate & " " & dtSystemtime
strSQL = "INSERT INTO Vault ([Processed_File],[Filedate],[File Size],[Process Date and time]) " & _
"VALUES ( '" & strFile & "' ,'" & dtLastModified & "','" & strFilesize & "','" & strProcessTimeDate & "')"
strFileNametoTable = "UPDATE '" & strTable & "' .[Processed_File]" & " " & _
"SET '" & strTable & "' .[Processed_File] = '" & strFile & "'" & " " & _
"WHERE '" & strTable & "'.[Processed_File] = '' ;"
dbs.Execute strSQL
DoCmd.TransferText acImportDelim, strImpSpec, strTable, strPathFile, blnHasFieldNames
dbs.Execute strFileNametoTable
' Uncomment out the next code step if you want to delete the
' text file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
dbs.Close
End Sub