473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Get most recent file in a folder and then import it with transfertext into a table.

I have the transfer text portion working fine, and I can select the most recent file in excel vba, but I can't get the most recent file portion of the code to work in access vba.

It ends with error "compile error variable not defined"

I'm a bit new at vba, could anyone help?


Private Sub Command6_Click()
On Error GoTo Err_Command6_Click

Set objFSO = CreateObject("Scripting.FileSystemObject")

FolderToScan = "\\10.3.0.144\RSH_Log\DATA"

Set objFolder = objFSO.GetFolder(FolderToScan)

NewestFile = ""
NewestDate = #1/1/1970#

For Each objFile In objFolder.Files
If objFile.DateLastModified > NewestDate Then
NewestDate = objFile.DateLastModified
NewestFile = objFile.Name
End If
Next
FileCopy NewestFile, "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt"
CurrentDb().Execute "DELETE * FROM tImport"
DoCmd.TransferText acImportDelim, "tImport Specification", "tImport", "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt", False
MsgBox "Data has been imported into the tImport table"
End If
Exit_Command6_Click:
Exit Sub

Err_Command6_Click:
MsgBox Err.Description
Resume Exit_Command6_Click

End Sub
Jan 21 '10 #1

✓ answered by ADezii

Try:
Expand|Select|Wrap|Line Numbers
  1. Dim objFile As File
  2. Dim objFolder As Folder
  3. Dim objFSO As FileSystemObject
  4. Dim FolderToScan As String
  5. Dim NewestFile As String
  6. Dim NewestDate As Date
  7.  
  8. Set objFSO = CreateObject("Scripting.FileSystemObject")
  9.  
  10. FolderToScan = "\\10.3.0.144\RSH_Log\DATA"
  11.  
  12. Set objFolder = objFSO.GetFolder(FolderToScan)
  13.  
  14. NewestFile = ""
  15. NewestDate = #1/1/1970#
  16.  
  17. For Each objFile In objFolder.Files
  18.   If objFile.DateLastModified > NewestDate Then
  19.     NewestDate = objFile.DateLastModified
  20.     NewestFile = objFile.Name
  21.   End If
  22. Next
  23.  
  24. FileCopy NewestFile, "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt"
  25.  
  26. CurrentDb().Execute "DELETE * FROM tImport", dbFailOnError
  27. DoCmd.TransferText acImportDelim, "tImport Specification", "tImport", _
  28.                   "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt", False
  29.  
  30. MsgBox "Data has been imported into the tImport table"

3 8085
ADezii
8,834 Expert 8TB
Try:
Expand|Select|Wrap|Line Numbers
  1. Dim objFile As File
  2. Dim objFolder As Folder
  3. Dim objFSO As FileSystemObject
  4. Dim FolderToScan As String
  5. Dim NewestFile As String
  6. Dim NewestDate As Date
  7.  
  8. Set objFSO = CreateObject("Scripting.FileSystemObject")
  9.  
  10. FolderToScan = "\\10.3.0.144\RSH_Log\DATA"
  11.  
  12. Set objFolder = objFSO.GetFolder(FolderToScan)
  13.  
  14. NewestFile = ""
  15. NewestDate = #1/1/1970#
  16.  
  17. For Each objFile In objFolder.Files
  18.   If objFile.DateLastModified > NewestDate Then
  19.     NewestDate = objFile.DateLastModified
  20.     NewestFile = objFile.Name
  21.   End If
  22. Next
  23.  
  24. FileCopy NewestFile, "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt"
  25.  
  26. CurrentDb().Execute "DELETE * FROM tImport", dbFailOnError
  27. DoCmd.TransferText acImportDelim, "tImport Specification", "tImport", _
  28.                   "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt", False
  29.  
  30. MsgBox "Data has been imported into the tImport table"
Jan 22 '10 #2
ADezii
8,834 Expert 8TB
One very important note that I missed is that: You must set a Reference to the Microsoft Scripting Runtime Object Library in order for this code to work.
Jan 22 '10 #3
Thank you, the Reference and the code were both very helpful.
Jan 26 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jeff | last post by:
Hi. I've created a database called japanese. I'm trying to import a japanese dictionary file into the database. The file is located at: http://ftp.cc.monash.edu.au/pub/nihongo/edict.gz Here's...
6
by: Vladislav Moltchanov | last post by:
I have discovered a couple of years ago, that import with DoCMD.TransferText for CSV text file doesnt work in Acc2000, while it works perfectly in ACC97. which has been discussed on this...
4
by: khutch | last post by:
Not that up on MS Access. I understand that the TransferText command can be used to import csv files into a database. Question: Does the text file have to mirror the alignment of the table columns...
0
by: matchine | last post by:
This is a recommendation based on my research on an issue with the transfer text functionality. The comments below were from a tech I approched for help. "The transfer text process blocked...
4
by: Lee | last post by:
I have an application where I need to find the most recent file placed in a folder. For example, my users periodically place an image file with the format "ImageXXXX.jpg" (where XXXX is some...
2
by: msbs48 | last post by:
Hi All very new to Access... having a problem with TransferText I am getting the message "Field 'NoName' doesn't exist in destination table" i saw a previous answer to this but it does not seem to...
5
by: Gregor Kovač | last post by:
Hi! I have a ASCII file that I need to import with METHOD L, since columns are fixed length. The problem is that I have more columns in a table that I'm importing to that there are columns in...
0
by: Julie Warden | last post by:
Group, I'm running Oracle 8.05 with Solaris 6. I have a database with several schemas, and I want to create a test schema from one of the other ones. I'm having a problem with my...
4
by: kkadakia | last post by:
I have a VB code that will allow me to browse for a Excel file. After selecting that file, I want it to imported into Access. Following is the code for browsing a Excel file: Private Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.