Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11

    Import multiple text files from folder & adding filename as record in column.

    Hello All,

    I am newbie, do not have coding knowledge. Looking for help after searching around in web.

    I am in need of importing multiple 100+ text files from a folder in a single table (Table1) designed as below specification, Since i would be importing multiple text files over the 52 weekly period i am thinking of to create a new table after every import.



    Also want to add filename as record along with imported data so I can create correspondence/association of where the data came from.

    I will be getting 100+ files every time, and have to run this weekly

    Is there any solution available?

    My text file has following format and delimited by pipe character

    Field Name Data Type
    Destination Text
    Description Text
    Tariff Number
    Qty Number
    Unit Text
    Value Numer


    Thank you
    Niriv

  2. #2
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    I got a good start by using the code listed by RuralGuy in following post https://www.accessforums.net/import-...ally-2030.html.

    With my import specification and table design was able to get all the text file in my identified table.

    Now I need to get following 2 achieved,

    1)add filename as record to associate the imported data.

    my text file name format is like following

    E_INDEL4_19052013.txt
    E_INCCU1_19052013.txt

    2) After adding filename as record split the file name in 3 more columns,

    Type Listing Date
    E INDEL4 19052013
    E INCCU1 19052013

    Thank you
    Niriv

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Are you planning to make a new table for the FileName or simply another field/fields in the existing table? Do you need to parse the file name now or could you simply store the string and parse it later when you need to? Parsing this filename string is easy.

  4. #4
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    Let me take a step back to explain.


    As a prerequisite, i created a Table1 design (post #1) and specification "ICE", I am able to pull all text files from my folder. Along with this import I like to add file name as field in a column so i could associate the imported records.

    After import of all text files and file name i want to split the file name as post # 2

    Hope was able to explain.



    Option Compare DatabaseOption Explicit
    '************** Code Start **************
    'This code was originally written by Terry Kreft.
    'It is not to be altered or distributed,
    'except as part of an application.
    'You are free to use it in any application,
    'provided the copyright notice is left unchanged.
    '
    'Code courtesy of
    'Terry Kreft
    Private Type BROWSEINFO
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
    End Type
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
    "SHGetPathFromIDListA" (ByVal pidl As Long, _
    ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
    "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
    As Long
    Private Const BIF_RETURNONLYFSDIRS = &H1
    Public Function BrowseFolder1(szDialogTitle As String) As String
    Dim X As Long, bi As BROWSEINFO, dwIList As Long
    Dim szPath As String, wPos As Integer
    With bi
    .hOwner = hWndAccessApp
    .lpszTitle = szDialogTitle
    .ulFlags = BIF_RETURNONLYFSDIRS
    End With
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
    If X Then
    wPos = InStr(szPath, Chr(0))
    BrowseFolder1 = Left$(szPath, wPos - 1)
    Else
    BrowseFolder1 = vbNullString
    End If
    End Function
    '*********** Code End *****************

    Public Sub GetScores()
    '-- Import all of the student scores to the tblScores table.
    Dim OurFolder As String
    Dim OurFile As String
    '-- First have the user select the folder
    OurFolder = BrowseFolder1("Select the folder that contains the scores.")
    '-- Dir returns a ZLS ("") when no more files match the criteria
    OurFile = Dir(OurFolder & "\*.txt")
    Do While OurFile <> ""
    '-- I created and saved the ScoreImportSpecs earlier.
    '-- I also created the tblScores table with the following
    '-- four text fields:
    '-- FirstName, LastName, ClassCode, Score
    DoCmd.TransferText acImportDelim, "ICE", "Table1", OurFile
    '-- Get the next *.txt filename
    OurFile = Dir
    Loop
    End Sub

  5. #5
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    Attaching a sample db, I want to get table 1 populated like sample table 2.
    Attached Files Attached Files

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    No need for second table. Build table and include fields for the parsed file name parts (TYPE, LISTING, FILEDATE) and import data to existing table. Then run UPDATE sql action to populate the 3 fields.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    I do not know whole lot of coding, my effort was for table 2 was to show a sample to others helping me to see how i want my table 1 to look.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Okay, do you still need help with import to existing table followed by UPDATE actions to populate the name parts fields?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    Yes, I do need help.

    So the current code brings all text file in my folder.

    I need to bring the filename along with data and then split the file name as I i indicated in post # 2

    Thank you

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Looks like you already have the import code. So to update the 3 fields, code following the TransferSpreadsheet line like:

    CurrentDb.Execute "UPDATE table1 SET TYPE='" & Left(OurFile,1) & "', LISTING='" & Mid(OurFile, 3, 6) & "', FILEDATE=#" & DateSerial(Mid(OurFile, 14, 4), Mid(OurFile, 12, 2), Mid(OurFile, 10, 2)) & "# WHERE LISTING Is Null"

    Assumes the file names will always follow the structure of the posted examples and FILEDATE is a date/time data type.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    Thanks a bunch, i got code running properly.

    To my current project I got two more items

    1) Is there a way to put a code in VB to check a folder where any text is size is less than 2KB and if there is no data after header row ; delete such files.

    2) In my data set (Attached) ran into import error, i found why import errors have one of the city name is not correctly defined in data set (Attached)

    It should have been NUREMBERG|

    Instead in my data set is N|RNBERG (NUREMBERG)|

    because of this my data set is throwing off in columns.

    Pleas help

  12. #12
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    Another help, Is there a way I could add vlookup in same code?

    I have table 'Location Code', which also has field 'Listing' similar to one we created in update command 'LISTING'

    Can we do vlookup based on listing columns from location code table and bring following fields



    Location Desc
    Country Code
    State

  13. #13
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    For my post #12, I am trying to add below to my code above but throwing up and error Same table 'table1 referenced as both the source and destination in make table query

    CurrentDb.Execute "SELECT table1.*, [Location Code].[Location Desc], [Location Code].[Country Code], [Location Code].State INTO [Table1]FROM table1 LEFT JOIN [Location Code] ON table1.Listing = [Location Code].Listing;"

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Can use FileLen() function to return file size and Kill to delete file: http://word.mvps.org/FAQs/MacrosVBA/DeleteFiles.htm

    If FileLen(OurFile)/1000 <2 Then
    Kill OurFile
    End If

    Not seeing the NUREMBERG issue in the files.

    CurrentDb.Execute is only used with action SQL statements (UPDATE, DELETE, INSERT, etc). A SELECT is not an action statement. It is used to open a recordset object in VBA.

    Explore the domain aggregate functions (DLookup, DSum, DCount, etc.). Access Help has guidelines.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    Niriv is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Posts
    11
    Thank you, i was able to implement SQL command to get

    Further to FileLen()

    I created a separate module, On the first run it worked well, then after adding more files to my folder it did not do anything neither got any error.

    Sub ICEDEL()
    '-- Import all of the student scores to the tblScores table.
    Dim MyFolder As String
    Dim MyFile As String
    '-- First have the user select the folder
    MyFolder = BrowseFolder1("")
    '-- Dir returns a ZLS ("") when no more files match the criteria
    My = Dir(MyFolder & "\*.txt")
    Do While MyFile <> ""
    '-- I created and saved the ScoreImportSpecs earlier.
    '-- I also created the tblScores table with the following
    '-- four text fields:
    '-- FirstName, LastName, ClassCode, Score
    If FileLen(MyFile) / 1000 < 1 Then
    Kill MyFile
    End If
    '-- Get the next *.txt filename
    MyFile = Dir
    Loop
    End Sub


    What could be issue it did not run second time properly.?

    Thnx

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Import multiple text files automatically
    By instructorTek in forum Import/Export Data
    Replies: 30
    Last Post: 10-20-2012, 04:50 PM
  2. Replies: 19
    Last Post: 10-08-2012, 07:47 AM
  3. Transfer all text files in a specified folder
    By rtackett in forum Programming
    Replies: 4
    Last Post: 09-10-2012, 11:50 AM
  4. Replies: 1
    Last Post: 06-20-2012, 12:18 PM
  5. Import multiple Text files with a Macro
    By ArchMap in forum Access
    Replies: 3
    Last Post: 07-01-2011, 04:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums