Connecting Tech Pros Worldwide Help | Site Map

Access VBA: How to upload .txt files from a specific folder

Alice
Guest
 
Posts: n/a
#1: May 26 '07
I'm trying to upload all .txt files from a specific folder using the
following code, but it didn't work. HELP

The problem lies in DoCmd.TransferText acImportFixed, "Imperial Import
Specification", TableName:="1089_daily", FileName:="myFile", False,
"" where myFile is defines as
myFile = Dir("C:\Temp\test\*.txt") ... but error occurs

Below is the entire code:

Sub FileFiles()

myFile = Dir("C:\Temp\test\*.txt")
Do Until myFile = ""
' upload Shipping report_daily
DoCmd.TransferText acImportFixed, "Imperial Import
Specification", "1089_daily", myFile, False, ""
' append this file into the Master Data
DoCmd.OpenQuery "1089_Append", acViewNormal, acEdit
DoCmd.Close acQuery, "1089_Append"
DoCmd.OpenQuery "1089_imperial_clear", acViewNormal, acEdit
DoCmd.Close acQuery, "1089_imperial_clear"

myFile = Dir
Loop

End Sub

Thanks!

Scott McDaniel
Guest
 
Posts: n/a
#2: May 26 '07

re: Access VBA: How to upload .txt files from a specific folder


On 26 May 2007 14:06:41 -0700, Alice <aliceytam@gmail.comwrote:
Quote:
>I'm trying to upload all .txt files from a specific folder using the
>following code, but it didn't work. HELP
What do you mean by "upload"? Are you moving these to a web server? It appears that you're importing them to your
database, not uploading.

What do you mean by "didn't work"? Did the import fail? Did you get any error messages?


Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
Alice
Guest
 
Posts: n/a
#3: May 26 '07

re: Access VBA: How to upload .txt files from a specific folder


Yea, i mean importing .txt files to the database. I got it to the code
below:

The only problem is i dont know what FileName to say ... under
DoCmd.TransferText acImportFixed, "Imperial Import Specification",
"1089_imperial_daily", "FileName: = ????? ", False, ""

Cause what it is doing right now is ... Access finds say 4 .txt file
in "C:\Temp\test" folder, and for i = 1 to 4, import this text file,
fixed, using this import specification, onto this table, in the file
that Access just found in the folder, ... so what's the language for
"the .txt file that Access is current on? (sorry, i know i'm not being
very clear). below is my code


Sub BatchImport()

With Application.FileSearch
..NewSearch
..LookIn = "C:\Temp\test"
..SearchSubFolders = False
..FileName = "*.txt"


If .Execute() 0 Then 'Files found
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
'upload Shipping report_daily
DoCmd.TransferText acImportFixed, "Imperial Import Specification",
"1089_imperial_daily", "???not sure what file name to put here",
False, ""
' append
DoCmd.OpenQuery "1089_Append", acViewNormal, acEdit
DoCmd.Close acQuery, "1089_Append"
DoCmd.OpenQuery "1089_imperial_clear", acViewNormal, acEdit
DoCmd.Close acQuery, "1089_imperial_clear"
Next i
Else
MsgBox "There were no files found."
End If

End With 'End Filesearch

End Sub






On May 26, 2:57 pm, Scott McDaniel <scott@NoSpam_Infotrakker.com>
wrote:
Quote:
On 26 May 2007 14:06:41 -0700, Alice <alicey...@gmail.comwrote:
>
Quote:
I'm trying to upload all .txt files from a specific folder using the
following code, but it didn't work. HELP
>
What do you mean by "upload"? Are you moving these to a web server? It appears that you're importing them to your
database, not uploading.
>
What do you mean by "didn't work"? Did the import fail? Did you get any error messages?
>
Scott McDaniel
scott@takemeout_infotrakker.comwww.infotrakker.com

Alice
Guest
 
Posts: n/a
#4: May 27 '07

re: Access VBA: How to upload .txt files from a specific folder


On May 26, 3:24 pm, Alice <alicey...@gmail.comwrote:
Quote:
Yea, i mean importing .txt files to the database. I got it to the code
below:
>
The only problem is i dont know what FileName to say ... under
DoCmd.TransferText acImportFixed, "Imperial Import Specification",
"1089_imperial_daily", "FileName: = ????? ", False, ""
>
Cause what it is doing right now is ... Access finds say 4 .txt file
in "C:\Temp\test" folder, and for i = 1 to 4, import this text file,
fixed, using this import specification, onto this table, in the file
that Access just found in the folder, ... so what's the language for
"the .txt file that Access is current on? (sorry, i know i'm not being
very clear). below is my code
>
Sub BatchImport()
>
With Application.FileSearch
.NewSearch
.LookIn = "C:\Temp\test"
.SearchSubFolders = False
.FileName = "*.txt"
>
If .Execute() 0 Then 'Files found
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
'upload Shipping report_daily
DoCmd.TransferText acImportFixed, "Imperial Import Specification",
"1089_imperial_daily", "???not sure what file name to put here",
False, ""
' append
DoCmd.OpenQuery "1089_Append", acViewNormal, acEdit
DoCmd.Close acQuery, "1089_Append"
DoCmd.OpenQuery "1089_imperial_clear", acViewNormal, acEdit
DoCmd.Close acQuery, "1089_imperial_clear"
Next i
Else
MsgBox "There were no files found."
End If
>
End With 'End Filesearch
>
End Sub
>
On May 26, 2:57 pm, Scott McDaniel <scott@NoSpam_Infotrakker.com>
wrote:
>
>
>
Quote:
On 26 May 2007 14:06:41 -0700, Alice <alicey...@gmail.comwrote:
>
Quote:
Quote:
>I'm trying to upload all .txt files from a specific folder using the
>following code, but it didn't work. HELP
>
Quote:
What do you mean by "upload"? Are you moving these to a web server? It appears that you're importing them to your
database, not uploading.
>
Quote:
What do you mean by "didn't work"? Did the import fail? Did you get any error messages?
>
Quote:
Scott McDaniel
scott@takemeout_infotrakker.comwww.infotrakker.com- Hide quoted text -
>
- Show quoted text

Never Mind ... i played around with it and got it to work.

Thanks!

Larry Linson
Guest
 
Posts: n/a
#5: May 27 '07

re: Access VBA: How to upload .txt files from a specific folder


"Alice" <aliceytam@gmail.comwrote
Quote:
Never Mind ... i played around with it and got it to work.
How about sharing your fix with others who may be interested in the issue?

Larry Linson
Microsoft Access MVP


Ironman
Guest
 
Posts: n/a
#6: May 28 '07

re: Access VBA: How to upload .txt files from a specific folder



Hi Alice... Glad to hear that you made it work. Studying your code, I
felt you were on the right track when you started using the DIR function
to iterate through the .TXT files in the subdirectory. As you probably
found out, the variable set to the DIR function holds the file name.
When the DIR function is called again (without parameters) it locates
the next file name.

Also, another way of approaching this import is to concatenate all the
TXT files to one large file before hand. You can do this outside of
Access or use VBA to do it. Since they are all fixed record and you are
importing all records to one table, you wouldn't have to execute your
QUERIES for every file found. You would only have to do it once! Here
is a fragment sample of my code. Good Luck! ...Greg

BTW: I'm importing 9 different files. They are all fixed records, but
they have different columns. Each type of file is then imported, if it
exists, into their respective tables and then deleted to insure that
they don't get imported again.

------------- Start of code fragment ------------------

Dim ImportFile As String

Public Const InFile_rt10 = "c:\PoP\files4Import\rt10.txt"
Public Const InFile_rt15 = "c:\PoP\files4Import\rt15.txt"
Public Const InFile_rt16 = "c:\PoP\files4Import\rt16.txt"
Public Const InFile_rt21 = "c:\PoP\files4Import\rt21.txt"
Closed Thread